1. Home
  2. Computing & Technology
  3. PHP / MySQL

Creating Thumbnails with PHP using the GD Library

By , About.com Guide

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:
<?php

//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.
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Graphics- GD Library
  5. Create Thumbnail PHP - Thumbnail GD Library - GD Library PHP Thumbnail

©2009 About.com, a part of The New York Times Company.

All rights reserved.