Generate Thumbnail images on the fly.

Reply

Join Date: Aug 2003
Posts: 82
Reputation: himerus is an unknown quantity at this point 
Solved Threads: 0
himerus's Avatar
himerus himerus is offline Offline
Junior Poster in Training

Generate Thumbnail images on the fly.

 
0
  #1
Nov 16th, 2003
This script will generate a thumbnail image of any JPEG image that is sent to it...
This file is independant, and mine is named resize.php you can send an image to the php file, and then return the thumbnail for viewing. It does require some work, but WELL worth the effort once you've mastered the script an example of this script can be seen here http://www.passionstandardpoodles.com/photos/

In this example, the resize is transparent to the user, because the script on the display page resizes the image if it isn't there, and then displays the saved image, and not the resized one. otherwise, on a right click and properties on a generated image, the image name would be
  1. whateverdomain.com/php_scripts/resize.php?main_file=myphoto.jpg

NOTE: This script requires the GD image library to be compiled, and to my knowledge only works on a *nix server. PHP must also be installed using GD support.

The following elements are required for compiling PHP with GD support: (PHP 4.2.2 was current release last time I used this)

  1. --with-gd=/your/path \
  2. --with-jpeg-dir=/your/path \
  3. --with-png-dir=/your/path \
  4. --with-freetype-dir=/your/path \
  5. --with-zlib-dir=/your/path

[php]
$image = "$path_to_images";
$savelocation = "$path_to_thumbnail_dir";
if(!file_exists("$path_to_images"))
{
$oldumask = umask(0);
mkdir("$path_to_thumbnail_dir", 0777);
umask($oldumask);
}
if (!$max_width)
$max_width = 125;
if (!$max_height)
$max_height = 125;
$size = GetImageSize($image);
$width = $size[0];
$height = $size[1];
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if ( ($width <= $max_width) && ($height <= $max_height) ) {
$tn_width = $width;
$tn_height = $height;
}
else if (($x_ratio * $height) < $max_height) {
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}
else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
$src = ImageCreateFromJpeg($image);
$dst = ImageCreate($tn_width,$tn_height);
ImageCopyResized($dst, $src, 0, 0, 0, 0,
$tn_width,$tn_height,$width,$height);
ImageJpeg($dst,"$savelocation"."thumb_$file");
ImageDestroy($src);
ImageDestroy($dst);

[/php]

NOTE: To save a few hours of headache, there MUST NOT be ANY whitespace in this file before the variables are reached. so, do not put any empty lines at the start of the file!

I will be creating a tutorial on this soon, but this should be enough to get you going. There is a lot to making the GD work properly, and I really only recommend using it if you are setting up a large image gallery, and don't want to spend hours creating thumbnails and having to upload them all.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,036
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: Generate Thumbnail images on the fly.

 
0
  #2
Nov 17th, 2003
I'm looking forward to your tutorial! Is GD built-into PHP or is it more of an extension?
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 82
Reputation: himerus is an unknown quantity at this point 
Solved Threads: 0
himerus's Avatar
himerus himerus is offline Offline
Junior Poster in Training

Re: Generate Thumbnail images on the fly.

 
0
  #3
Nov 17th, 2003
Since PHP version 4.3.0, GD has been built into PHP. Prior to that, it had to be separately installed, and PHP compiled with the paramaters I posted above.

I haven't had the chance to play with the new version of PHP, so I haven't had the luxury of seeing if it works properly out of the box. It appears from the changelog, that there have been TONS of bug fixes related to the GD system.

I know that in the past, I have always had a HELL of a time getting the GD install / PHP compile to work properly. usually took several times compiling PHP to get it right with all the libraries that have to be installed, and pointed to properly. It was simply a pain to set up.

I'm excited to see how the new setup works, and I'll base my tutorial off of that.
Reply With Quote Quick reply to this message  
Join Date: Nov 2003
Posts: 781
Reputation: Zachery has a spectacular aura about Zachery has a spectacular aura about 
Solved Threads: 21
Team Colleague
Zachery's Avatar
Zachery Zachery is offline Offline
The Geek Father

Re: Generate Thumbnail images on the fly.

 
0
  #4
