mouseywings Posted September 21, 2014 Share Posted September 21, 2014 I'm wondering if the GD library is capable of coloring ONLY the non-transparent areas in an image. I had this image white, black, and every other color under the moon, but could only get GD to color the entire canvas (width and height) of the image with an opacity over it. I'm merging many layers on top of each other so I can't afford to color the entire square and lower the opacity and do some weird multiply stuff. That's the image above. Then I have some lineart, markings, eye color, etc. So I had all my possible colors white areas that I layer on top of eachother. I want to color the entire non-transparent areas with a color (dynamic).. so I can't just color it in Photoshop and then upload it or else I would literally have millions of possibilities for every single color and shade.. Thanks for any help. I was trying to go with ImageMagick if GD isn't capable, but I can't even get that downloaded in my Wamp. I'm using 64 bit Wamp so I have no idea if that does something or whatever. I can get it installed on my computer and run it via command line, but I can't get it to work and render images in my Wamp files... can't ever find a .dll that works. Quote Link to comment https://forums.phpfreaks.com/topic/291205-gd-library-colorize-non-transparent-areas-in-image/ Share on other sites More sharing options...
darkfreaks Posted September 22, 2014 Share Posted September 22, 2014 colorizing an image(GD): http://php.net/manual/en/function.imagefilter.php example: http://blog.svnlabs.com/php-colorize-an-image-using-gd/#.VCAxAGO0thE Quote Link to comment https://forums.phpfreaks.com/topic/291205-gd-library-colorize-non-transparent-areas-in-image/#findComment-1491796 Share on other sites More sharing options...
Barand Posted September 22, 2014 Share Posted September 22, 2014 That was fun! Using GD I managed to get the set of four images from your two files (mask and rabbit). The second image was using image colorize filter with same values as bunny #4. (I used the blue background so I could check the transparent areas) It may need some refining but at least it's a start. the HTML for the four images is <img src='mousy2_img.php?r=200&g=120&b=120' border='0' width='200' height='200' alt='output'> <img src='mousy2_img.php?r=0&g=155&b=0' border='0' width='200' height='200' alt='output'> <img src='mousy2_img.php?r=0&g=0&b=155' border='0' width='200' height='200' alt='output'> <img src='mousy2_img.php?r=0&g=150&b=150' border='0' width='200' height='200' alt='output'> and code for mousy2_img.php <?php $colourToAdd = array ('red'=>$_GET['r'], 'green'=>$_GET['g'], 'blue'=>$_GET['b'], 'alpha'=>0); $im = imagecreatefrompng('mousyorig.png'); // your rabbit $mask = imagecreatefrompng('mousy.png'); // your mask overlay $w = imagesx($im); $h = imagesy($im); $mk = imagecolorat($mask,100,100); $trans = imagecolorallocate($im,1,1,1); imagecolortransparent($im, $trans); imagealphablending($im,0); // // scan mask image // for ($y=0; $y<$h; ++$y) { for ($x=0; $x<$w; ++$x) { if (imagecolorat($mask, $x, $y) != $mk) { imagesetpixel($im,$x,$y,$trans); // if transparent in mask } // ensure transparent in image else { addColour($im,$x,$y,$colourToAdd); // apply chosen color to pixel } // using same intensity as original } } function addColour($image, $x, $y, $add) { $col = imagecolorat($image, $x, $y); $c = imagecolorsforindex($image, $col); foreach ($c as $k=>&$v) { $v = $k!='alpha' ? $add[$k]*$v/255 : $add[$k]; } $z = imagecolorallocatealpha($image,$c['red'], $c['green'], $c['blue'], $c['alpha']); imagesetpixel($image,$x,$y,$z); } header("Content-Type: image/png"); imagepng($im); imagedestroy($im); imagedestroy($mask); ?> Quote Link to comment https://forums.phpfreaks.com/topic/291205-gd-library-colorize-non-transparent-areas-in-image/#findComment-1491800 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.