There is a regular picture png. monochrome, only some pixels have different transparency.

It is necessary to replace all the pixels with any other color, while maintaining the transparency of the pixel.

How can this be done, please tell me.

Can't determine pixel transparency? need on standard php

  • Look towards imagic - ilyaplot
  • You can not replace all the pixels and superimpose the image of a transparent red square (for example) and thus the dark pixel will look darker and the lighter light, but the tone will change closer to the red, etc. - Naumov

1 answer 1

Pure PHP won't work. The required minimum is the standard PHP extension GD . It is possible that it is already activated.

First of all, you will need to create an image in PHP memory; for this, numerous functions of the imagecreate* family are provided. For example, create from a png file:

 $image = imagecreatefrompng('путь к файлу.png'); 

The imagecolorat function will help determine the color and transparency of a pixel (for convenience, you can additionally apply imagecolorsforindex ). From the documentation:

Result:

 array(4) { ["red"]=> int(119) ["green"]=> int(123) ["blue"]=> int(180) ["alpha"]=> int(127) } 

To set a pixel on an image, use the imagesetpixel function, its signature:

 bool imagesetpixel ( resource $image , int $x , int $y , int $color ) 

Here in the previously created image to the point with coordinates $x , $y pixel is displayed with the color $color , which must also be created.

To get the color with the alpha channel, use the imagecolorallocatealpha function, its signature:

 int imagecolorallocatealpha ( resource $image , int $red , int $green , int $blue , int $alpha ) 

I propose to study the materials on the links, then independently write the script you need is not difficult.