In the following script (obtained from a PHP class some months ago) I can take a jpeg image and create a new, smaller, image from it and save the new version in a folder. This uses the PHP GD2 library.
I have a question: How do I get rid of the new images when the visitor goes to another page - after all, they are going to start multiplying faster than rabbits? What happens when several visitors access the page and the image is constantly overwritten?
Although this script names a particular image and destination image, a PHP script will call an image by name from a table and create a destination thumbnail using the image's name.PHP Code:<?php
$image = file_get_contents("test.jpg"); // Get the .jpg image
$source = imagecreatefromstring($image); // Get the data about the image
$src_w = imagesx($source); // Get the width of the image
$src_h = imagesy($source); // Get the height of the image
if( $src_h > $src_w ){$dst_h = 400; // If the H is more than the W, the destination image's H will be 400px
$dst_w = ($src_w/$src_h)*$dst_h; // Resize the W in proportion.
}
else{
$dst_w = 400; // If the H is less than the W, the destination image's W will be 400px
$dst_h = ($src_h/$src_w)*$dst_w; // Resize the H in proportion.
}
$thumb = imagecreatetruecolor($dst_w, $dst_h);
imagecopyresampled($thumb,$source,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
imagejpeg($thumb, "newthumb2.jpg");
echo"<img src='newthumb2.jpg' alt=''>";
?>
Should I simply designate a folder to receive all the destination thumbs and not worry about it?





Bookmarks