Jump to content

Help with GD and changing the color of an image


ladymeow

Recommended Posts

I'm trying to come up with a script that will change the color of a transparent png to a new color. Like #FF0000 to #0000FF. The images I'm using are solid color with a transparent background. Anyways it's not going so well. I have this script that works for palpated images but when I try to use it with an image that has alpha transparency it gets all weird on me and shows up as solid blocks of color.

 

<?php
   $pic = imagecreatefrompng("base.png");
   $colorToChange = "FF0000";
   $newColor = "0000FF";

   $c1 = sscanf($colorToChange,"%2x%2x%2x");
   $c2 = sscanf($newColor ,"%2x%2x%2x");

   $cIndex = imagecolorexact($pic,$c1[0],$c1[1],$c1[2]);
   imagecolorset($pic,$cIndex,$c2[0],$c2[1],$c2[2]);

   header("Content-Type: image/png");
   imagepng($image);
   imagedestroy($image);
?> 

 

I want to be able to change the color of a png image that has alpha transparency so that I can keep the soft line rather then having a pixilated edge. Is this even possible to do with PHP? Please help point me in the right direction. I'm very new to GD and PHP.

Link to comment
Share on other sites

Could you not isolate the alpha channel value for each pixel and turn it up to 100%. If the alpha value is less than 50% (for example) then replace the colour with white at 100%?

 

Then create a loop to do this for every pixel and you'll have a new image that you could create as a JPG if necessary.

Link to comment
Share on other sites

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.