一个简单的裁切成圆形图片的方法
function cuttingCircularImage($imgpath){
$imageInfo = getimagesize($imgpath);
$mime = $imageInfo['mime'];
$srcImg = null;
switch ($mime) {
case in_array($mime, array('image/jpeg', 'image/jpg')):
$srcImg = imagecreatefromjpeg($imgpath);
break;
case 'image/png':
$srcImg = imagecreatefrompng($imgpath);
break;
default:
return '';
}
$width = $imageInfo[0];
$height = $imageInfo[1];
$w = min($width, $height);
$img = imagecreatetruecolor($w, $w);
imagesavealpha($img, true);
//拾取一个完全透明的颜色,最后一个参数127为全透明
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r = $w / 2; //圆半径
$circleX = $width / 2; //圆心X坐标
$circleY = $height / 2; //圆心Y坐标
for ($x = ($circleX - $r); $x < ($circleX + $r); $x++) {
for ($y = ($circleY - $r); $y < ($circleY + $r); $y++) {
$rgbColor = imagecolorat($srcImg, $x, $y);
if (((($x - $circleX) * ($x - $circleX) + ($y - $circleY) * ($y - $circleY)) < ($r * $r))) {
imagesetpixel($img, ($x - $circleX + $r), ($y - $circleY + $r), $rgbColor);
}
}
}
imagedestroy($srcImg);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
// return $img;
}