HaLo2FrEeEk Posted January 13, 2010 Share Posted January 13, 2010 Ok, I have a base image, or actually multiple base images, that use certain colors to map a replacement area. Here is an example of a base image: The blue needs to be replaced with one color and the transparency needs to be another color. This one seems like it would be simple, just replace the blue with the color I want and the alpha with the other color, right? Well I can't seem to find a GD function that does this, and there's the issue of images like these: With the gradient. Anyone have any ideas? As a side note, I'd really rather not do this pixel by pixel, I think that'd take way to long and be way to taxing on the server, these images are all 256x256, that's 65,536 pixels I'd have to check...EVERYTIME the image is loaded. As a reference, with the number of foregrounds, backgrounds, and colors I have to work with, there are a total of 429,981,696 possible unique combinations of primary color, secondary color, foreground, and background images. I don't want to do each image pxel by pixel as it loads. And as a final piece of information, I have TGA versions of all these images that include an alpha channel. In my tests in Photoshop, when I created 2 layers, filled the one on top with what I wanted my primary color to be, and filled the bottom one with my secondary color, then selected, and deleted, the alpha from the base image, I got a perfect result. Is there any way to make an equivalent of this using PHP? Meaning, can PHP access and use the Alpha channel from a, say, BMP or TGA image? Quote Link to comment https://forums.phpfreaks.com/topic/188304-dynamically-changing-colors-in-an-image/ Share on other sites More sharing options...
HaLo2FrEeEk Posted January 13, 2010 Author Share Posted January 13, 2010 Apology for the broken image in the first post, here's what it was supposed to be: Quote Link to comment https://forums.phpfreaks.com/topic/188304-dynamically-changing-colors-in-an-image/#findComment-994085 Share on other sites More sharing options...
mattal999 Posted January 13, 2010 Share Posted January 13, 2010 Here you go: <?php function html2rgb($input) { $input = ($input[0] == "#") ? substr($input, 1, 6) : substr($input, 0, 6); return array( 'r' => hexdec(substr($input, 0, 2)), 'g' => hexdec(substr($input, 2, 2)), 'b' => hexdec(substr($input, 4, 2)) ); } function colorReplace($image, $oldcolor, $newcolor=false) { $rgbold = html2rgb($oldcolor); $oldcolor = imagecolorallocate($image, $rgbold[r], $rgbold[g], $rgbold[b]); imagecolortransparent($image, $oldcolor); if($newcolor) { $rgbnew = html2rgb($newcolor); $newimage = imagecreatetruecolor(imagesx($image), imagesy($image)); $newcolor = imagecolorallocate($image, $rgbnew[r], $rgbnew[g], $rgbnew[b]); imagefill($newimage, 0, 0, $newcolor); imagecopymerge($newimage, $image, 0, 0, 0, 0, imagesx($image), imagesy($image), 100); imagecopymerge($image, $newimage, 0, 0, 0, 0, imagesx($image), imagesy($image), 100); imagedestroy($newimage); } } header("Content-type: image/jpeg"); $img = imagecreatefrompng("http://infectionist.com/images/misc/background_12.png"); colorReplace($img, "0000fe", "ffffff"); colorReplace($img, "ffff00", "000000"); imagejpeg($img); imagedestroy($img); ?> Test: (I'll remove that test script soon after you've seen it in action). Quote Link to comment https://forums.phpfreaks.com/topic/188304-dynamically-changing-colors-in-an-image/#findComment-994162 Share on other sites More sharing options...
HaLo2FrEeEk Posted January 13, 2010 Author Share Posted January 13, 2010 Well 2 things I notice straight off the bat. You're replacing using exact colors, meaning the gradient replacement does absolutely nothing. Next, when replacing colors in an image like the second one I posted (background_12) there is a fringe around the edges of the color changes, example: Notice the little line around the edges? There has to be a way to utilize the alpha channel provided in the BMP or TGA versions of these images. Quote Link to comment https://forums.phpfreaks.com/topic/188304-dynamically-changing-colors-in-an-image/#findComment-994560 Share on other sites More sharing options...
HaLo2FrEeEk Posted January 14, 2010 Author Share Posted January 14, 2010 Bump, please I need help with this! I really can't figure it out, I've tried everything I can think of. I don't want to use ImageMagick because I don't have IMagick installed (ImageMagick for PHP) so I'd have to save the image then display it. There has to be a way to do it, it can't be impossible. Quote Link to comment https://forums.phpfreaks.com/topic/188304-dynamically-changing-colors-in-an-image/#findComment-994644 Share on other sites More sharing options...
HaLo2FrEeEk Posted January 15, 2010 Author Share Posted January 15, 2010 Bump please, guys. mattal, you helped me out first, can't you help me finish this? Quote Link to comment https://forums.phpfreaks.com/topic/188304-dynamically-changing-colors-in-an-image/#findComment-995273 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.