PHP 8.5.0 Alpha 2 available for testing

Voting

: seven minus six?
(Example: nine)

The Note You're Voting On

boukeversteegh at gmail dot com
14 years ago
If you care about speed, you probably already cache your generated images to a file. In that case, DON'T use "createimagefrompng" and "imagepng" to output the image. Use fpassthru instead. It is literally hundreds of times faster.

<?php
header
("Content-Type: image/png");

# Generate cachefile for image, if it doesn't exist
if( !file_exists($cachefile) ) {
$im = generateimage(); # some code generates an image resource
imagepng($im, $cachefile); # store the image to cachefile

# don't output it like this:
/* imagepng($im);*/

imagedestroy($im);
}

$fp = fopen($cachefile, 'rb'); # stream the image directly from the cachefile
fpassthru($fp);
exit;
?>

I've tested it with a 5120x5120 (1.2Mb) image, that was cached on the harddisk. Using imagepng, the transfer took 12 seconds. Using fpassthru, it took only 32ms!

<< Back to user notes page

To Top