Nov 17th, 2003
woudlnt this become server intensive over time if you had a high trafic site?
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 82
Reputation: himerus is an unknown quantity at this point 
Solved Threads: 0
himerus's Avatar
himerus himerus is offline Offline
Junior Poster in Training

Re: Generate Thumbnail images on the fly.

 
0
  #5
Nov 17th, 2003
It would ABSOLUTELY bog down a server...

I've used it before where it generated a thumbnail each time the thumbnail was called for, but for the obvious reasons, this is NOT a good idea.

The script above when called will save a copy of the thumbnail image that GD generated. Then, using an IF/ELSE command, you can always check to see if the thumbnail exists, and if not, create it...

The last setup I used this with, I created a control script for uploading images, and two versions of this resize script.

The user uploads an image of ANY size, and the script resizes it first into a decent size for display on a full page... let's just say 640x480 pixels... .then the script runs again with new paramaters for the thumbnail, and creates an images 100x100, or whatever it was set to.

Then, the photo gallery scripts only call images that are alredy stored on the file system.

I also used it in an application once where it would take a full uploaded directory of photos, and scan the directory image by image, and create the thumbs for displaying.

I will definitely have to work on a good tutorial on this one, because like you said, if not used properly, can SEVERELY affect performance of the server.
Reply With Quote Quick reply to this message  
Join Date: Nov 2003
Posts: 781
Reputation: Zachery has a spectacular aura about Zachery has a spectacular aura about 
Solved Threads: 21
Team Colleague
Zachery's Avatar
Zachery Zachery is offline Offline
The Geek Father

Re: Generate Thumbnail images on the fly.

 
0
  #6
Nov 18th, 2003
why not use mysql or another type of DB to store a generate images, so that theyre only generated once
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 82
Reputation: himerus is an unknown quantity at this point 
Solved Threads: 0
himerus's Avatar
himerus himerus is offline Offline
Junior Poster in Training

Re: Generate Thumbnail images on the fly.

 
0
  #7
Nov 18th, 2003
They are actually already only generated once.

With the $savelocation variable set to the location that the thumbnails are to be stored.

The function ImageJpeg is sending the created thumbnail to the directory specified to be saved, and used at another time.

The first segment of code posted above "whateverdomain.com/php_scripts/resize.php?main_file=myphoto.jpg" would indeed call for the image thumbnail to be re-created each time, but since I originally wrote that, I designed the system to use something that basically checked to make sure the thumbnail exsisted, and if not, it would create it, save it, and call the saved image, and after that instance, that thumbnail would not need to be created again.

As I look over that code above, there are a few details that were customized from the original "bare" script. I'll have to make a few adjustments, and I'll put together the full example with all files included and some decently commented code.

Reply With Quote Quick reply to this message  
Join Date: Nov 2003
Posts: 781
Reputation: Zachery has a spectacular aura about Zachery has a spectacular aura about 
Solved Threads: 21
Team Colleague
Zachery's Avatar
Zachery Zachery is offline Offline
The Geek Father

Re: Generate Thumbnail images on the fly.

 
0
  #8
Nov 18th, 2003
im not anygood with php scripts in general, i didnt get a chance to look over it but i didnt see anything that made sure not to regerage images over and over >.> so i was just adding my two cents
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 82
Reputation: himerus is an unknown quantity at this point 
Solved Threads: 0
himerus's Avatar
himerus himerus is offline Offline
Junior Poster in Training

Re: Generate Thumbnail images on the fly.

 
0
  #9
Nov 18th, 2003
DOH!! :eek:

It's really my fault, because the explanation is a little lacking at this point. That main segment of code is just whats called by the external scripts whenever it is needed, so depending on how it's called, it can do either.

That's why I really want to redo this whole deal, and make a really nice tutorial on it..

Those are the kinds of ?'s that will keep me on my toes though!!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2003
Posts: 781
Reputation: Zachery has a spectacular aura about Zachery has a spectacular aura about 
Solved Threads: 21
Team Colleague
Zachery's Avatar
Zachery Zachery is offline Offline
The Geek Father

Re: Generate Thumbnail images on the fly.

 
0
  #10
Nov 18th, 2003
glad to be of some help
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC