Jump to content

How Do I Create Thumbnail Images?


skyler_sdf

Recommended Posts

Building a basic thumbnail is a five stage process:

1. load the source image into a php variable

2. determinate the height and width of the original image

3. create a blank thumbnail image of the correct size

4. copy the original image to the blank thumbnail

5. display the thumbnail using the correct content type.

Let's create a thumbnail from a large version of the SitePoint logo in jpeg format


<?php

//specify source image

$sourceimage='sample_images/sitepoint_logo.jpg';

//Specify thumbnail height and width

$thumbwidht=200;

$thumbheight=100;

//load the source image

$original=imagecreatefromjpeg($sourceimage);

//get the size of the original

$dims=getimagesize($sourceimage);

//create a blank thumbnail (note slightly reduced height)

$thumb=imagecreatetruecolor($thumbwidth, $thumbheight);

//copy a resized version of the original onto the thumbnail

imagecopyresampled($thumb, $original, 0, 0, 0, 0, $thumbwidht, $thumbheight, $dims[0], $dims[1]);

//send the content header

header("Content-type: image/jpeg");

//display the image

imagejpeg($thumb);

?>

imagecreatefromjpeg=load an image from the file system into a PHP variable

The getimagesize function tells us the width and height of the image (more on getimagesize in a moment)

The imagecreatetruecolor function is used to create a blank image(in memory, as a PHP variable) into which the thumbnail image will be placed.

The imagecopyresampled function is the point at which the thumbnail is actually created from the original.It places a resized version of the image into the blank thumbnail image, resambling along the way to ensure that the image is resized smoothly.

We use imagejpeg to output the completed thumbanil.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.