【发布时间】:2012-10-18 11:11:05
【问题描述】:
我想使用 PHP 裁剪圆形图像,但我的新图像似乎有一些透明像素。当然,我只希望椭圆的外部区域具有背景透明
我的代码如下:
$image = imagecreatetruecolor($this->dst_w, $this->dst_h);
imagealphablending($image,true);
imagecopy ( $image , $image_s , 0, 0, $this->src_x, $this->src_y, $this->dst_w, $this->dst_h );
$mask = imagecreatetruecolor($this->src_x, $this->src_y);
$mask = imagecreatetruecolor($this->dst_w, $this->dst_h);
$transparent = imagecolorallocate($mask, 255, 0, 0);
imagecolortransparent($mask, $transparent);
imagefilledellipse($mask, $this->dst_w/2, $this->dst_h/2, $this->dst_w, $this->dst_h, $transparent);
$red = imagecolorallocate($mask, 0, 0, 0);
imagecopymerge($image, $mask, 0, 0, 0, 0, $this->dst_w, $this->dst_h,100);
imagecolortransparent($image, $red);
imagefill($image,0,0, $red);
if ($ext=="jpg" || $ext=="jpeg") {
imagejpeg($image, $this->croppedImage);
} else if ($ext=="png") {
imagepng($image, $this->croppedImage);
}
imagedestroy($image);
imagedestroy($mask);
// <------- END generate cropped Image ------->
// <------- START generate transparent Image ------->
$this->generateTransparentImage('circle');
......
实际生成图像的示例如下:
编辑:generateTransparentImage 函数与上面列出的代码无关;此函数生成此图像: http://s7.postimage.org/byybq9163/Koala7_500x375_c_transparent.png
【问题讨论】:
-
如果你想让别人玩你的代码,你也应该分享原始图像。另外
generateTransparentImage的代码是隐藏的,所以其他人也无法复制。 -
PHP - Mask polygon over image 的可能副本 - 如果您不想使用库,Wideimage 的源代码是免费软件,因此您可以将其移植到您的应用程序中。否则,有很多现有的问题展示了如何做到这一点的各种其他方法,请免费查看:stackoverflow.com/search?q=%5Bphp%5D+%5Bgd%5D+mask
标签: php image-processing gd