Here's another on-the-fly thumbnail creation script.
When I scripted the pictuerviewer on my page, I had all the pictures only in full size and qualit, because I wanted the posibility für visitors to download the pictures.
But as Imagesizes of more than 4 MP are to large for websites, I created thumbnails and the smaller pictures on the fly. But I found out, that the Script needed too much RAM, especially in the thumbnail overview, when I had more then 50 thumbnails to create on the fly at the same time.
So I modified my image creator and my viewer to let them store images, that are created. So only the first visitor has to wait (which is usually me for controlling the uploads und updates), all other visitors get the stored images, which is much faster.
Create different folders. I have a main folder called 'imagesdb' and the tree subfolders full (Full quality images), show (images for the picture viewer) and thumb (for thumbnails in overview).
Store the script for example as image.php and link it like that:
<?PHP
$image_name = "foo.jpg";
$style = "show";
$image_name = "imagesdb/$style/$image_name";
if(!file_exists($image_name))
$image_name = "image.php?image_name=$image_name&style=$style";
?>
Now the main script, stored in the file image.php:
<?PHP
$image_name = $_GET['image_name'];
$style = $_GET['style'];
switch($style) {
case "show":
$max_size = 800;
break;
case "thumb":
$max_size = 125;
}
$dest_file = "imagesdb/$style/$image_name";
$image_file = "imagesdb/full/$image_name";
$size = getimagesize($image_file);
if($size[0] > $size[1]) {
$divisor = $size[0] / $max_size;
}
else {
$divisor = $size[1] / $max_size;
}
$new_width = $size[0] / $divisor;
$new_height = $size[1] / $divisor;
settype($new_width, 'integer');
settype($new_height, 'integer');
$image_big = imagecreatefromjpeg($image_file);
$image_small = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_small, $image_big, 0,0, 0,0, $new_width,$new_height, $size[0],$size[1]);
imagedestroy($image_big);
header("Content-type: image/jpeg");
if($style=="show" || $style=="thumb") {
if(!file_exists($dest_file))
imagejpeg($image_small, $dest_file, 100);
}
imagejpeg($image_small, '', 100);
imagedestroy($image_small);
?>
As this website helped me for several times in the past and for creating this script, I hope I can help others with this script saving the time for developing a much more performant solution than an allways-on-the-fly-creating script.