View Full Version : thumbnail from image in db

09-03-2007, 01:59 AM
i was looking for some php code that resizes images effectively creating a thumbnail without resaving and creating a new file. i found this but the problem is that it apparently only works with local urls linking to images files. my images are stored in a mysql database and are retrieved with another php file called "getdata.php?id=x". this php file does work and i can view the original image fine. when i link to http:/mysite.com/pathto/thumbnail.php?url=getdata.php?id=x it gives me this error:

Warning: getimagesize(getdata.php?id=2) [function.getimagesize]: failed to open stream: No such file or directory in /home/public_html/path/thumbnail.php on line 7

this is code for thumbnail.php:

// Get image info from variable
$url = $_GET['url'];
$img = getimagesize($url) or die();//line 7

// Checks if URL is image
if ( $img[2] == 1 ) {
$pic = imagecreatefromgif( $url ) or die();
} elseif ( $img[2] == 2 ) {
$pic = imagecreatefromjpeg( $url ) or die();
} elseif ( $img[2] ==3 ) {
$pic = imagecreatefrompng( $url ) or die();
} else {exit();}

// If an image is found and we can determine that it is an image
if ($pic) {
// Get width and height from the image
$width = imagesx($pic);
$height = imagesy($pic);
$theight = 100;

// Calculate the new width
$twidth = ($theight * $width) / $height;

// Create new image
$thumb = @imagecreatetruecolor ( $twidth, $theight )
or die ("Can't create Image!");

// Resize the image into a thumb
imagecopyresized($thumb, $pic, 0, 0, 0, 0,
$twidth, $theight, $width, $height);

// Change page type to a jpeg
header ("Content-type: image/jpeg");

// Create jpeg image from thumbnail

Len Whistler
09-03-2007, 04:16 AM
If you upload your images and have control over them then this PHP script might be useful. It generates images from the embedded thumbnails.

$file = "test.jpg";

$image = exif_thumbnail($file, $width, $height, $type);

if ($image!==false) {
header('Content-type: ' .image_type_to_mime_type($type));
echo $image;
} else {
// no thumbnail available, handle the error here
echo 'No thumbnail available';