0
votes

Last week I converted my page img src values from pointing at image files to using a PHP script to serve up the images. The primary reason was to accommodate both files and database BLOBs as the actual source.

Now when a user goes to a page, sometimes images show and sometimes not. If not, and the page is refreshed\reloaded, then the image appears. When images do not appear, sometimes it is an image the user has already accessed previously today.

I am stumped.

Here is the img tag:

<img src="../somedir/image_script.php?i=1234">

The image_script.php file figures out where to get the image from, then finishes up with:

header("Content-type: image/jpeg");
if($from_db){
    print $image_blob;
} else {
    $im = imagecreatefromjpeg($image_file);
    imagejpeg($im,null,100);
    imagedestroy($im)
}

I am using PHP 5.2.8 on IIS 6 using FastCGI. There are no cache headers on the image_script.php file nor on the directory it is in. Currently 99.9% of the images are file based, so I do not know if there is a difference in result between db-based and file-based images. When I go directly to image_script.php in my browser it returns the requested image (i=????) 100% of the time.

a> Any clue as to why the hit and miss with images being displayed? and,

b> what would be a proper way to actually cache the images served up by the PHP script? (they are very static)

  • Scott
1

1 Answers

1
votes

Hmm. Can't tell for sure, but maybe your imagecreatefromjpeg is occasionally running out of memory? In that case, you'd serve an error message out as JPEG data and never see it, right?

Incidentally, wouldn't just grabbing the image file in as a string and shovelling it out without going through imagecreatefromjpeg/imagejpeg/imagedestroy be more efficient? It looks like you're reading a JPEG file, creating an internal PHP memory image from it, then reconverting it to a JPEG (at hefty 100% quality) then serving that data out, when you could simply read the JPEG file data in and print it, like you do from the database.

What happens if you do, say...

...
} else {
    header ('Content-length: ' .filesize($image_file));
    readfile ($image_file);
}