PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Wed Dec 17, 2014 4:40 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sun May 23, 2010 9:39 am 
Offline
Forum Newbie

Joined: Mon May 17, 2010 10:19 am
Posts: 3
Hi:

I have developed a simple PHP code to convert some defined colors of an image to transparent, while keep the other colors (including transparent areas) as in the original image. The code is as follow:

Syntax: [ Download ] [ Hide ]
<?php
function Verify($Red,$Green,$Blue) {
        //Here I define the colors to convert to transparent
       
        if ($Red==245 and $Green==0 and $Blue==0) return false;
        if ($Red==196 and $Green==227 and $Blue==0 return false;
        return true;
}

$image_source = imagecreatefrompng('test.png');
$new_image = imagecreatetruecolor(256, 256);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$Fill = imagecolorallocatealpha($new_image,0,0,0,127);
imagefill($new_image, 0, 0, $Fill);

for ($y=0;$y<256;$y++) {
        for ($x=0;$x<256;$x++) {
                $rgb = imagecolorat($image_source,$x,$y);
                $r = ($rgb >> 16) & 0xFF;
                $g = ($rgb >>8) & 0xFF;
                $b = $rgb & 0xFF;              
                if (Verify($r,$g,$b)) {imagesetpixel($new_image,$x,$y,$rgb);}          
        }
}      

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


The script works very well but, as you can image, the performance is not very good. I would like to ask in this forum how to improve the performance of the code.

Thanks in advance.


Last edited by pepe_yola on Mon May 24, 2010 1:35 am, edited 2 times in total.

Top
 Profile  
 
PostPosted: Mon May 24, 2010 1:03 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 12721
Location: New York, NY, US
Moved to Code Critique.

_________________
(#10850)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 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