944,214 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 6599
  • PHP RSS
May 30th, 2007
0

Square Image Thumbnail

Expand Post »
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:

PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 2
Newbie Poster
jnscollier is offline Offline
12 posts
since Mar 2007
May 31st, 2007
0

Re: Square Image Thumbnail

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.
Reputation Points: 730
Solved Threads: 181
Nearly a Senior Poster
MidiMagic is offline Offline
3,314 posts
since Jan 2007
May 31st, 2007
0

Re: Square Image Thumbnail

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.

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 4
Light Poster
UrbanSky is offline Offline
42 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Creating an event or an action to a button.
Next Thread in PHP Forum Timeline: Scrolling display is blurred





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC