Home > Back End, PHP > Easy Thumbnails with PHP

Easy Thumbnails with PHP

July 15th, 2009



Don’t hassle with image processing software if you don’t have to.  This post will show you how to automate your thumbnail making with PHP.

What You’ll Need

Before you can get started you’ll need to make sure your server has the GD library installed.  If you’re not sure if it is, just create a PHP file, and place <?php phpinfo(); ?> in it and save it to your server.  When you navigate to the file it will list all the important stuff about your PHP.  Look for a GD section, with the GD support enabled, and we require a version higher than 2.0.

 

image

 

Getting Started

First lets look at how we want to use this script.  We want to be able to just place a url to our PHP file in the src attribute of an HTML img tag as if our PHP script was the image.

1
<img src=”thumbnail.php?image=thumbnail.jpg&width=100/>

Now that we see what we want to make, lets get started.  First we are going to make a function that handles taking the source image and turning it into a thumbnail.

To break this down a little, first we figure out the width and height of the source image.  We need to know this so when we resize the source image we can keep the proportions in tact.

We then figured out what the thumbnail height would be based on the width given and the proportions of the original image.

We then create a blank that will eventually be our thumbnail.

Copy and resize the source image data into the blank image.

Then display your brand spanking new thumbnail.

 

Here is the PHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function makeThumb($src,$newWidth) {
    // read the source image given
    $srcImage = imagecreatefromjpeg($src);
    $width = imagesx($srcImage);
    $height = imagesy($srcImage);
   
    // find the height of the thumb based on the width given
    $newHeight = floor($height*($newWidth/$width));
   
    // create a new blank image
    $newImage = imagecreatetruecolor($newWidth,$newHeight);
   
     // copy source image to a thumb at the new size
     imagecopyresized($newImage,$srcImage,0,0,0,0,$newWidth,$newHeight,$width,$height);
     
     // create the thumbnail
     imagejpeg($newImage);
}

 

Getting URL Variables

All the heavy lifting is done, now we just need to read the variables in the URL and place them in the function we just made. We also want to add a header to let your browser know that this PHP file will be outputting a jpeg.

Here is the PHP:

1
2
3
4
5
6
7
$imageSrc = (string)$_GET['image'];    
$width = $_GET['width'];

if (is_numeric($width) && isset($imageSrc)){
    header('Content-type: image/jpeg');
    makeThumb($imageSrc, $width);
}

You could probably do a more in depth check to see if the source image given is actually an image, but for now we’ll just check to make sure there is a value there.

 

Bring it All Together

That’s all there is to it. Here is all of the PHP together.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php

$imageSrc = (string)$_GET['image'];    
$width = $_GET['width'];

if (is_numeric($width) && isset($imageSrc)){
    header('Content-type: image/jpeg');
    makeThumb($imageSrc, $width);
}

function makeThumb($src,$newWidth) {

    $srcImage = imagecreatefromjpeg($src);
    $width = imagesx($srcImage);
    $height = imagesy($srcImage);
   
    $newHeight = floor($height*($newWidth/$width));
   
    $newImage = imagecreatetruecolor($newWidth,$newHeight);
   
     imagecopyresized($newImage,$srcImage,0,0,0,0,$newWidth,$newHeight,$width,$height);

     imagejpeg($newImage);
}
?>

In this Demo, place any image in the URL and it will be converted to a thumbnail.

Download Files

  • RSS
  • Print this article!
  • Digg
  • del.icio.us
  • DZone
  • Facebook
  • Mixx
  • Google Bookmarks
  • Design Float
  • Reddit
  • StumbleUpon
  • Technorati
  • Live
  • TwitThis
Author: Shawn
Categories: Back End, PHP



Top