Hi All
This is my first posting so be gentle!!
I am having trouble with using PHP and the GD lib to create (some) thumbnail images.
The background....
I have a site that admins upload images over a PHP interface which updates the MYSQL database.
The first person to request the image then makes the PHP script create a thumbnail for use on the site. This then gets saved to a thumbnail image folder.
The next time someone requests the image the script checks to see if the file /photo/thumbnail/whatever.jpg file exists if it does the script returns the filename to be included as the source for the image, if not then it goes through the create image bit.
The problem ...
Now this is working fine apart from the odd image here and there which get the thumbnails created and saved fine, however when the script goes back to serve the image then a PHP error occurs (not always the same one!). and just the placeholder appears.
If I delete the file and let the script create the image again the same happens.
If I open the image in the browser
http://mysite.com/photo/thumbnail/whatver.jpg
up pops the image thumbnail file.
If I copy another thumbnail to that file name then the thumbnail appears fine on the site(obviously not the picture I want!).
I have tried to think of everything causing this behaviour and I am now lost.
I don't think the code is at fault and I am coming round to the posibility that there was a problem with the original image as copying another known good original to the other original filename and deleting the thumbnails also results in success (obviously not the picture I want yet again).
However the original images all serve in a browser ok and if I output directly to the screen instead of saving the file to disk this appears ok.
Here is the basic code for the image create inlude file.
PHP Code:
<? include "common.php"; // Database connection etc.....
$pilot_id=$HTTP_GET_VARS['id']; // Get the pilot id for this image
$sql2="SELECT photo FROM tbl_pilots WHERE id=".$pilot_id;
$result2 = mysql_query($sql2);
if (mysql_num_rows($result2)>0) {
$row2 = mysql_fetch_object($result2);
$image = $row2->photo;
$cachefile = ("photos/pilots/thumbnails/".$image); // set the name of the cahcefile in the thumbnails directory
if (file_exists($cachefile)) { // if the page has been cached from an earlier request
include($cachefile); // output the contents of the cache file
exit; // exit the script, so that the rest is not executed
}
// If no cache file is found then get the image, resample, resize and output to the file in $cachefile
// This should only run once for each image unless the thumbnail is deleted.
if ($image == NULL)
{$image = "noimage.jpg";}
$imf = imagecreatefromjpeg ("photos/pilots/".$image);
$x= imagesx ($imf);
$y= imagesy ($imf);
$xyratio = $x/$y;
if ($x > $y)
{$sx = 75;
$sy = 75 / $xyratio;}
else
{$sy = 75;
$sx = 75 * $xyratio;}
$im= imagecreatetruecolor($sx, $sy);
imagecopyresampled ($im, $imf, 0, 0, 0, 0, $sx, $sy, $x, $y);
Header("Content-type: image/jpeg");
imagejpeg($im,$cachefile,100); // output the thumbnail at 100% quality
}
?>
If you have not fallen asleep already ;-)
If I change the above code to output to the browser and not to a file
imagejpeg($im,'',100); <<<< 2 single quotes not one double!!
Then the image appears fine however the CPU load starts to rise as the site is busy and without the caching then the site slows down during busy times.
So it is only when the routine reads back the saved image thumbnail that the error occurs.
Has anyone come accross this sort of behaviour before ?
If anyone wants to help in tracking this problem down then feel free.
I can point you to the live urls and play with the code to try and sort things out.
Thanks
Paul.