Hi people! I got up again, I can not move.
The task is to take a PNG image in which there are transparent, translucent, and non-transparent pixels, and replace all opaque pixels with black, and replace all semi-transparent pixels with black, but at the same time, keep it translucent.
Here is the beginning of the code:

$im = imagecreatefrompng('img/1.png'); 

(yes, yes, then it is necessary to begin to discolour further, and with this line I am already oochen with a lot of faith)

Please help!

There are ideas to do this in two cycles, and walk through each pixel, but I can’t collect all my thoughts and knowledge about the functions of the GD library.

  • It seems to me that imagemagic is better suited for this, in theory it’s enough just to reduce the brightness to zero to lower the pictures. - zb '

1 answer 1

All the same, he came. Here's the answer if someone comes in handy:

 $overlay = imagecreatefrompng('img/overlay/1.png'); $color = array(255, 0, 0); list($width, $height) = getimagesize('img/overlay/1.png'); for ($i = 1; $i < $height; $i++) { for ($u = 1; $u < $width; $u++) { $color_index = imagecolorat($overlay, $u, $i); $rgb = imagecolorsforindex($overlay, $color_index); if($rgb['alpha'] < 127 ){ $color2 = imagecolorallocatealpha($overlay, $color[0], $color[1], $color[2], $rgb['alpha']); imagesetpixel($overlay, $u, $i, $color2); } } } imagepng($im, 'img/overlay/img.png');