Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP GD2: how to maintain alpha channel transparency and correct gamma
    primarykey
    data
    text
    <p>I was intrigued by <a href="http://www.4p8.com/eric.brasseur/gamma.html" rel="nofollow noreferrer">this discussion</a> of image scaling and subsequently found out that the PHP code I'm using to create thumbnails from uploaded images suffers from the same problem. I decided to try the PHP fix posted near the bottom (converting gamma from 2.2 to 1.0, resizing the image, converting gamma back from 1.0 to 2.2). This works to solve the issue noted in the article, however this modification to the code has the unfortunate side effect of knocking out PNG alpha channel transparency.</p> <p>Here is the code I have with the gamma correction in place.</p> <pre><code>&lt;?php $image = imagecreatefrompng($source_file); $resized_image = imagecreatetruecolor($new_width, $new_height); imagealphablending($resized_image, false); imagesavealpha($resized_image, true); imagegammacorrect($image, 2.2, 1.0); imagecopyresampled($resized_image, $image, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height); imagegammacorrect($resized_image, 1.0, 2.2); imagepng($resized_image, $dest_file); ?&gt; </code></pre> <p>Anyone know how to resize the image, employing the gamma correction trick, while maintaining the alpha channel transparency of the original image?</p> <h1>Edit</h1> <p>sample images:</p> <ol> <li>original file - PNG with alpha channel transparency</li> <li>resized file with both imagegammacorrect() function calls commented out</li> <li>resized file with both imagegammacorrect() function calls in place</li> </ol> <p>You can see that the transparency is fine until you attempt to correct the gamma. (easiest way to see the transparency is working below is to inspect the paragraph tag wrapped around the images and add a background: black; to its style attribute via FireBug or similar.)</p> <p><a href="http://ender.hosting.emarketsouth.com/images/test-image.png" rel="nofollow noreferrer">original image http://ender.hosting.emarketsouth.com/images/test-image.png</a> <a href="http://ender.hosting.emarketsouth.com/images/test-image-resized-no-gamma.png" rel="nofollow noreferrer">no gamma correction http://ender.hosting.emarketsouth.com/images/test-image-resized-no-gamma.png</a> <a href="http://ender.hosting.emarketsouth.com/images/test-image-resized.png" rel="nofollow noreferrer">gamma corrected - no transparency http://ender.hosting.emarketsouth.com/images/test-image-resized.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.
 

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