Article

Build An Automated PHP Gallery System In Minutes

Page: 1 2 3 4 5 6 7 8 Next

Storing the Images

It’s time to store the uploaded image in the directory we created earlier. We will use the copy function to copy the image from its temporary location to our photos directory.

copy($photos_uploaded['tmp_name'][$counter],    
 $images_dir . '/' . $filename);

We can also use the PHP 4-specific function:

move_uploaded_file($photos_uploaded['tmp_name'][$counter],    
 $images_dir . '/' . $filename);

The important thing to notice in the above code is $photos_uploaded['tmp_name'][$counter]. As discussed earlier, PHP provides us with the temporary location of the uploaded file; key tmp_name holds that value.

Automatic Thumbnails

Now comes the most interesting part: building the automatic thumbnails. To build these thumbnails we require an image manipulation program, e.g. the Graphics Development (GD) Library, ImageMagick or NetPbm, all of which are available online for free.

Before we can build a thumbnail, we need to determine the dimensions of the image. To ensure we create proportionate thumbnails, we classify all images into two major categories:

  1. Wide Image, where the width is greater than the height
  2. Tall Image, where the height is greater than the width

Now we need to find out whether the uploaded image is Wide one or Tall. For that, we use the GetImageSize function to fetch the dimensions of the uploaded image and, using those vales, we decide the appropriate size of the thumbnail:

$size = GetImageSize($images_dir . "/" . $filename);    
   
// Wide Image    
if($size[0] > $size[1])    
{    
$thumbnail_width = 100;    
$thumbnail_height = (int)(100 * $size[1] / $size[0]);    
}    
   
// Tall Image    
else    
{    
 $thumbnail_width = (int)(100 * $size[0] / $size[1]);    
 $thumbnail_height = 100;    
}

Here we calculate the size for the thumbnail using the aspect ratio of the dimensions of the original image. I have preset the thumbnail size to 100 pixels wide or tall, depending on the size of the original image.

The formulae used to calculate the aspect ratios are as follows.

For Wide Images:

$thumbnail_width = <Preset Width>    
$thumbnail_height = <Preset Width> * <height_dimension_of_original_image> /    
                  <width_dimension_of_original_image>

For Tall images:

$thumbnail_width = <Preset Height> * <width_dimension_of_original_image> /    
                        <height_dimension_of_original_image>    
$thumbnail_height = <Preset Height>

Using ImageMagick

ImageMagick is a collection of tools that allow us to create and manipulate images using command line arguments. ImageMagick supports a range of image formats, including JPEG, GIF, BMP, PNG, TIFF, etc. Binaries for all popular operating systems are available and can be downloaded from www.ImageMagick.org.

Before using ImageMagick, you need to know its absolute location path on your server:

$imagemagickPath = "/usr/local/bin";

To create thumbnails, we use the convert utility of ImageMagick with which we resize the original image, thereby instructing it to modify its geometry. The following code uses command line arguments to send values to ImageMagick:

exec("$imagemagickPath/convert -geometry " .    
 "{$thumbnail_width}x{$thumbnail_height} " .    
 "$images_dir/$filename $images_dir/tb_$filename");

The convert utility requires arguments in the following format:

convert –geometry <width>x<height> <source_path> <destination_path>

Here, <source_path> is the path to the original uploaded file and <desitnation_path> is the path where the thumbnail is to be created. The naming convention used for thumbnails is “tb_filename.extension”.

Alternatively, you can also use Perl, in combination with PerlMagick, to communicate with ImageMagick.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links