php method to set the picture to a circular picture: 1. Create a PHP sample file; 2. Create a transparent picture; 2. Through "function yuan_img($imgpath) {...}" The method is to process the image into a circle.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to set pictures in php For round pictures?
php Picture rounding processing:
The php gd library functions used are
imagecolorat imagesetpixel
First, round the picture Processed into a circle:
The original picture is as follows:
The effect after processing:
Use the following formula to calculate
(x-a)*(x-a)+(y-b)*(y-b)<r2
If the formula is established, it means that the current x, y point is within the circle
x, y is the current coordinate
a, b is the center position of the circle
r is the radius
First create a transparent picture,
and then scan the original image line by line as shown in the figure where the pixels are within the circle Just keep the transparent color if the pixel is not there
function yuan_img($imgpath) { $ext = pathinfo($imgpath); $src_img = null; switch ($ext['extension']) { case 'jpg': $src_img = imagecreatefromjpeg($imgpath); break; case 'png': $src_img = imagecreatefrompng($imgpath); break; } $wh = getimagesize($imgpath); $w = $wh[0]; $h = $wh[1]; $w = min($w, $h); $h = $w; $img = imagecreatetruecolor($w, $h); //这一句一定要有 imagesavealpha($img, true); //拾取一个完全透明的颜色,最后一个参数127为全透明 $bg = imagecolorallocatealpha($img, 255, 255, 255, 127); imagefill($img, 0, 0, $bg); $r = $w / 2; //圆半径 $y_x = $r; //圆心X坐标 $y_y = $r; //圆心Y坐标 for ($x = 0; $x < $w; $x++) { for ($y = 0; $y < $h; $y++) { $rgbColor = imagecolorat($src_img, $x, $y); if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) { imagesetpixel($img, $x, $y, $rgbColor); } } } return $img; }
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to set the picture to a circular picture in php. For more information, please follow other related articles on the PHP Chinese website!