云迈博客

您现在的位置是:首页 > 后端开发 > PHP > 正文

PHP

图片转为圆角

刘琦2020-08-28PHP385
图片转为圆角publicfunctionyuanjiao($imgpath){$ext=pathinfo($imgpath);$src_img=null;$info=g

图片转为圆角
public function yuanjiao($imgpath){
$ext = pathinfo($imgpath);
$src_img = null;
$info = getimagesize($imgpath); //取得图片信息,具体请查php手册
$ename=explode(‘/‘,$info[‘mime’]);
$ext=$ename[1];
switch($ext){
case “png”:
$src_img=imagecreatefrompng($imgpath);
break;
case “jpeg”:
$src_img=imagecreatefromjpeg($imgpath);
break;
case “jpg”:
$src_img=imagecreatefromjpeg($imgpath);
break;
case “gif”:
$src_img=imagecreatefromgif($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);
}
}
}

    $ext=pathinfo($imgpath,PATHINFO_EXTENSION);
    if (empty($ext)){
        $ext = "png";
    }
    $rand_name="./uploads/img".md5(time().mt_rand()).".".$ext;
    // imagepng($img);
    switch(strtolower($ext)){
        case 'jpg':
        case 'jpeg':
        case 'jpe':
            imagejpeg($img,$rand_name);
            break;
        case 'png':
            imagepng($img,$rand_name);
            break;
        case 'gif':
            imagegif($img,$rand_name);
            break;
        case 'bmp':
        case 'wbmp':
            imagewbmp($img,$rand_name);
            break;
    }
    imagedestroy($img);
    $newstr = substr($rand_name,1);
    return $newstr;
}

发表评论

评论列表

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~