PHP GD2: how to maintain alpha channel transparency and the right gamma

I was intrigued by this discussion of image scaling, and it later turned out that the PHP code I use to create thumbnails from the uploaded images suffers from the same problem. I decided to try the PHP fix placed near the bottom (gamma conversion from 2.2 to 1.0, image resizing, gamma conversion back from 1.0 to 2.2). This works to solve the problem noted in the article, however this code modification has an unpleasant side effect when choosing the transparency of the PNG alpha channel.

Here is the code that I have with gamma correction.

<?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);
?>

- , , -, - ?

:

  • - PNG -
  • imagegammacorrect()
  • imagegammacorrect()

, , . ( , , - , , : , FireBug .)

http://ender.hosting.emarketsouth.com/images/test-image.png - http://ender.hosting.emarketsouth.com/images/test-image-resized-no-gamma.png ​​- http://ender.hosting.emarketsouth.com/images/test-image-resized.png

+5
2

. , -, -, - -, - , .

, imagegammacorrect() . , RGB, GD -? .

, . , , . , ...

<?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 < 200; $x++) {
    for ($y = 0; $y < 200; $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, 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 < 100; $x++) {
    for ($y = 0; $y < 100; $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);
    }
}

imagepng($resized_image, 'test-image-scaled.png');
?>

, ... , :

http://www.jejik.com/sander/test-image-scaled.png

+2

imagecopyresampled() . php.net .

-1

All Articles