So i found a script that does what I want but I'm having trouble "implementing" it. Basically I have all the image file names stored in my table and i want them to all display (the ones uploaded by the particular user logged in) on a page.

Of course I don't want them enormous, just small thumbnail images would do. I don't want them clickable or anything, i don't even want them to enlarge. But because of the whole portrait and landscape thing, i didn't want to hardcode the size in html. so, after searching i found this script:

function createThumb($source,$dest) {
$thumb_size = 150;
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];
   if($width> $height) {
$x = ceil(($width - $height) / 2 );
$width = $height;
   } elseif($height> $width) {
$y = ceil(($height - $width) / 2);
$height = $width;
   }
$new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
$im = imagecreatefromjpeg($source);
imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
imagejpeg($new_im,$dest,100);
}

here's how I'm calling the function:

<?php 
$path = "upload/$pic1";
 
echo 
"<img src=\"'".createThumb($path)."'\" />";
?>

(that's in a while statement)

i get a page full of wingdings and it says i'm also missing an argument. can anyone help?? the directions with this script really suck and don't explain anything.

thanks!

Recommended Answers

All 2 Replies

I see the problem. You can't call it in the img tag. That script creates a new file on the server, instead of returning an image. It doesn't return anything.

The function takes two arguments, the path to the image to change, and the path to store the changed image.

You need to put the path to the created image in the img tag.

Call the function, and then write the img tag on the next line. The src= should point to the same path as the changed image path.

what you could do is hardcore the the save directory into the function (as you have to save the image to the server, and this makes sense as you can then have it acting as a cache so that you only have to process the image once, rather than everytime the function is called which creates very high server load.) the create a file name dynamically.

function createThumb($source) {

$image_save_dir = "/imagecache/";
$image_filename = substr(md5(microtime()),0,$length)."jpg";
$thumb_size = 150;
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];
   if($width> $height) {
$x = ceil(($width - $height) / 2 );
$width = $height;
   } elseif($height> $width) {
$y = ceil(($height - $width) / 2);
$height = $width;
   }
$new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
$im = imagecreatefromjpeg($source);
imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
imagejpeg($new_im,$image_save_dir.$image_filename,100);
return $image_save_dir.$image_filename;
}

This functions returns the file location so you can call it dynamically from within you img tag.

I will leave how to do the caching up to you (You do learn anything by someone doing it all for you.

But as a simple hint, you could create the filename from and md5 has of the source link and use the first 5 charaters of the md5 hash. This will ensure that you generate the same filename everytime you are passed the same source link. Then check in your file cache directory to see if that filename exists if it does just return the link without processing the image again. Simple

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.