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

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)

--with-gd=/your/path \ 
--with-jpeg-dir=/your/path \ 
--with-png-dir=/your/path \ 
--with-freetype-dir=/your/path \ 
--with-zlib-dir=/your/path
$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);

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. :D

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.

why not use mysql or another type of DB to store a generate images, so that theyre only generated once

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.

:)

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 :)

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.. :D

Those are the kinds of ?'s that will keep me on my toes though!!! ;)

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.

GD works fine on Apache on Windows (PHP4) didn't test PHP5 but it should work.

GD works fine on Windows and *nix hosting. BTW most hosting providers have GD installed by default, so that should not be a problem.

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

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)

--with-gd=/your/path \ 
--with-jpeg-dir=/your/path \ 
--with-png-dir=/your/path \ 
--with-freetype-dir=/your/path \ 
--with-zlib-dir=/your/path
$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);

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. :)

Hmmm Mate ,
I tried this and i managed to get it to work as well , but the quality of the generated image is VERY BAD , it looks like a 16 bit image :P

I am not pretty sure if its due to my PC , or due to settings in the above piece of code which has limited the number of colours so as to limit the size of the generated thumbnail.

whatever be the case your article has been a great help to several people browsing through the forum. If you can help me out with improving picture quality , it'll be great.

Looking forward for your response.

Rahul Agrawal
PHP Developer

the bad quality is caused by imagecreate

for jpg and png it's better to use imagecreatetruecolor

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

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)

--with-gd=/your/path \ 
--with-jpeg-dir=/your/path \ 
--with-png-dir=/your/path \ 
--with-freetype-dir=/your/path \ 
--with-zlib-dir=/your/path
$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);

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. :)

Here is also good and easy example of creating thumbnail with class and simple function:
1: http://www.phpmoot.com/php-image-processing/
2: http://www.phpmoot.com/php-create-thumbnail/

This article has been dead for over six months. Start a new discussion instead.