Generate GIF, JPG, and PNG thumbnails on the fly

If you need to quickly generate thumbnails with PHP you can use this code.

<?php

// create and return a thumbnail version of an image

// set up height and width of the created thmbnail
if (isset($_GET['width'])) {
    $newWidth = $_GET['width'];
} else {
    $newWidth = 100;
}

if (isset($_GET['height'])) {
    $newHeight = $_GET['height'];
} else {
    $newHeight = 100;
}

if (isset($_GET['quality'])) {
    $quality = intval($_GET['quality']);
    if ($quality > 100) $quality = 100;
    if ($quality < 1) $quality = 1;
} else {
    $quality = 80;
}

// read in the passed image
if(substr($src, strlen($src)-3,3)=="gif" || substr($src, strlen($src)-3,3)=="GIF"){
    $im = imagecreatefromgif("$src"); // Attempt to open
}
if(substr($src, strlen($src)-3,3)=="jpg" || substr($src, strlen($src)-3,3)=="JPG" || substr($src, strlen($src)-3,3)=="peg" || substr($src, strlen($src)-3,3)=="PEG"){
    $im = imagecreatefromjpeg ("$src"); // Attempt to open
}
if(substr($src, strlen($src)-3,3)=="png" || substr($src, strlen($src)-3,3)=="PNG"){
    $im = imagecreatefrompng ("$src"); // Attempt to open
}

if (isset($_GET['mode'])) {
    $mode = $_GET['mode'];
    // only allow two modes (fixed and proportional)
    // and set the default to proportional if an unknown mode is set or no incoming
    // setting is detected
    if ($mode != 'proportional' && $mode != 'fixed') {
        $mode = 'proportional';
    }
} else {
    $mode = 'proportional';
}

if ($mode == 'proportional') {
    // Create thumbnails constraining their MAXIMUM size and retaining proportions

    //obtain the original image Height and Width
    $srcWidth = imagesx($im);
    $srcHeight = imagesy($im);

    // start by using width for calculations
    $percentage = (double)$newWidth / $srcWidth;
    $destHeight = round($srcHeight * $percentage);
    $destWidth = round($srcWidth * $percentage);

    if($destHeight > $newHeight){
        // if the width produces a height bigger than we want, calculate based on height
        $percentage =(double)$newHeight / $srcHeight;
        $destHeight = round($srcHeight * $percentage);
        $destWidth = round($srcWidth * $percentage);
    }
} else {
    $destHeight = $newHeight;
    $destWidth  = $newWidth;
}

// create a blank image of the required size
$new = imagecreatetruecolor ($destWidth, $destHeight);

// copy a resied version of the original to the new image
imagecopyresampled($new,$im,0,0,0,0,$destWidth,$destHeight, imagesx($im),imagesy($im));

// output header and file
header ("content-type: image/jpeg");
imagejpeg ($new, NULL, $quality);

// clear the image buffers
imagedestroy ($im);
imagedestroy ($new);
?>

Save it to a file named thumbnail.php (or whatever you like) and use it like this:

// Absolute path to image file
$src     = $_SERVER['DOCUMENT_ROOT'] . 'image.jpg';
$width   = 50;
$height  = 50;
$quality = 100;

$params = "src={$src}&width={$width}&height={$height}&quality={$quality}";
echo '<img src="thumbnail.php?' . $params . '" alt=""/>';

// Or you can omit some params and use the defaults
$params = "src={$src}";
echo '<img src="thumbnail.php?' . $params . '" alt=""/>';

Note that this just opens whatever file you pass to $src (which is a very bad idea). To prevent this security flaw you could hardcode the path inside the thumbnail.php file and just pass a filename via $src.

Leave a comment
Name
Email (optional)
Your email will only be used to show your Gravatar and to send you replies to this article (if you opt to receive them). You won't get ANY other messages at all!
Comment
No HTML allowed
Notify me via email
 
 
Share

Valid HTML 4.01 Strict