cancel
Showing results for 
Search instead for 
Did you mean: 

PHP: Dynamically Creating Thumbnails

logical
Grafter
Posts: 28
Registered: ‎12-08-2007

PHP: Dynamically Creating Thumbnails

Hello there,
I would like to dynamically create some image thumbnails. I have written a php file, getThumbnail.php, that I call during every iteration of a for loop. However, the unexpected result is thus:
all the thumbnails are the same, they are all of the last image in the directory.
Other than that, the result is as expected.
Does anyone know why this happens? How can I fix it?
Thanks in advance,
Logical
Code:
index.php:

for($i = 1; intval($i) <= intval($total); $i = intval($i) + 1)
{
$photo = $album . $i;
$filename = $photo . '.jpg';
$image = './' . $album . '/' . $filename;
$_SESSION['image'] = $image;
<img src="getThumbnail.php" />
}

getThumbnail.php:

session_start();
$file = $_SESSION['image'];
$size = 0.45;
list($width, $height) = getimagesize($file);
$modwidth = 80;
$modheight = 80;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, null, 100);
2 REPLIES 2
Peter_Vaughan
Grafter
Posts: 14,469
Registered: ‎30-07-2007

Re: PHP: Dynamically Creating Thumbnails

You don't need the intval in the for loop.
Do you have a session_start() in index.php?
I'm note sure what you are trying to do by having HTML code (<img src="getThumbnail.php">) within the php section as that will not work.
I would pass the filename as a parameter rather than using $_SESSION[] as its likely the $_SESSION[] has not been saved to disk before it is used within getThumbnail.php
index.php

for($i = 1; $i <= $total; $i += 1)
{
$photo = $album . $i;
$filename = $photo . '.jpg';
$image = './' . $album . '/' . $filename;
?>
<img src="getThumbnail.php?<?php echo $image ?>" /><br>
<?php
}

getThumbnail.php

$file = $_GET['file'];
$size = 0.45;
list($width, $height) = getimagesize($file);
$modwidth = 80;
$modheight = 80;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, null, 100);

logical
Grafter
Posts: 28
Registered: ‎12-08-2007

Re: PHP: Dynamically Creating Thumbnails

Thanks. It all works fine.