note 69168 added to function.imagejpeg
| From: | webmaster at jongliertreff dot de | Date: | Fri, 25 Aug 2006 11:56:39 +0000 |
| Subject: | note 69168 added to function.imagejpeg | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116307@lists.php.net to get a copy of this message | ||
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($bilddatei);
// 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.
----
Server IP: 217.13.201.10
Probable Submitter: 80.64.180.85
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.imagejpeg.php
Edit -- https://master.php.net/note/edit/69168
Del: integrated -- https://master.php.net/note/delete/69168/integrated
Del: useless -- https://master.php.net/note/delete/69168/useless
Del: bad code -- https://master.php.net/note/delete/69168/bad+code
Del: spam -- https://master.php.net/note/delete/69168/spam
Del: non-english -- https://master.php.net/note/delete/69168/non-english
Del: in docs -- https://master.php.net/note/delete/69168/in+docs
Del: other reasons-- https://master.php.net/note/delete/69168
Reject -- https://master.php.net/note/reject/69168
Search -- https://master.php.net/manage/user-notes.php