Note that there are some explanatory texts on larger screens.

plurals
  1. PORounded transparent _smooth_ corners using imagecopyresampled() PHP GD
    primarykey
    data
    text
    <p>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 <a href="http://php.net/manual/en/function.imageantialias.php" rel="nofollow noreferrer"><code>imageantialias()</code></a> throws Fatal Error since <a href="http://phpfreaks.com/forums/index.php?topic=226019.0" rel="nofollow noreferrer">PHP is running on Debian</a> and re-compiling it is not an option.</p> <p>The trick I've found to make those corners look smooth is resizing the image with <a href="http://php.net/manual/en/function.imagecopyresampled.php" rel="nofollow noreferrer"><code>imagecopyresampled()</code></a> as the following:</p> <ol> <li>prepare the image;</li> <li><i>imagecopyresample</i> it to 10x size;</li> <li>draw corners with a special colour;</li> <li>make that color transparent;</li> <li>decrease the image to it's original size</li> </ol> <p>But here comes the problem: the corners of the result image (after step 5) are <a href="https://i.imgur.com/wKWwc.png" rel="nofollow noreferrer">smooth, but not transparent</a>. When sending to output the image after step 4 (i.e. before decreasing it's size) – <a href="http://s46.radikal.ru/i111/1104/0d/f5828ed4eace.png" rel="nofollow noreferrer">everything's as it should be</a>.</p> <p>Here's the part of the code responsible for making corners rounded:</p> <pre> // $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</pre> <p>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 <code>$colorcode</code> colour. I've been playing with <a href="http://php.net/manual/en/function.imagesavealpha.php" rel="nofollow noreferrer"><code>imagesavealpha()</code></a> and <a href="http://php.net/manual/en/function.imagealphablending.php" rel="nofollow noreferrer"><code>imagealphablending()</code></a> as <a href="https://stackoverflow.com/questions/32243/can-png-image-transparency-be-preserved-when-using-phps-gdlib-imagecopyresampled/32302#32302">advised</a>, but no result.</p> <p>Please help me to make this work.</p> <p>P.S. This may be useful: when uploaded the <a href="http://s46.radikal.ru/i111/1104/0d/f5828ed4eace.png" rel="nofollow noreferrer">large PNG</a> to imgur.com it had it <a href="https://i.imgur.com/BUDLa.jpg" rel="nofollow noreferrer">converted to JPG</a> and as you can see all corners got filled with that very restored $colorcode.</p> <p>P.S. Hope I won't get banned for overusing the word "enlargement" :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload