Jump to content

Recommended Posts

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:

 

background_12.png

 

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:

 

background_4.png

 

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?

Link to comment
https://forums.phpfreaks.com/topic/188304-dynamically-changing-colors-in-an-image/
Share on other sites

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:

 

image.php

 

(I'll remove that test script soon after you've seen it in action).

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:

 

emblemtest.php

 

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.