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