Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's some code that does work. Basically, it separates out the alpha channel, resizes the image using gamma correct, resizes the alpha channel <em>without</em> gamma correct, then copies over the alpha channel to the resized image that was done with gamma correct.</p> <p>My guess is that the imagegammacorrect() function has a bug. Perhaps gamma only applies to RGB and GD tries to do the same calculation to the alpha channel as well? Color theory is not my forte.</p> <p>Anyway, here's the code. Unfortunately I could not find a better way to separate out the channels than to loop over the pixels one-by-one. Oh well...</p> <pre><code>&lt;?php // Load image $image = imagecreatefrompng('test-image.png'); // Create destination $resized_image = imagecreatetruecolor(100, 100); imagealphablending($resized_image, false); // Overwrite alpha imagesavealpha($resized_image, true); // Create a separate alpha channel $alpha_image = imagecreatetruecolor(200, 200); imagealphablending($alpha_image, false); // Overwrite alpha imagesavealpha($alpha_image, true); for ($x = 0; $x &lt; 200; $x++) { for ($y = 0; $y &lt; 200; $y++) { $alpha = (imagecolorat($image, $x, $y) &gt;&gt; 24) &amp; 0xFF; $color = imagecolorallocatealpha($alpha_image, 0, 0, 0, $alpha); imagesetpixel($alpha_image, $x, $y, $color); } } // Resize image to destination, using gamma correction imagegammacorrect($image, 2.2, 1.0); imagecopyresampled($resized_image, $image, 0, 0, 0, 0, 100, 100, 200, 200); imagegammacorrect($resized_image, 1.0, 2.2); // Resize alpha channel $alpha_resized_image = imagecreatetruecolor(200, 200); imagealphablending($alpha_resized_image, false); imagesavealpha($alpha_resized_image, true); imagecopyresampled($alpha_resized_image, $alpha_image, 0, 0, 0, 0, 100, 100, 200, 200); // Copy alpha channel back to resized image for ($x = 0; $x &lt; 100; $x++) { for ($y = 0; $y &lt; 100; $y++) { $alpha = (imagecolorat($alpha_resized_image, $x, $y) &gt;&gt; 24) &amp; 0xFF; $rgb = imagecolorat($resized_image, $x, $y); $r = ($rgb &gt;&gt; 16 ) &amp; 0xFF; $g = ($rgb &gt;&gt; 8 ) &amp; 0xFF; $b = $rgb &amp; 0xFF; $color = imagecolorallocatealpha($resized_image, $r, $g, $b, $alpha); imagesetpixel($resized_image, $x, $y, $color); } } imagepng($resized_image, 'test-image-scaled.png'); ?&gt; </code></pre> <p>Replace hard-coded values with variables of course... And here's the result I get using your image and my code:</p> <p><a href="http://www.jejik.com/sander/test-image-scaled.png" rel="nofollow noreferrer">Resized image http://www.jejik.com/sander/test-image-scaled.png</a></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