Shoutbox

Assistance with PHP and GD - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Assistance with PHP and GD (/showthread.php?tid=81804)

Assistance with PHP and GD by MeEtc on 02-21-2008 at 06:57 PM

I'm in the middle of trying to optimize the php code thats used to generate the images for YASS, cleaning up code, reducing variables and whatnot. In order to help find bottlenecks, I am sending header information containing execution time and memory usage at various points in the script. I ended up graphing them in Excel too, which I have attached for anyone interested. On the graph itself, ignore the last 2 points. the second last one i used a different function to get the memory usage. The last one is the same on all graphs so that they would all be at the same scale.

I basically have 2 bottlenecks right now, the first is using imagecopy/imagecopyresized to create the background for all images. Chances are, this won't be able to be reduced too much.

The second bottleneck is calling imagepng to create the final image. As I have the code right now, the sequence of events is, imagepng is called, and the image created is written to a file. A few lines later, this file is read from disk and sent to the browser. What I want to happen, if possible, is for imagepng to be called, have the image stored in memory, sent to the browser, and then written to disk. Basically, saving from having to do a disk read.

The problem is, imagepng returns a boolean value for if the operation is successful, and not the data for the image. I have no idea if its possible to capture the output buffer to a variable or not, or if this order of events would even be possible.

Any ideas on how to improve it?

The actual code as it is now:

code:
$success = imagepng($main_image, $imgfile);
if ($success = FALSE){
   die('Error creating image');
}else{
   // run a SQL query
}
//output header information, and then the final image.
header('Content-type: image/png');
Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
Header("Expires: Sat, 01 Jan 2000 01:00:00 GMT");
Header("Pragma: no-cache");
       
readfile($imgfile);
imagedestroy($main_image);


On a side note, why are .xls files not allowed to be uploaded?
RE: Assistance with PHP and GD by WDZ on 02-21-2008 at 07:18 PM

quote:
Originally posted by MeEtc
The problem is, imagepng returns a boolean value for if the operation is successful, and not the data for the image. I have no idea if its possible to capture the output buffer to a variable or not, or if this order of events would even be possible.
It's possible, I do it all the time... :p

code:
ob_start();
$success = imagepng($main_image);
$imgdata = ob_get_clean();

imagedestroy($main_image);

if(!$success) {
    die('Error creating image');
}

// ... sql query and headers and stuff ...

echo $imgdata;
file_put_contents($imgfile, $imgdata);

quote:
On a side note, why are .xls files not allowed to be uploaded?
Fixed.
RE: Assistance with PHP and GD by Matti on 02-21-2008 at 07:22 PM

In fact, that's very easy to do. :) You can use the Output Control Functions of PHP for this.

  1. Start buffering the output by calling:
    code:
    ob_start();
  2. Then, let imagepng() send the image to the output. Instead of getting printed, PHP will store it in the output buffer.
    code:
    imagepng($main_image);
  3. Now, you read what imagepng() has outputted with ob_get_flush(). This function gives you the output AND stops the output buffering, so basically it's does the same as ob_get_contents() and ob_end_flush() together, which is interesting in our case.
    code:
    $png_output = ob_get_flush();
  4. Yay, you have a variable containing the PNG output! :D You can now save it to a file and send the PNG output to the browser after all, by simply print()ing it. Don't forget your headers! ;)
Link: Comment by trucex on PHP: Output Control.

Argh, beaten by WDZ... the shame... :P
RE: Assistance with PHP and GD by MeEtc on 02-21-2008 at 07:34 PM

actually, mynetx beat both of you on WLM.
And yes, its working. I just wish now there was some other way to speed up imagepng


RE: Assistance with PHP and GD by ShawnZ on 02-21-2008 at 09:00 PM

why do you need to store it in an output buffer just so you can print the output buffer again? why not just call imagepng?


RE: Assistance with PHP and GD by MeEtc on 02-21-2008 at 09:15 PM

because i need to save it to disk as well, so I can used a cached version of the image if the contents of it haven't changed. saves processing and cpu time