Voting

: min(nine, seven)?
(Example: nine)

The Note You're Voting On

webmaster at jongliertreff dot de
18 years ago
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";
// I've taken the foldernames. It's easier. For the
//thumbnails replace "show" with "thumb".
$image_name = "imagesdb/$style/$image_name";
if(!
file_exists($image_name))
$image_name = "image.php?image_name=$image_name&style=$style";
// only if file doesn't exist call the on-the-fly creating file
?>

Now the main script, stored in the file image.php:

<?PHP

$image_name
= $_GET['image_name'];
$style = $_GET['style'];

// Now set the maximum sizes to the different styles.
// You may set additional styles, but remember to
// create the according subfolders.

switch($style) {
case
"show":
$max_size = 800;
break;
case
"thumb":
$max_size = 125;
}

$dest_file = "imagesdb/$style/$image_name";
// set output file
$image_file = "imagesdb/full/$image_name";
// set source file
$size = getimagesize($image_file);
// get original size

if($size[0] > $size[1]) {
$divisor = $size[0] / $max_size;
}
else {
$divisor = $size[1] / $max_size;
}
// to get allways pictures of the same size, which ist
// mostly wanted in imageviewers, look what ist larger:
// width or height

$new_width = $size[0] / $divisor;
$new_height = $size[1] / $divisor;
// set new sizes

settype($new_width, 'integer');
settype($new_height, 'integer');
// sizes should be integers

$image_big = imagecreatefromjpeg($image_file);
// load original image
$image_small = imagecreatetruecolor($new_width, $new_height);
// create new image
imagecopyresampled($image_small, $image_big, 0,0, 0,0, $new_width,$new_height, $size[0],$size[1]);
// imageresampled whill result in a much higher quality
// than imageresized
imagedestroy($image_big);
// the original data are no longer used

header("Content-type: image/jpeg");

if(
$style=="show" || $style=="thumb") {
if(!
file_exists($dest_file))
imagejpeg($image_small, $dest_file, 100);
}
// if you have set additional sizese put them in the
// if-arguments, too.
// if someone calls the image.php directly in the
// browser with imagenames allready existing, they
// won't be overwritten

imagejpeg($image_small, '', 100);
imagedestroy($image_small);
// finally send image to browser and destroy no longer
// needed data.

?>

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.

<< Back to user notes page

To Top