| | |
Generate Thumbnail images on the fly.
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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
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)
[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.
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
PHP Syntax (Toggle Plain Text)
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)
PHP Syntax (Toggle Plain Text)
--with-gd=/your/path \ --with-jpeg-dir=/your/path \ --with-png-dir=/your/path \ --with-freetype-dir=/your/path \ --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.
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.
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.
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.
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.
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.
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.
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!!!
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!!!
![]() |
Other Threads in the PHP Forum
- Previous Thread: Help a Newb here!
- Next Thread: how to insert database value to a textbox
| Thread Tools | Search this Thread |
apache api array auto beginner binary broken cache cakephp checkbox class cms code codingproblem cron curl customizableitems database date display dynamic echo email error errorlog file files filter folder form format forms forum function functions gc_maxlifetime global google headmethod href htaccess html image include insert ip javascript joomla limit link login mail malfunctioning memmory memory menu method mlm multiple mysql nodes oop parameter parsing paypal pdf php phpmysql popup query radio random recursion recursiveloop remote script search select server sessions snippet source space sql static survey syntax system table trouble tutorial up-to-date update upload url validator variable video web youtube






