Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After couple of hours of testing and kicking my head against the wall, I think I've found solution. Problem was about allocating transparent color using <code>imagecolorallocate()</code>. I did not get it at first sight. It was totally wrong approach. However, <code>imagecolorallocatealpha()</code> has helped me alot.</p> <p>Also, alpha blending must be off before saving alpha channel on working layer. However, it must be done right after creating blank true-color image, like</p> <pre><code> $im = imagecreatetruecolor($w, $h); $alphacolor = imagecolorallocatealpha($img, $r, $g, $b, 127); imagealphablending($im, false); imagesavealpha($im, true); </code></pre> <p>This code is a key for getting smooth corners in transparent area after resize-down.</p> <p>After all, I've wrote this function</p> <pre><code> function imageCreateCorners($sourceImageFile, $radius) { # function body } </code></pre> <p>I've tested it with couple of images and it returned image with smooth corners for every bg color.</p> <pre><code> imagepng(imageCreateCorners('jan_vesely_and_james_gist.jpg', 9), 'test.png'); </code></pre> <h2>Output</h2> <p><strong>Original image</strong></p> <p><img src="https://i.stack.imgur.com/a140b.jpg" alt="enter image description here"></p> <h2>IN BROWSER (Same png file 'test.png')</h2> <p><img src="https://i.stack.imgur.com/19epz.gif" alt="enter image description here"></p> <p>It finally returns fully transparent alpha channel so you can use that image on every background you want.</p> <p>I almost forgot to post function code :)</p> <h2>function imageCreateCorners($sourceImageFile, $radius)</h2> <pre><code> function imageCreateCorners($sourceImageFile, $radius) { # test source image if (file_exists($sourceImageFile)) { $res = is_array($info = getimagesize($sourceImageFile)); } else $res = false; # open image if ($res) { $w = $info[0]; $h = $info[1]; switch ($info['mime']) { case 'image/jpeg': $src = imagecreatefromjpeg($sourceImageFile); break; case 'image/gif': $src = imagecreatefromgif($sourceImageFile); break; case 'image/png': $src = imagecreatefrompng($sourceImageFile); break; default: $res = false; } } # create corners if ($res) { $q = 10; # change this if you want $radius *= $q; # find unique color do { $r = rand(0, 255); $g = rand(0, 255); $b = rand(0, 255); } while (imagecolorexact($src, $r, $g, $b) &lt; 0); $nw = $w*$q; $nh = $h*$q; $img = imagecreatetruecolor($nw, $nh); $alphacolor = imagecolorallocatealpha($img, $r, $g, $b, 127); imagealphablending($img, false); imagesavealpha($img, true); imagefilledrectangle($img, 0, 0, $nw, $nh, $alphacolor); imagefill($img, 0, 0, $alphacolor); imagecopyresampled($img, $src, 0, 0, 0, 0, $nw, $nh, $w, $h); imagearc($img, $radius-1, $radius-1, $radius*2, $radius*2, 180, 270, $alphacolor); imagefilltoborder($img, 0, 0, $alphacolor, $alphacolor); imagearc($img, $nw-$radius, $radius-1, $radius*2, $radius*2, 270, 0, $alphacolor); imagefilltoborder($img, $nw-1, 0, $alphacolor, $alphacolor); imagearc($img, $radius-1, $nh-$radius, $radius*2, $radius*2, 90, 180, $alphacolor); imagefilltoborder($img, 0, $nh-1, $alphacolor, $alphacolor); imagearc($img, $nw-$radius, $nh-$radius, $radius*2, $radius*2, 0, 90, $alphacolor); imagefilltoborder($img, $nw-1, $nh-1, $alphacolor, $alphacolor); imagealphablending($img, true); imagecolortransparent($img, $alphacolor); # resize image down $dest = imagecreatetruecolor($w, $h); imagealphablending($dest, false); imagesavealpha($dest, true); imagefilledrectangle($dest, 0, 0, $w, $h, $alphacolor); imagecopyresampled($dest, $img, 0, 0, 0, 0, $w, $h, $nw, $nh); # output image $res = $dest; imagedestroy($src); imagedestroy($img); } return $res; } </code></pre> <p>Function returns <strong>GD object</strong> or <strong>false</strong>.</p> <hr> <p>Function works with solid JPEG, GIF and PNG images. Also, it works great with transparent PNGs and GIFs.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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