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

Creating Thumbnails with PHP using the GD Library

By Angela Bradley, About.com

2 of 4

Using Imagecopyresized ()

When using imagecopyresized () we have a number of parameters that need to be entered. They are: destination_image, source_image, starting_X_ordinate, starting_Y_ordinate, initial_X_image_startpoint, initial_Y_image_startpoint, width, height, original_width, original_height.

Here is an example of how to resize an image to a certain % of it's original size, and keep it proportional. Pay attention to the comments in the code, as they explain what we are doing in each step.

<?php
// The file you are resizing
$file = 'your.jpg';

//This will set our output to 45% of the original size
$size = 0.45;

// This sets it to a .jpg, but you can change this to png or gif
header('Content-type: image/jpeg');

// Setting the resize parameters
list($width, $height) = getimagesize($file);
$modwidth = $width * $size;
$modheight = $height * $size;

// Creating the Canvas
$tn= imagecreatetruecolor($modwidth, $modheight);
$source = imagecreatefromjpeg($file);

// Resizing our image to fit the canvas
imagecopyresized($tn, $source, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);

// Outputs a jpg image, you could change this to gif or png if needed
imagejpeg($tn);
?>
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.