比如:把头像转换成圆形等其他用途,PHP将图片转换成圆形的代码如下:
/**
* 将图片转为圆形
* src 原始图片地址
* dst 生成后的圆形图片存储路径
*/
function img_circle($src, $dst){
// 获取原图尺寸,并设置新图片的宽度和高度
list($w, $h) = getimagesize($src);
if( $w > $h ){
$w = $h;
}else{
$h = $w;
}
$oimgSrc = imagecreatefromstring(file_get_contents($src));
$oimgDst = imagecreatetruecolor($w, $h);
imageantialias($oimgDst, true); // 抗锯齿,好像没什么效果
imagealphablending($oimgDst,false);
$transparent = imagecolorallocatealpha($oimgDst, 0, 0, 0, 127);
$r=$w/2;
for($x=0;$x<$w;$x++){
for($y=0;$y<$h;$y++){
$c = imagecolorat($oimgSrc,$x,$y);
$_x = $x - $w/2;
$_y = $y - $h/2;
if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){
imagesetpixel($oimgDst,$x,$y,$c);
}else{
imagesetpixel($oimgDst,$x,$y,$transparent);
}
}
}
imagesavealpha($oimgDst, true);
imagepng($oimgDst, $dst);
imagedestroy($oimgDst);
imagedestroy($oimgSrc);
return true;
} 上一篇: php判断字符串中是否包含指定字符串