What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Assistance with PHP and GD

Assistance with PHP and GD
Author: Message:
MeEtc
Patchou's look-alike
*****

Avatar
In the Shadow Gallery once again

Posts: 2200
Reputation: 60
38 / Male / Flag
Joined: Nov 2004
Status: Away
O.P. Assistance with PHP and GD
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?

.zip File Attachment: execTimes.zip (5.9 KB)
This file has been downloaded 163 time(s).

This post was edited on 02-21-2008 at 07:02 PM by MeEtc.
[Image: signature/]     [Image: sharing.png]
I cannot hear you. There is a banana in my ear.
02-21-2008 06:57 PM
Profile PM Web Find Quote Report
WDZ
Former Admin
*****

Avatar

Posts: 7106
Reputation: 107
– / Male / Flag
Joined: Mar 2002
RE: Assistance with PHP and GD
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.

This post was edited on 02-21-2008 at 07:27 PM by WDZ.
02-21-2008 07:18 PM
Profile PM Web Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: Assistance with PHP and GD
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

This post was edited on 02-21-2008 at 07:23 PM by Matti.
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
02-21-2008 07:22 PM
Profile E-Mail PM Web Find Quote Report
MeEtc
Patchou's look-alike
*****

Avatar
In the Shadow Gallery once again

Posts: 2200
Reputation: 60
38 / Male / Flag
Joined: Nov 2004
Status: Away
O.P. RE: Assistance with PHP and GD
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
[Image: signature/]     [Image: sharing.png]
I cannot hear you. There is a banana in my ear.
02-21-2008 07:34 PM
Profile PM Web Find Quote Report
ShawnZ
Veteran Member
*****

Avatar

Posts: 3146
Reputation: 43
32 / Male / Flag
Joined: Jan 2003
RE: Assistance with PHP and GD
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?
Spoiler:
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
02-21-2008 09:00 PM
Profile PM Web Find Quote Report
MeEtc
Patchou's look-alike
*****

Avatar
In the Shadow Gallery once again

Posts: 2200
Reputation: 60
38 / Male / Flag
Joined: Nov 2004
Status: Away
O.P. RE: Assistance with PHP and GD
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
[Image: signature/]     [Image: sharing.png]
I cannot hear you. There is a banana in my ear.
02-21-2008 09:15 PM
Profile PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On