1

I am reading images from minimum of 1x1 pixel to maximum of 1600x1600 pixels. I have written three PHP methods and are working perfectly.

// using FGC
function output_fgc($filename)
{
    header("Content-Type: image/png");
    echo file_get_contents($filename);
}

// using fopen
function output_fp($filename)
{
    $handle = fopen($filename, 'rb');

    header("Content-Type: image/png");
    header("Content-Length: " . filesize($filename));

    fpassthru($handle);
}

// using GD
function output_gd($filename)
{
    $im = imagecreatefrompng($filename);

    header('Content-Type: image/png');

    imagepng($im);
    imagedestroy($im);
}

It seems performance is same for all methods. Just want to know which uses more server resources? Are there any better methods than this? Thanks in advance.

Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117
  • The last method is definitely unnecessarily resource-consuming (unless you want to make 10000% super sure your file is actually an image, but that will be overkill in most cases.) – Pekka Mar 16 '13 at 15:29
  • @Pekka웃, All images are PNGs and saved in server. So first two methods are good to go? – Madan Sapkota Mar 16 '13 at 15:32
  • yeah - although you'd have to send your own caching headers if you want them to be cached, and sending static content through PHP is not every efficient in general. Any specific reason why you can't just put the original PNG in a web accessible folder? – Pekka Mar 16 '13 at 15:34
  • go with option 1: `file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.` – bitWorking Mar 16 '13 at 15:36
  • @code I still don't see why you need to use PHP if you're not doing anything with the image, but either way, be sure to send caching headers, see e.g. [How to use HTTP cache headers with PHP](http://stackoverflow.com/q/1971721) – Pekka Mar 16 '13 at 15:39
  • @Pekka웃, When user request `200x100` Pixel placeholder image, first time I am creating with GD, Save and Output, next time if same size is requested, I am just reading the saved file and outputting to the browser instead creating new again. – Madan Sapkota Mar 16 '13 at 15:42
  • pekka is right..if you look at status code from `http://placehold.it/200x200`. the first time is `200 OK` the following are `304 Not Modified`. – bitWorking Mar 16 '13 at 15:49

2 Answers2

3

When user request 200x100 Pixel placeholder image, first time create with GD, Save and Output, next time if same size is requested, I am just reading the saved file and outputting to the browser instead creating new again.

If you can use Apache's mod_rewrite module, there's an even more efficient approach that doesn't need PHP at all once an image is created. See Create resized image cache but prevent abuse

Stolen from there:

HTML:

<img src="images/cache/200x150-picture_001.jpg" />

.htaccess code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ images/image.php?f=$1 [L]

This technique looks whether an image of the correct size/file name exists in the "cache" folder, and only if it doesn't, it calls your PHP script. Your script would have to parse the size info (like 200x100 as you say), resize, and output the image. The next time somebody requests the image, Apache will serve it statically, saving resources in the process.

The only downside to this is that the PHP script will be called only once, so if the original image changes, the change will never propagate to the resized version. You may need to occasionally wipe all resized images to prevent this, or delete all resized images if the original changes.

Also be sure to send caching info when outputting the image with PHP. See e.g. How to use HTTP cache headers with PHP

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • @Code this will work the next time if the same size is requested by *any* user. The file will already be there. – Pekka Mar 16 '13 at 16:11
0

Use readfile($filename). It's the best option for reading any file in php.

function output_rf($filename)
{
    header("Content-Type: image/png");
    readfile($filename);
}
Sos.
  • 914
  • 10
  • 14