Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP & GD speed issues.
Old 04-13-2005, 01:52 PM PHP & GD speed issues.
Novice Talker

Posts: 9
Trades: 0
Here's my situation. I'm writing a script that takes a greyscale GIF image and treats it as a 'channel' used to mix two colours and create a colourised version of it. So far things are good. It works exactly as I'd like it to, 'cept its slow. Real, real slow. Without further ado, I put together this page:

http://www.g33kz.co.uk/mh_channel_mix/source.php

You can see the whole source of the script there and test it out using the form at the bottom. I know there's a lot of work still to do, especially in error handling etc. but I want to clear up the performance issue first.

If you read the ISSUES and TODO areas of the comments you'll see a couple of ideas I've had, but before I embark upon them I wanted to run the code by a few more minds.

If I do choose to write my own gif interpreter/writer, I'll need to know how a gif is put together. I know that GIFs are a 256 colour palette followed by a bitmap, all compressed with LZW (it is LZW, right) compression. Still, I wouldn't have the first clue how to read it, decompress it and interpret it the way GD does.

Many thanks in advance. You're all beautiful.
MarkyH is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-13-2005, 04:31 PM
lajkonik86's Avatar
Ultra Talker

Posts: 389
Trades: 0
well i don't know anything about this.

I did once use a bicubic image resizer. It actually interpolated pixels stuff and reevaluated the color. Atleast i think cause i don't understand it really. Anyhowz it is pretty fast, hope it helps out.

Code:
function imageCopyResampleBicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
{
  $scaleX = ($src_w - 1) / $dst_w;
  $scaleY = ($src_h - 1) / $dst_h;

  $scaleX2 = $scaleX / 2.0;
  $scaleY2 = $scaleY / 2.0;

  $tc = imageistruecolor($src_img);

  for ($y = $src_y; $y < $src_y + $dst_h; $y++)
  {
   $sY  = $y * $scaleY;
   $siY  = (int) $sY;
   $siY2 = (int) $sY + $scaleY2;

   for ($x = $src_x; $x < $src_x + $dst_w; $x++)
   {
     $sX  = $x * $scaleX;
     $siX  = (int) $sX;
     $siX2 = (int) $sX + $scaleX2;

     if ($tc)
     {
       $c1 = imagecolorat($src_img, $siX, $siY2);
       $c2 = imagecolorat($src_img, $siX, $siY);
       $c3 = imagecolorat($src_img, $siX2, $siY2);
       $c4 = imagecolorat($src_img, $siX2, $siY);

       $r = (($c1 + $c2 + $c3 + $c4) >> 2) & 0xFF0000;
       $g = ((($c1 & 0xFF00) + ($c2 & 0xFF00) + ($c3 & 0xFF00) + ($c4 & 0xFF00)) >> 2) & 0xFF00;
       $b = ((($c1 & 0xFF)  + ($c2 & 0xFF)  + ($c3 & 0xFF)  + ($c4 & 0xFF))  >> 2);

       imagesetpixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r+$g+$b);
     }
     else
     {
       $c1 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY2));
       $c2 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY));
       $c3 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY2));
       $c4 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY));

       $r = ($c1['red']  + $c2['red']  + $c3['red']  + $c4['red']  ) << 14;
       $g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) << 6;
       $b = ($c1['blue']  + $c2['blue']  + $c3['blue']  + $c4['blue'] ) >> 2;

       imagesetpixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r+$g+$b);
     }
   }
  }
}
__________________
Know what to Download

Please login or register to view this content. Registration is FREE
lajkonik86 is offline
Reply With Quote
View Public Profile
 
Old 04-13-2005, 04:49 PM
Novice Talker

Posts: 9
Trades: 0
It uses the same kind of methodology to cycle through each pixel one at a time. Perhaps I need to implement some benchmarking code to isolate exactly which parts of the script are running slow.
MarkyH is offline
Reply With Quote
View Public Profile
 
Old 04-14-2005, 12:04 AM
Novice Talker

Posts: 9
Trades: 0
Update: Great, great, great speed improvements. I noticed a huge rookie error on my part, since I was loading the channel gif each iteration (once for each pixel) which was an obvious flaw. It's down from around 8 seconds to about 0.25 seconds.

I also added channel cache which dumps the raw channel data into a file so the gif doesn't have to be parsed each time. It saves about a tenth of a second on loading times, which is nice. 0.2 is not so bad, I suppose, but I would prefer it at about 0.05.

Anyone want to suggest any other optimisations?
MarkyH is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP & GD speed issues.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.06169 seconds with 12 queries