php 图片圆形化处理

    xiaoxiao2021-03-25  102

    用到的php gd库函数有

    imagecolorat

    imagesetpixel

    首先是把图片处理成圆形的:

    原图如下:

    处理过之后效果:

    用以下公式计算

    (x-a)*(x-a)+(y-b)*(y-b)<r2

    公式成立说明当前x,y点在圆内

    x,y为当前的坐标

    a,b为圆的圆心位置

    r为半径

    先创建一张透明的图片,

    然后一行一行的扫描原图如图像素点在圆内就画出这个像素不在的就保持透明色就可以

    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;     }

    转载请注明原文地址: https://ju.6miu.com/read-11495.html

    最新回复(0)