PHP / MySQL

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

Creating Thumbnails with PHP using the GD Library

By Angela Bradley, About.com

3 of 4

Using Imagecopyresampled ()

If you need a good quality reduction, and imagecopyresized () doesn't look nice enough, you may have better luck with imagecopyresampled (). The parameters you need to define in imagecopyresampled () 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 use imagecopyresampled (). If you are having trouble understanding, be sure to pay attention to the comments in the code, they explain everything that is going on in each step.

<?php
// The file you are resizing
$file = 'yourfile.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;

// Resizing the Image
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);

// Outputting a .jpg, you can make this gif or png if you want
//notice we set the quality (third value) to 100
imagejpeg($tn, null, 100);
?>

Explore PHP / MySQL

About.com Special Features

PHP / MySQL

  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.