I need a script which makes rounded transparent corners on supplied image. I've found one and it works good except the one thing: the applied corners do not look smooth. The imageantialias()
throws Fatal Error since PHP is running on Debian and re-compiling it is not an option.
The trick I've found to make those corners look smooth is resizing the image with imagecopyresampled()
as the following:
- prepare the image;
- imagecopyresample it to 10x size;
- draw corners with a special colour;
- make that color transparent;
- decrease the image to it's original size
But here comes the problem: the corners of the result image (after step 5) are smooth, but not transparent. When sending to output the image after step 4 (i.e. before decreasing it's size) – everything's as it should be.
Here's the part of the code responsible for making corners rounded:
// $dest = image resource $q=10; // making everything 10x bigger $new_width=$width*$q; $new_height=$height*$q; $radius=$radius*$q; $magnified=imagecreatetruecolor($new_width, $new_height); imagecopyresampled($magnified, $dest, 0,0, 0,0, $new_width,$new_height, ($new_width/$q),($new_height/$q)); // picking the unique colour $found = false; while($found == false) { $r = rand(0, 255); $g = rand(0, 255); $b = rand(0, 255); if(imagecolorexact($magnified, $r, $g, $b) != (-1)) { $found = true; } } $colorcode = imagecolorallocate($magnified, $r, $g, $b); // drawing corners imagearc($magnified, $radius-1, $radius-1, $radius*2, $radius*2, 180, 270, $colorcode); imagefilltoborder($magnified, 0, 0, $colorcode, $colorcode); imagearc($magnified, $new_width-$radius, $radius-1, $radius*2, $radius*2, 270, 0, $colorcode); imagefilltoborder($magnified, $new_width-1, 0, $colorcode, $colorcode); imagearc($magnified, $radius-1, $new_height-$radius, $radius*2, $radius*2, 90, 180, $colorcode); imagefilltoborder($magnified, 0, $new_height-1, $colorcode, $colorcode); imagearc($magnified, $new_width-$radius, $new_height-$radius, $radius*2, $radius*2, 0, 90, $colorcode); imagefilltoborder($magnified, $new_width-1, $new_height-1, $colorcode, $colorcode); // making the unique colour transparent imagecolortransparent($magnified, $colorcode); // scaling down the enlarged image to it's original size // expecting corners to remain transparent imagecopyresampled($dest, $magnified, 0,0, 0,0, ($new_width/$q),($new_height/$q), $new_width,$new_height); // but they're not // sending $magnified to output for testing purposes $dest=$magnified; // outputting $dest as image/png
So as you can see, the problem occurs when enlarged image is being imagecopyresampled to it's original size. The transparent corners get filled with the $colorcode
colour. I've been playing with imagesavealpha()
and imagealphablending()
as advised, but no result.
Please help me to make this work.
P.S. This may be useful: when uploaded the large PNG to imgur.com it had it converted to JPG and as you can see all corners got filled with that very restored $colorcode.
P.S. Hope I won't get banned for overusing the word "enlargement" :)