PHP - Resize an Image with GD


This script requires the GD extension for php. It resizes an image on the fly, maintaining aspect ratio, to a new image of maximum width and height, $output_width and $output_height. This could be used to shrink an image and output a thumbnail on the fly, or blow up an image and display it on the fly.

It accepts PNGs GIFs, and JPGs using GD functions. The die_default_image() function, is a way of erroring out by displaying a blank 1x1 transparent gif. If the page calling the script is trying to display an image, it will merely display a blank image in case of error.

php_image.php
<?php
    $output_width =600;
    $output_height=400;
 
    $path = ( (isset($_REQUEST['path']))? $_REQUEST['path'] : "");
    $size_arr = getimagesize($path);
    if ($size_arr[2]==IMAGETYPE_GIF)
        $image = imagecreatefromgif($path);
    else if ($size_arr[2]==IMAGETYPE_JPEG)
        $image = imagecreatefromjpeg($path);
    else if ($size_arr[2]==IMAGETYPE_PNG)
        $image = imagecreatefrompng($path);
    else
        die_default_image();
 
    $tmpname = tempnam( sys_get_temp_dir() , "phptmp");
 
    resize_image($tmpname, $image, $size_arr, $output_width, $output_height);
 
    header('Content-Type: image/jpg');
    header('Content-Disposition: inline; filename="'.basename($path).'"');
    echo file_get_contents( $tmpname );
    unlink( $tmpname );
    die('');
 
 
function die_default_image()
{
    //43byte 1x1 transparent pixel gif
    header("content-type: image/gif");
    echo base64_decode("R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==");
}
 
function resize_image($thumbname, $image, $size_arr, $max_width, $max_height)//maintain aspect ratio
{
    $width  = $max_width;
    $height = $max_height;
    list($width_orig, $height_orig, $img_type) = $size_arr;
    $ratio_orig = $width_orig/$height_orig;
 
    if ($width/$height > $ratio_orig) {
       $width = floor($height*$ratio_orig);
    } else {
       $height = floor($width/$ratio_orig);
    }
 
    // Resample
    $tempimg = imagecreatetruecolor($width, $height);
    imagecopyresampled($tempimg, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
    imagejpeg($tempimg, $thumbname, 80);
}
 
if ( !function_exists('sys_get_temp_dir')) {
  function sys_get_temp_dir() {
    if (!empty($_ENV['TMP'])) { return realpath($_ENV['TMP']); }
    if (!empty($_ENV['TMPDIR'])) { return realpath( $_ENV['TMPDIR']); }
    if (!empty($_ENV['TEMP'])) { return realpath( $_ENV['TEMP']); }
    $tempfile=tempnam(uniqid(rand(),TRUE),'');
    if (file_exists($tempfile)) {
    unlink($tempfile);
    return realpath(dirname($tempfile));
    }
  }
}
?>

Usage:
<img src='php_image.php?path=http://www.google.com/intl/en_ALL/images/logo.gif' width=600 height=400>
or
<img src='php_image.php?path=myimage.gif' width=600 height=400>



code snippets are licensed under Creative Commons CC-By-SA 3.0 (unless otherwise specified)

Andy Gee on 2010-06-07 15:09:55
If you want your image to be centered on a specified output size (let say 300w x 500h)
Try this:
function resize_to_canvas($filename,$canvas_w=3300,$canvas_h=5100)
{
list($width, $height) = getimagesize($filename);

$original_overcanvas_w = $width/$canvas_w;
$original_overcanvas_h = $height/$canvas_h;

$dst_w = round($width/max($original_overcanvas_w,$original_overcanvas_h),0);
$dst_h = round($height/max($original_overcanvas_w,$original_overcanvas_h),0);

$dst_image = imagecreatetruecolor($canvas_w, $canvas_h);
$background = imagecolorallocate($dst_image, 255, 255, 255);
imagefill($dst_image, 0, 0, $background);

$src_image = imagecreatefromjpeg($filename);
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $width, $height);

header('Content-type: image/gif');
imagegif($dst_image);
}