2

Before I start, I just want to note that I am a PHP noob. What I want to do is tint a PNG image to one color. So all transparent pixels will remain transparent, and all non-transparent pixels will be that color. I have searched many sites for this answer but for some reason I can't find what I want.

Here is my first attempt based on different examples I found:

<?php
header('Content-Type: image/png');

$color = $_GET['color'];
$im = imagecreatefrompng($_GET['img']);
$width = imagesx($im);
$height = imagesy($im);
$imn = imagecreatetruecolor($width, $height);
imagealphablending($imn,false);
$col=imagecolorallocatealpha($imn,255,255,255,127);
imagesavealpha($imn,true);
imagefilledrectangle($imn,0,0,$width,$height,$col);
imagealphablending($imn,true);
imagecopy($imn, $im, 0, 0, 0, 0, $width, $height);
imagefilter($imn, IMG_FILTER_GRAYSCALE);


if ($color[0] == '#')
$color = substr($color, 1);

if (strlen($color) == 6)
$r = $color[0].$color[1];
$g = $color[2].$color[3];
$b = $color[4].$color[5];

$r = hexdec($r); 
$g = hexdec($g); 
$b = hexdec($b);

imagefilter($imn, IMG_FILTER_COLORIZE, $r, $g, $b);

imagepng($imn);
imagedestroy($imn);

?>

Essentially a perfect example of what I want can be seen here. The only change will be that instead of black, I want it to be converted to the color the user specifies. Convert non-transparent pixels to black

Thank You

=============================== 10/17/2012 Update

So based on xception's answer, here is the code that I used to execute his script:

<?php

$source = "test.png";
$temp = "temp.png";
$color = "red";
$final = "FINAL.png";

exec("convert $source -alpha extract -threshold 0 -negate -transparent white $temp");
exec("convert $temp -fill $color -opaque black $final");
?>

It worked, however there is a small problem. As illustrated in the screenshots below, there are jagged edges. Any ideas on how to smooth the image so it looks as nice as it did in the BEFORE screenshot?

BEFORE:

BEFORE

AFTER:

AFTER

Community
  • 1
  • 1
Marin Petkov
  • 2,128
  • 4
  • 17
  • 23

1 Answers1

1

Example in 2 steps based on the link you pointed to:

convert <source> -alpha extract -threshold 0 -negate -transparent white <tmp>
convert <tmp> -fill red -opaque black <destination>

replace <source>, <tmp>, <destination> with appropriate file names, replace red with the color you want.

EDIT: shorter version found by question author:

exec("convert $source -threshold 100% +level-colors '#00FF00', $final");
xception
  • 4,241
  • 1
  • 17
  • 27
  • Thanks for the comment. So I am new to PHP and am not familiar with ImageMagick and am not sure how to use your example above. Do you think you can show me a full example? Also, if I am reading what you have posted correctly, that script will create an image on the server instead of displaying it? I am in need of something like my first example where it processes the image and displays it with the changes. – Marin Petkov Oct 16 '12 at 00:10
  • Processing images with imagemagick in php is possible, you require the imagick extension, however I don't have enough time to turn this example into a imagick example since the documentation for the fuctions there is very bad - except for the function and parameter names nothing was documented last time I used it - took me a day to figure out how to do what I wanted with it. Try googling the equivalent php imagick code for of each parameters in the command lines I gave you. – xception Oct 16 '12 at 00:22
  • Thank you, I will look into it and will post the example once I have it. – Marin Petkov Oct 16 '12 at 00:23
  • That would be a good idea... if you have some extra time... try submitting some documentation on php.net for those functions as well. – xception Oct 16 '12 at 00:29
  • Ok so I posted the example above with the results. Any ideas on how to fix the jagged edges problem? – Marin Petkov Oct 17 '12 at 05:22
  • Save the alpha(opacity) map of the original image and apply it to the new one. – xception Oct 17 '12 at 16:57
  • 1
    Did a little more playing around and actually this does the trick without the need of an alpha map: `exec("convert $source -threshold 100% +level-colors '#00FF00', $final");` Thanks for your help. – Marin Petkov Oct 20 '12 at 15:01
  • Can I include that in my answers so that others find it more easily (without reading all comments)? – xception Oct 20 '12 at 17:05