I want to crop a circle image using PHP but it seems that my new image have some transparent pixels. Of course, I want ONLY the outside area of the ellipse to have background transparent
My code is listed below:
$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');
......
An example of actual generated image is here:
EDIT: generateTransparentImage function has nothing to do with the code listed above; this function generate this image: http://s7.postimage.org/byybq9163/Koala7_500x375_c_transparent.png