Recovered png image is black

image example

Note: SO converted above the reference image to JPEG. Here is a transparent PNG .


Below is an example of a code that recreates a png image on a new canvas and retains transparency. As you can see, this also allows you to manipulate the pixel level, for example. with a custom function like custom_func($r, $g, $b) , which is better illustrated at the bottom of this question.

Basically, this code recreates / redraws the above image on a new canvas successfully, as it is. Please note that in this image the sky is completely transparent.

  $image = imagecreatefrompng('grass.png'); $x_dimension = imagesx($image); $y_dimension = imagesy($image); $new_image = imagecreatetruecolor($x_dimension, $y_dimension); // create a transparent canvas $trans_color = imagecolorallocatealpha($new_image, 0x00, 0x00, 0x00, 127); imagefill($new_image, 0, 0, $trans_color); for ($x = 0; $x < $x_dimension; $x++) { for ($y = 0; $y < $y_dimension; $y++) { $rgb = imagecolorat($image, $x, $y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; $alpha = ($rgb & 0x7F000000) >> 24; //$pixel = custom_function($r, $g, $b); imagesetpixel($new_image, $x, $y, imagecolorallocatealpha($image, $r, $g, $b, $alpha)); } } imagesavealpha($new_image, true); imagepng($new_image, 'grass-result.png'); 

However, when I run the same code in this particular png image below.

problematic png image

It gives me an almost black image like this.

very dark blue - almost black recreated image


I would like to understand what is happening here and why? Most importantly, I would like to learn about the possible factors that may affect the process, so I could study it. Why is the result different from one png to another?

Ideally, I would like my code to be able to save and transmit the transparency state (transparent, translucent or opaque) of the original png image, as it is, to the recreated image. As you can see, I was able to achieve this, with the exception of the case described above.


Just in case, here is my environment. Windows 7 - 64 bit ** Wampserver2.5 ** Apache-2.4.9 ** Mysql-5.6.17 ** php5.5.12-64b. Also here is var_dump image with getimagesize() :

 array (size=6) 0 => int 228 1 => int 230 2 => int 3 3 => string 'width="228" height="230"' (length=24) 'bits' => int 8 'mime' => string 'image/png' (length=9) 

UPDATE Here is the proof that the sample image is truly transparent, and that it can be manipulated while maintaining transparency. Note that the bottom of the image is more brownish. This was achieved by slightly modifying this line of imagesetpixel($new_image, $x, $y, imagecolorallocatealpha($image, 100, $g, $b, $alpha));

managed transparent image

+5
source share
1 answer

The second image is 8 bits, which means that it only supports 256 colors. This makes it a "palette-based model" and, as such, does not support alpha transparency.

Just adding the following line after creating $image , you can fix the problem:

 imagepalettetotruecolor($image); 

This does not affect images that are already true color, therefore grass.png continues to be processed correctly. On the PHP page on the page :

Returns TRUE if the conversion is complete, or if the original image is already a true color image, otherwise FALSE is returned.

+1
source

Source: https://habr.com/ru/post/1233584/


All Articles