PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Nov 13, 2018 4:20 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Tue Nov 20, 2007 8:14 am 
Offline
Forum Contributor

Joined: Mon Aug 21, 2006 7:09 am
Posts: 272
Location: Romania
I want to replace a color that has alpha channel with one picked by the user but saving the alpha channel in the process. I look on the web and this is done only for png images, I need for gifs and jpg images. The code I made is the common script for replacing colors in an image.
Syntax: [ Download ] [ Hide ]
//rgb is the color picked by the user

$red=hexdec(substr($rgb,0,2));

$green=hexdec(substr($rgb,2,2));

$blue=hexdec(substr($rgb,4,2));

for($i=0;$i<imagecolorstotal($im);$i++){               

  $col=ImageColorsForIndex($im,$i);

  $red_set=$red/255*$col['red']+$ligtht;

  $green_set=$green/255*$col['green']+$ligtht;

  $blue_set=$blue/255*$col['blue']+$ligtht;

  if($col['alpha']<=126) {$alpha=$col['alpha'];}

}

P.S. Sorry about the formating but the Post new topic textarea message doesn' t jump to a new line when I press Enter in FF 2.0.9 .
Edit: ^ Today it works.


Last edited by Rovas on Fri Nov 30, 2007 2:26 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 21, 2007 9:44 am 
Offline
Forum Contributor

Joined: Mon Aug 21, 2006 7:09 am
Posts: 272
Location: Romania
Made some changes to the script but this time the image isn' t displayed and no error message are shown.
Syntax: [ Download ] [ Hide ]
function ImageColorWithTransparency($im, $rgb){

        /*creating a true color replica */

        $x=imagesx($im);

        $y=imagesy($im);

        $im1=imagecreatetruecolor($x,$y);

         $j=0;

        $ligtht=0;

        $red=hexdec(substr($rgb,0,2));

        $green=hexdec(substr($rgb,2,2));

        $blue=hexdec(substr($rgb,4,2));

        for($i=0;$i<imagecolorstotal($im);$i++){

                $col=ImageColorsForIndex($im,$i);

                if($col['alpha']>=126 && j==0){

                                imagecolortransparent($im,$i);

                                imagefill($im1,0,0,$cul);

                                $j++;

                        }

                        else{

                                $red_set=$red/255*$col['red']+$ligtht;

                                $green_set=$green/255*$col['green']+$ligtht;

                                $blue_set=$blue/255*$col['blue']+$ligtht;

                        }

                        if($red_set>255)$red_set=255;

                        if($green_set>255)$green_set=255;

                        if($blue_set>255)$blue_set=255;

                        if($red_set<0)$red_set=0;

                        if($green_set<0)$green_set=0;

                        if($blue_set<0)$blue_set=0;

                        imagecolorset($im1,$i,$red_set,$green_set,$blue_set);

        }

        return $im1;

        imagedestroy($im1);

}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 21, 2007 9:51 am 
Offline
Jedi Mod
User avatar

Joined: Tue Dec 21, 2004 6:03 pm
Posts: 5263
Location: usrlab.com
ImageColorsForIndex() won't work for JPEGs or 24bit (32bit) PNG files. But ignoring that..

How are you trying to display the image?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 21, 2007 11:16 am 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
Syntax: [ Download ] [ Hide ]
return $im1;

   imagedestroy($im1);


FYI, functions are terminated when they hit a return.. so your imagedestroy() will never be fired.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 22, 2007 2:46 am 
Offline
Forum Contributor

Joined: Mon Aug 21, 2006 7:09 am
Posts: 272
Location: Romania
Thank you JCart for that tip.
onion2k: The display format is gif. That' s way I don' t use
Syntax: [ Download ] [ Hide ]
imagealphablending()

//and

imagesavealpha();

I put some echo in the function and found out that the transparent color is correctly identified for gifs, the formula is good but I didn' t find out why is not making the image :( .


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 22, 2007 7:47 am 
Offline
Jedi Mod
User avatar

Joined: Tue Dec 21, 2004 6:03 pm
Posts: 5263
Location: usrlab.com
Without seeing the code you're using to try to output the image there's not much anyone can do to help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 22, 2007 8:09 am 
Offline
Forum Contributor

Joined: Mon Aug 21, 2006 7:09 am
Posts: 272
Location: Romania
I changed the function a bit after I observed that in gif image the last color is the one with the transparent pixels, added the header for the image but it still doesn' t save transparency and for some images it omits some parts of it.
Syntax: [ Download ] [ Hide ]
function ImageColorWithTransparency($im, $rgb){

        $j=0;

        $ligtht=0;

       

        $red=hexdec(substr($rgb,0,2));

        $green=hexdec(substr($rgb,2,2));

        $blue=hexdec(substr($rgb,4,2));

       

        //echo "Before the  for <br />";

        for($i=0;$i<imagecolorstotal($im);$i++){

                $col=imagecolorsforindex($im,$i);

                 if($col['alpha']>=126 &&$j==0){

                                $id=imagecolortransparent($im);

                                //echo " <h5>Transparent color found:" .$sir."!</h5> <br />";

                                //echo "Id is: " .$id;

                                imagecolortransparent($im,$id);

                                imagefill($im,0,0,$id);

                                $j++;

                        }

                        else{

                                //echo "It changes color at stepl " .$i ."<br />";

                                $red_set=$red/255*$col['red']+$ligtht;

                                $green_set=$green/255*$col['green']+$ligtht;

                                $blue_set=$blue/255*$col['blue']+$ligtht;

                                if($red_set>255)$red_set=255;

                                if($green_set>255)$green_set=255;

                                if($blue_set>255)$blue_set=255;

                                if($red_set<0)$red_set=0;

                                if($green_set<0)$green_set=0;

                                if($blue_set<0)$blue_set=0;

                        }

                        //echo "Color image <br />";

                        imagecolorset($im,$i,$red_set,$green_set,$blue_set);

        }

        return $im;

}



$path="../css/pics/";

$col="";

if(!isset($_GET['rgb'])) $col="000000";

else{$col=$_GET['rgb'];}

if(!isset($_GET['file'])){echo "file is not defined"; exit;}

else{

        $im=$path .$_GET['file'];

        //echo $im;

        $imag=imagecreatefromgif($im);

        $pict=ImageColorWithTransparency($imag, $col);

        header("Content-type:image/gif");

        imagegif($pict);

        imagedestroy($pict);

}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 23, 2007 6:57 am 
Offline
Forum Contributor

Joined: Mon Aug 21, 2006 7:09 am
Posts: 272
Location: Romania
I made it work by removing some echo that I forgot to comment :oops: . But it only works for some gifs while for others it puts transparent pixels where there aren' t in the original (i. e. changes their alpha channel) :( .


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 30, 2007 2:28 am 
Offline
Forum Contributor

Joined: Mon Aug 21, 2006 7:09 am
Posts: 272
Location: Romania
The solution is pretty simple just use imagecolorset, there is no need to verify which color has alpha channel but only for gifs.


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

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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