2

My goal

I have an existing PNG. It currently has anti-aliasing, or in other words, shades of gray. I want the image to be 1 bit, or in other words, only using the colors black and white. My aim is to do this with PHP GD. I have to do this with an existing image, and can not create the image from scratch using imagecreatetruecolor.

What I'm trying

The function I've found that seems best for the job is imagetruecolortopalette http://php.net/manual/en/function.imagetruecolortopalette.php

Here is a simple version of what I'm trying to do

$user_design = base64_decode($_POST['input']);
$design_file = fopen('path/filename.png', 'w') or die("Unable to open file!");
imagetruecolortopalette($design_file, false, 1);
fwrite($design_file, $user_design);
fclose($design_file);

With this being the key line. The 1 being the "maximum number of colors that should be retained in the palette."

imagetruecolortopalette($design_file, false, 1);

Behavior I'm getting

The image appears unchanged. I'm not sure if I'm using PHP GD correctly, or if this function doesn't do what I think it does.

Other ideas

These also seem promising.

imagecolordeallocate seems like I may be able to use it to deallocate colors, but not sure how to do this without calling it 254 times.

http://php.net/manual/en/function.imagecolordeallocate.php

imagecolorset seems like I may be able to use it to set the palette, but I'm not sure how to do this for existing images.

http://php.net/manual/en/function.imagecolorset.php

Mostly I suspect imagetruecolortopallette is the best bet, but any and all ideas welcome.

Goose
  • 4,764
  • 5
  • 45
  • 84
  • possible duplicate of [Create 1 bit bitmap (monochrome) in php](http://stackoverflow.com/questions/11167706/create-1-bit-bitmap-monochrome-in-php) – MaxVT Jul 10 '15 at 13:55
  • No, because that is how to create a 1 bit bitmap from scratch, and I need to do this with an existing image. I will change my question to clarify this. – Goose Jul 10 '15 at 13:59
  • For a 1-bit image, the max. number of colors should be 2. A 1-color image is a flat block of color. – MaxVT Jul 10 '15 at 17:47
  • I didn't get a flat block of color, I got the original image, so either I'm not using GD correctly, or this isn't the correct function. I'm not sure which. I think I'm not able to use a resource like a file with GD, and GD only accepts strings and can only create images from scratch. Any knowledge on this would be appreciated. – Goose Jul 10 '15 at 19:23

1 Answers1

1
$im = imagecreatefromstring($user_design);
imagefilter($im, IMG_FILTER_GRAYSCALE);
imagefilter($im, IMG_FILTER_CONTRAST, -1000);
imagepng($im, 'path/filename.png');
imagedestroy($im);

How do you convert an image to black and white in PHP

Community
  • 1
  • 1
  • 1
    This works, however, as per the comments in the question you linked, I had to change it to `imagefilter($im, IMG_FILTER_CONTRAST, -1000);` with `-1000` instead of `-100` – Goose Jul 13 '15 at 14:53
  • Also, it is making the background, (or perhaps text on a path elements) black, even when I comment out `IMG_FILTER_GRAYSCALE` and `IMG_FILTER_CONTRAST`. This, however, is a separate issue and your answer accomplished what I asked for. – Goose Jul 13 '15 at 15:07