Problem creating thumbnails
[email protected] (Quinn) Sun, 20 Jul 2003 20:16:10 +0200
| Newsgroups | php.lang |
|---|---|
| Message-ID | <[email protected]> |
I've created a gallery section of my web site. The index page looks to see
if there are any sub-directories and, if there are, looks inside for Jpegs.
It then creates thumbnails on the fly from the jpegs and displays then in
tables, suitably linked.
Here's the problem. The whole shebang works fine on my local system,
whether running under WinXP or Linux. My hosting provider says that all
necessary GD libraries are enabled on the host system, so in theory it
should work. But it doesn't. What happens is that the browser throws up
image placeholders (because it comes across the <IMG SRC... tag) but then
these disappear as whatever is coming back from my thumbnail routine is
obviously empty. So before I start whining at my hosting company, I'd
appreciate any help people could give.
Here's the relevant code - I've included quite a lot of it, so sorry about
the long post.
The index.php page includes a function that builds the table incorporating
the thumbnails. This has been passed the parameter $sourcedir which
contains the name of a subdirectory under the current one, plus "/". The
function looks at the files in this subdir and identifies Jpeg files using
getimagesize(). It puts the filename into $file. The next important line
is:
echo "<IMG SRC='thumbnail.php?img=".$sourcedir.$file."'>";
When I view the output HTML (on both local and hosted versions), the output
from this line is (eg):
<img SRC='thumbnail.php?img=new/cherries-01.JPG'>
In the same directory as the index.php file is thumbnail.php. This takes
the jpeg image, makes a new smaller version and streams it to index.php.
The code for this is:
<?php
header("Content-type: image/jpeg");
$sourcefile = stripslashes($_GET['img']); // get filename passed via GET
$maxdim = 100 ; // set the maximum dimension for the thumnail
$sourcedata = getimagesize($sourcefile);
/* now determine the dimensions of the new image
- there's probably a more elegant way to do this */
if ( $sourcedata[0] > $sourcedata[1] ) { // if width greater than height
$newwidth = $maxdim;
$newheight = $sourcedata[1] / ($sourcedata[0]/$maxdim) ;
} else {
$newheight = $maxdim ;
$newwidth = $sourcedata[0] / ($sourcedata[1]/$maxdim) ;
}
SWITCH ($sourcedata[2]) {
// create resource from source image
CASE 2:
$imagesrc = @imagecreatefromjpeg($sourcefile);
break;
CASE 3:
$imagesrc = @imagecreatefrompng($sourcefile);
break;
}
/* create resource for a new, blank image */
$thumbnail = imagecreatetruecolor($newwidth, $newheight);
/* now we'll copy one image to the other, resampling as we do */
imagecopyresampled($thumbnail,$imagesrc,0,0,0,0,$newwidth,$newheight,$sourc
edata[0],$sourcedata[1]);
/* the resource $thumbnail now contains the new image, so all we need do
is output it */
imagejpeg($thumbnail);
?>
Like I say, this all works fine on my local system, so I'm baffled. All
ideas gratefully received.