PHP Scripts

Create a thumbnail from a larger image

This assumes your Internet provider has not only PHP enabled but al least the GD libraries compiled within it.
Two files are involved, the thumbnail script, and the html page that uses the generated thumb.

The final Image is called as <img src= in normal html, using the string created by thumbnail.php?im=image.jpg,
as shown in anypage.htm at the bottom.

thumbnail.php

<?php
// useage is thumbnail.php?im=imagename.jpg
// set for 120 px thumb
Header("Content-type: image/jpeg");
$orig_image = imagecreatefromjpeg($im);
list($width, $height, $type, $attr) = getimagesize($im);
if ($width > 120) {
$ratio = 120 / $width;
$newheight = $ratio * $height; }
else $newheight = $height;
$sm_image = imagecreatetruecolor(120,$newheight) or die ("Cannot Initialize new gd image stream");;
Imagecopyresampled($sm_image,$orig_image,0,0,0,0,120,$newheight,imagesx($orig_image),imagesy($orig_image));
imageJPEG($sm_image);
imagedestroy($sm_image);
imageDestroy($orig_image);
?>


anypage.htm:

<html>
And the thumbnail to see is here:

<?php
$t="thumbnail.php?im=";
// then using image 36022.jpg as an example

$f="36022.jpg";
$file = $t . $f;
$v='<img src=$file';
?>

<img src="<?php echo $file; ?>" alt="Generating thumb">
</html>

 

Demo set for 65 pixels width

Back