djfox Posted June 27, 2009 Share Posted June 27, 2009 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? Quote Link to comment Share on other sites More sharing options...
djfox Posted June 27, 2009 Author Share Posted June 27, 2009 All right, am I to assume that there is another way to accomplish a similar task in saving myself from creating millions of images? Quote Link to comment Share on other sites More sharing options...
djfox Posted June 27, 2009 Author Share Posted June 27, 2009 No one has any idea? Quote Link to comment Share on other sites More sharing options...
Alex Posted June 27, 2009 Share Posted June 27, 2009 You'll have to use the PHP GD library. There are many tutorials are online. What you want to accomplish is pretty simple. Some functions you might want to look into: Php GD image*() (png, gif, jpeg) imagecreatefrom*() '' imagecopymerge() imagecolortransparent() imagecolorallocate() Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 27, 2009 Share Posted June 27, 2009 Can you give a sample input image (i.e. a shape you want to color)? Quote Link to comment Share on other sites More sharing options...
djfox Posted June 27, 2009 Author Share Posted June 27, 2009 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: Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 27, 2009 Share Posted June 27, 2009 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). Quote Link to comment Share on other sites More sharing options...
djfox Posted June 27, 2009 Author Share Posted June 27, 2009 I thought that png would be best because it has the transparency of gif and the colour pallet of jpg and unlike gif, the edges aren`t pixely and jagged.. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 27, 2009 Share Posted June 27, 2009 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. Quote Link to comment Share on other sites More sharing options...
djfox Posted June 27, 2009 Author Share Posted June 27, 2009 Aw man. Ok, maybe I can cook up another way then to get something similar. Quote Link to comment Share on other sites More sharing options...
zq29 Posted June 28, 2009 Share Posted June 28, 2009 I put together some code a while back which may not fit your case exactly, though it may get you on the right track. Check it out here. Quote Link to comment 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.