Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a true grayscale PNG in PHP

I need to create a grayscale image in PHP. I am not talking about an indexed image with grayscale values in its palette, but about a TRUE grayscale image. The difference is in the 26th byte of the PNG (color type):

0 - greyscale  <-- THIS IS WHAT I NEED
2 - RGB
3 - RGB with palette
4 - greyscale + alpha
6 - RGB + alpha

(See How to check a PNG for grayscale/alpha color type? for details)

I tried imagefilter($im, IMG_FILTER_GRAYSCALE); as well as imagetruecolortopalette($im, false, 255); but all I get are either RGB grayscale images (color type 2) or RGB palette images with a grayscale palette (color type 3). I also tried to initialize the image with imagecreate() instead of imagecreatetruecolor() but again this only leads to a palette image.

Is there any way to create a color type 0 grayscale PNG with PHP's GD functions (or any other functions in PHP)?

Here are some samples of different grayscale images to show what I mean. They all look the same, but if you open them in PhotoShop and look at the Image -> Mode setting, you see the difference. Also a hex editor will reveal the difference in the 26th byte:

RGB RGB, color type 2, 3149 bytes
RGB palette RGB palette, color type 3, 3971 bytes
True Grayscale Image True grayscale image, color type 0, 1105 bytes <-- THIS IS WHAT I NEED


UPDATE 01:

Here is the basic code that I use to create the PNGs. Commented lines are alternatives that I have tried:

//$im = imagecreate($image_size, $image_size);
$im = imagecreatetruecolor($image_size, $image_size);

//imagefilter($im, IMG_FILTER_GRAYSCALE);
//imagetruecolortopalette($im, false, 255);

imagepng($im, $imgPathName);
imagedestroy($im);
like image 377
Jpsy Avatar asked Sep 17 '12 14:09

Jpsy


People also ask

How do I make a PNG grayscale?

Right-click the picture that you want to change, and then click Format Picture on the shortcut menu. Click the Picture tab. Under Image control, in the Color list, click Grayscale or Black and White.

How is a grayscale image created?

Grayscale images can be the result of measuring the intensity of light at each pixel according to a particular weighted combination of frequencies (or wavelengths), and in such cases they are monochromatic proper when only a single frequency (in practice, a narrow band of frequencies) is captured.

Is an 8 bit image grayscale?

8 bit color format is one of the most famous image format. It has 256 different shades of colors in it. It is commonly known as Grayscale image. The range of the colors in 8 bit vary from 0-255.

How do I convert an image from RGB to grayscale?

I = rgb2gray( RGB ) converts the truecolor image RGB to the grayscale image I . The rgb2gray function converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance.


1 Answers

GD library does not support converting to a "true" grayscale. It only supports RGB and TrueColor*.

Is there any way to create a color type 0 grayscale PNG with PHP's GD functions (or any other functions in PHP)?

ImageMagick is what you are looking for.

$im = new Imagick();
$im->readImage('file.png');
$im->setImageType(Imagick::IMGTYPE_GRAYSCALE);
$im->writeImage('file.gray.png');
like image 177
h0tw1r3 Avatar answered Sep 20 '22 12:09

h0tw1r3