A short while ago Ty W posted an interesting question on StackOverflow. Apparently, most graphics software cannot scale images the right way. Usually it's hard to notice the flaw but the linked article does a great job of explaining the problem.
PHP's GD library suffers from the same issue, but Ty discovered that the sample PHP program provided with the article did not work on partially transparent images. After a couple of hours of fiddling I managed to get a working solution.
Apparently, the imagegammacorrect() function in PHP deals badly with images that have an alpha channel. I suspect that it tries to apply the same calculation to the alpha channel that it applies to the red, green and blue channels. To work around this, my solution splits the aplha channel from the original image. The alpha channel is resampled regularly while the red, green and blue channels are resampled using gamma correction.
Here's the code.
- <?php
- // Load image
- $image = imagecreatefrompng($path_to_image);
- // Create destination
- $resized_image = imagecreatetruecolor($new_width, $new_height);
- imagealphablending($resized_image, false); // Overwrite alpha
- imagesavealpha($resized_image, true);
- // Create a separate alpha channel
- $alpha_image = imagecreatetruecolor($width, $height);
- imagealphablending($alpha_image, false); // Overwrite alpha
- imagesavealpha($alpha_image, true);
- for ($x = 0; $x < $width; $x++) {
- for ($y = 0; $y < $height; $y++) {
- $alpha = (imagecolorat($image, $x, $y) >> 24) & 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, $new_width, $new_height, $width, $height);
- imagegammacorrect($resized_image, 1.0, 2.2);
- // Resize alpha channel
- $alpha_resized_image = imagecreatetruecolor($width, $height);
- imagealphablending($alpha_resized_image, false);
- imagesavealpha($alpha_resized_image, true);
- imagecopyresampled($alpha_resized_image, $alpha_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
- // Copy alpha channel back to resized image
- for ($x = 0; $x < $new_width; $x++) {
- for ($y = 0; $y < $new_height; $y++) {
- $alpha = (imagecolorat($alpha_resized_image, $x, $y) >> 24) & 0xFF;
- $rgb = imagecolorat($resized_image, $x, $y);
- $r = ($rgb >> 16 ) & 0xFF;
- $g = ($rgb >> 8 ) & 0xFF;
- $b = $rgb & 0xFF;
- $color = imagecolorallocatealpha($resized_image, $r, $g, $b, $alpha);
- imagesetpixel($resized_image, $x, $y, $color);
- }
- }
- // Write resized image
- imagepng($resized_image, $path_to_resized_image);
- // Clean-up
- imagedestroy($image);
- imagedestroy($resized_image);
- imagedestroy($alpha_image);
- imagedestroy($alpha_resized_image);
- ?>
- <p>I hope this is of help to other people as well!</p>
Comments
Nobody has posted any comments yet.
Comments have been retired for this article.