PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Oct 18, 2018 1:14 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Thu Dec 02, 2004 2:45 pm 
Offline
Forum Newbie

Joined: Thu Dec 02, 2004 2:39 pm
Posts: 5
Does anyone know how i can remove the color from an image on the fly. I have a huge collection of jpeg images that are going to be used for an ecommerce site. The problem is the images all have an extremely ugly background color. I was wondering if anyone knew how to remove this color with the gd library or if it can't be removed, as they are jpegs and wont' support transparency, can the color be coverted to white? There are over 10,000 images and i dont' want to have to go through all of them one by one.

Here is a sample picture:

Image

All of the images have this same unique background color......

Thanks in advance for any help


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 4:10 pm 
Offline
DevNet Resident

Joined: Sun Jul 20, 2003 9:25 pm
Posts: 1150
Location: West Yorkshire, England
You just have to get the width and height of the image, and then loop through each pixel, using imagecolorat() to get the color at the specified pixel. Then check to see if this pixel is the color that you want to change and if so use imagesetpixel() to change the color.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 8:07 pm 
Offline
Forum Newbie

Joined: Thu Dec 02, 2004 2:39 pm
Posts: 5
thanks for the reply. is there anyway you could supply me with a sample chunk of code as i am very new to gd library. If not its ok im sure i can figure it out.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 8:17 pm 
Offline
DevNet Resident

Joined: Sun Jul 20, 2003 9:25 pm
Posts: 1150
Location: West Yorkshire, England
Not tested it, but it would be something like:

Syntax: [ Download ] [ Hide ]
function editAllPixels($image, $find, $replace){

                $details = getimagesize($image);

                $new = imagecreatetruecolor($details[0], $details[1]);

                for($x = 0;$x < $details[0];$x++){

                        for($y = 0;$y < $details[1];$y++){

                                $color = getColorFromColorIndex(imagecolorat($image,$x,$y));

                                if($color == $find){

                                   $colors = imagecolorallocate($new,$replace['r'],$replace['g'],$replace['b']);

                                   imagesetpixel($new,$x,$y,$colors);

                                }

                        }

                }

                return $new;   

        }



        function getColorFromColorIndex($color){

                $array = array();

                $array['r'] = ($color  >> 16) & 0xFF;

                $array['g'] = ($color  >>  & 0xFF;

                $array['b'] = $color  & 0xFF;

                return $array;

        }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 8:28 pm 
Offline
Forum Newbie

Joined: Thu Dec 02, 2004 2:39 pm
Posts: 5
kettle_drum wrote:
Not tested it, but it would be something like:

Syntax: [ Download ] [ Hide ]
function editAllPixels($image, $find, $replace){
                $details = getimagesize($image);
                $new = imagecreatetruecolor($details[0], $details[1]);
                for($x = 0;$x < $details[0];$x++){
                        for($y = 0;$y < $details[1];$y++){
                                $color = getColorFromColorIndex(imagecolorat($image,$x,$y));
                                if($color == $find){
                                   $colors = imagecolorallocate($new,$replace['r'],$replace['g'],$replace['b']);
                                   imagesetpixel($new,$x,$y,$colors);
                                }
                        }
                }
                return $new;   
        }

        function getColorFromColorIndex($color){
                $array = array();
                $array['r'] = ($color  >> 16) & 0xFF;
                $array['g'] = ($color  >>  & 0xFF;
                $array['b'] = $color  & 0xFF;
                return $array;
        }


Thank you very very much. I'll give it a try right now.
:lol:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 10:07 pm 
Offline
Forum Newbie

Joined: Thu Dec 02, 2004 2:39 pm
Posts: 5
Im prob jus stupid but i can't seem to get it to work. maybe im just using the function wrong......


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 10:56 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6445
Location: 53.01N x 112.48W
Keep in mind that this will only convert the EXACT colour listed. The sample picture you showed has anti-aliasing between the image proper, and the background colour. Removing that single shade of turqoise will leave the darker shades that show up around the subject.

Are you familiar with Photoshop? You can set up a batch action that could go through your image directory, open each image, convert the background colour (and use anti-aliasing so it will likely get more shades of turqoise), and re-save it.

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 9:40 pm 
Offline
Forum Newbie

Joined: Thu Dec 02, 2004 2:39 pm
Posts: 5
I don't have photoshop. Do you know if this is possible with macromedia fireworks? Thanks for the idea!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 06, 2004 10:48 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6445
Location: 53.01N x 112.48W
No, I really have no experience with Fireworks.

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 06, 2004 11:15 am 
Offline
Forum Contributor

Joined: Fri Aug 20, 2004 4:28 am
Posts: 155
Location: Falkirk, Scotland
I've got quite a bit of Fireworks experience but have to say I've never particulary tried to look for a batch function, as the only occasional time I need it it is covered by a freeware photoediting app. However, that said, I a) haven't seen one and b) don't believe there is one as it is mainly for the creation of images for the web rather than photoediting. Or maybe I'm very wrong and have just put my foot in it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group