Jump to content

Edit Colour of an Image via PHP?


djfox

Recommended Posts

I`ve seen another site that does this so I know it can be done, I just don`t know how and I want to use a similar thing for my site. How would someone use php to change the colour of an image?

 

Details:

My new system I`d like to put in uses virtual pets. The virtual pets can be of different colours via RBG code (ie, 255|000|000) which will be different for each pet. However, I do not want to create an image for each and every colour, it`s just not possible, especially when there will be a similar system for different eye colours, body colours, secondary body colour, and halo colour. There are no basic shapes (ie, squares, rectangles, etc). These are oblong shapes. I just want the code to change the colour of the image that I input. The image will be a flat colour, with a transparent background (one image for eyes, one image for body, one image for halo, etc). How can I accomplish this via PHP and not make 16777216 images for each element to get the different colours?

Link to comment
Share on other sites

I had looked at those and read through the comments on those pages but could not see any way that those could be used in editing the colour of the image. They all talked about greyscaling, resizing and flipping, not recolouring.

 

For the image, here is the image:

bodycolour.png

Link to comment
Share on other sites

Since it's a PNG with alpha transparency, I'm not sure you can achieve what you're trying to do. If the images had only one color and single channel transparency, you should be able to do it with imagefilltoborder(), imagecolortransparent() and some other functions. I'm thinking you could merge your image with another same sized image with a different color, use imagefilltoborder() to color the shape, and then imagecolortransparent() to make the original transparent part transparent again. And maybe end with blurring the image, to get the alpha transparency effect at the edge back (although I'm not sure if that's possible).

Link to comment
Share on other sites

It is. I would also use PNG, since it supports alpha transparency (for pretty blending) and employs lossless compression. All I'm saying is that AFAIK you can't do what you're trying to, when the source image has alpha transparency (fades from the main color to transparent at the edges). There may be a way, but if there is, it's not straight forward.

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.