Square Image Thumbnail

Reply

Join Date: Mar 2007
Posts: 12
Reputation: jnscollier is an unknown quantity at this point 
Solved Threads: 2
jnscollier's Avatar
jnscollier jnscollier is offline Offline
Newbie Poster

Square Image Thumbnail

 
0
  #1
May 30th, 2007
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:

  1. function createThumb($source,$dest) {
  2. $thumb_size = 150;
  3. $size = getimagesize($source);
  4. $width = $size[0];
  5. $height = $size[1];
  6. if($width> $height) {
  7. $x = ceil(($width - $height) / 2 );
  8. $width = $height;
  9. } elseif($height> $width) {
  10. $y = ceil(($height - $width) / 2);
  11. $height = $width;
  12. }
  13. $new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
  14. $im = imagecreatefromjpeg($source);
  15. imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
  16. imagejpeg($new_im,$dest,100);
  17. }

here's how I'm calling the function:

  1. <?php
  2. $path = "upload/$pic1";
  3.  
  4. echo
  5. "<img src=\"'".createThumb($path)."'\" />";
  6. ?>

(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!
Last edited by cscgal; May 31st, 2007 at 1:54 am. Reason: Excessive bbcode color removed
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 165
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: Square Image Thumbnail

 
0
  #2
May 31st, 2007
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.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 42
Reputation: UrbanSky is an unknown quantity at this point 
Solved Threads: 4
UrbanSky UrbanSky is offline Offline
Light Poster

Re: Square Image Thumbnail

 
0
  #3
May 31st, 2007
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.

  1. function createThumb($source) {
  2.  
  3. $image_save_dir = "/imagecache/";
  4. $image_filename = substr(md5(microtime()),0,$length)."jpg";
  5. $thumb_size = 150;
  6. $size = getimagesize($source);
  7. $width = $size[0];
  8. $height = $size[1];
  9. if($width> $height) {
  10. $x = ceil(($width - $height) / 2 );
  11. $width = $height;
  12. } elseif($height> $width) {
  13. $y = ceil(($height - $width) / 2);
  14. $height = $width;
  15. }
  16. $new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
  17. $im = imagecreatefromjpeg($source);
  18. imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
  19. imagejpeg($new_im,$image_save_dir.$image_filename,100);
  20. return $image_save_dir.$image_filename;
  21. }

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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC