[FX.php List] Creating thumbnail for TIFF uploads on the fly

Troy Meyers tcmeyers at troymeyers.com
Sun Sep 30 15:51:38 MDT 2007


I've set up a PHP page where customers can upload photos. On the confirmation page following the upload I show a thumbnail-sized version of the photo and some size and type statistics.

This all works fine for JPGs but I want to do the same for TIFFs... the TIFFs work fine except I don't know the method to get a TIFF loaded into a PHP image object so that the thumbnail can be generated. Here's the code that I'm using, which was customized from a few examples I found on the web:

Filename: thumb.php (used in pages as if an image file, with file name info passed as GETs)

<?php

// function to replace plain imagecopyresampled() for better performance
// courtesy of Tim Eckel at leethost dot com

function fastimagecopyresampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 3) {
  // Plug-and-Play fastimagecopyresampled function replaces much slower imagecopyresampled.
  // Just include this function and change all "imagecopyresampled" references to "fastimagecopyresampled".
  // Typically from 30 to 60 times faster when reducing high resolution images down to thumbnail size using the default quality setting.
  // Author: Tim Eckel - Date: 09/07/07 - Version: 1.1 - Project: FreeRingers.net - Freely distributable - These comments must remain.
  //
  // Optional "quality" parameter (defaults is 3). Fractional values are allowed, for example 1.5. Must be greater than zero.
  // Between 0 and 1 = Fast, but mosaic results, closer to 0 increases the mosaic effect.
  // 1 = Up to 350 times faster. Poor results, looks very similar to imagecopyresized.
  // 2 = Up to 95 times faster.  Images appear a little sharp, some prefer this over a quality of 3.
  // 3 = Up to 60 times faster.  Will give high quality smooth results very close to imagecopyresampled, just faster.
  // 4 = Up to 25 times faster.  Almost identical to imagecopyresampled for most images.
  // 5 = No speedup. Just uses imagecopyresampled, no advantage over imagecopyresampled.

  if (empty($src_image) || empty($dst_image) || $quality <= 0) { return false; }
  if ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
    $temp = imagecreatetruecolor ($dst_w * $quality + 1, $dst_h * $quality + 1);
    imagecopyresized ($temp, $src_image, 0, 0, $src_x, $src_y, $dst_w * $quality + 1, $dst_h * $quality + 1, $src_w, $src_h);
    imagecopyresampled ($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $dst_w * $quality, $dst_h * $quality);
    imagedestroy ($temp);
  } else imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
  return true;
}


// The source file and display name
$source_filename = $_GET['id'].'.bin';
$users_filename = $_GET['name'];

// Set a maximum height and width of the thumbnail version
$width = 120;
$height = 100;

// Content type
header('Content-type: image/jpeg');
header('content-disposition: inline; filename="thumbnail of '.$users_filename.'"');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($source_filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($source_filename);
// the next, fastimagecopyresampled(), could be the native PHP imagecopyresampled() but this should be better
fastimagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);

?>

(end of file)


The thing I need is something to replace the line:
$image = imagecreatefromjpeg($source_filename);
...that will read the TIFF file content into the $image variable.

It'd be called "imagecreatefromtif" if it were built into PHP, I expect, but I can't see one. Any suggestions?

-Troy


More information about the FX.php_List mailing list