Monthly Archives: March 2011

Tools ‹ phpspecialist — WordPress

Tools ‹ phpspecialist — WordPress.

Advertisements

When viewing files online, it is often necessary (or at least convenient) to be able to create a thumbnail image on the fly. You may want to create previews of photos you are linking to, or perhaps resize photos for your website to a more user friendly size without having to bother with a graphics program.

In this tutorial we will explore how to do this using imagecopyresized () and using imagecopyresampled. We will also cover calling the image from the PHP file, as well as saving the image to your server.

1. 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);
?>

2.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);
?>