1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Creating Thumbnails with PHP using the GD Library


4 of 4

Using The Thumbnail Images
To use your thumbnail images you have two options: you can call the .php file directly, or you can save the new image you created.

If you are using the .php file directly, you must be sure that nothing else is sent to the browser from the PHP file (text for example), that would keep it from being read as an image. You can link it the same way you would link any other graphic:

<img src="http://www.my_site.com/my_thumbnail_program.php">
You can also save the image through the imagejpeg () function (or gif or png.) Below is an example of how you would do this:

//Name you want to save your file as $save = 'myfile.jpg';

$file = 'original.jpg'; echo "Creating file: $save"; $size = 0.45; header('Content-type: image/jpeg') ; list($width, $height) = getimagesize($file) ; $modwidth = $width * $size; $modheight = $height * $size; $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; // Here we are saving the .jpg, you can make this gif or png if you want //the file name is set above, and the quality is set to 100% imagejpeg($tn, $save, 100) ; ?>

This code saves the thumbnailed file onto your server as whatever you specified($save). You can then call this new graphic file directly, and do not need the PHP again once the image file has been created.
  1. About.com
  2. Technology
  3. PHP / MySQL
  4. Graphics- GD Library
  5. Create Thumbnail PHP - Thumbnail GD Library - GD Library PHP Thumbnail

©2014 About.com. All rights reserved.