I'm trying to use PHP with GD to create a round version of a classic photo (a square one), in PNG with a transparent background... I've succeeded to do it by following some tutorials on the internet (such as http://thedebuggers.com/transparent-circular-crop-using-php-gd/ ), but the circle is not smooth so the quality is not acceptable for my use...
I tried with the following way, but I'm facing a problem: only the top left corner is transparent (and the circle is, during PI/2, totally perfect), but 3/4 of the black background built with GD is still here (the bottom corners and the top right corner).
Could you please help me about this issue?
Thanks in advance,
//$image_s is a resource (photo loaded to be processed)
$width = imagesx($image_s);
$height = imagesy($image_s);
$newwidth = 500;
$newheight = 500;
$image = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image, true);
imagecopyresampled($image, $image_s, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//create masking
$mask = imagecreatetruecolor($newwidth, $newheight);
$transparent = imagecolorallocate($mask, 255, 0, 0);
imagecolortransparent($mask,$transparent);
imagefilledellipse($mask, $newwidth/2, $newheight/2, $newwidth, $newheight, $transparent);
$red = imagecolorallocate($mask, 0, 0, 0);
imagecopymerge($image, $mask, 0, 0, 0, 0, $newwidth, $newheight, 100);
imagecolortransparent($image,$red);
imagefill($image, 0, 0, $red);
$finalImage = imagecreatetruecolor(100, 100);
$transparentColor = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparentColor);
imagealphablending($finalImage, false);
imagesavealpha( $finalImage, true );
imagecopyresampled($finalImage, $image, 0, 0, 0, 0, 100, 100, $newwidth, $newheight);
imagepng($finalImage, $markerPicturePath);
imagedestroy($image);
imagedestroy($mask);
imagedestroy($finalImage);
//$markerPicturePath is now a PNG picture with a round extracted from the photo, with a top left transparent corner and 3 other black corners...
It seems the background needs to be continuous from the top left corner to other corners to allow the transparency everywhere, so I added some space when I build the ellipse.
I edited the imagefilledellipse line to
imagefilledellipse($mask, $tempWidth/2, $tempHeight/2, $tempWidth-2, $tempHeight-2, $transparent);
Thanks,