Modifying image size

Top Japanese page




Overview

Using GD, obtains a image size and modifing the sizse and display on a browser without changing the aspect ratio. This does not write out to a file. For generating a thumbnail file, refer to Making thumbnails.

Flow

  1. Make an object from original image file
  2. Get the image size from the object
  3. Calculate actual image size to be fit in the target size
  4. Display the image with the new size

A sample code

 use GD;
 
 my $im;
 $im =~ /\.jpe?g$/i and $im = GD::Image->newFromJpeg($imagefile);
 $im =~ /\.gif$/i   and $im = GD::Image->newFromGif($imagefile);
 $im =~ /\.png$/i   and $im = GD::Image->newFromPng($imagefile);
 $im =~ /\.gd$/i    and $im = GD::Image->newFromgd($imagefile);
 $im =~ /\.gd2$/i   and $im = GD::Image->newFromgd2($imagefile);
 
 my ($width, $height) = $im->getBounds();
 
 my $width_shrink  = $max_width / $width;
 my $height_shrink = $max_height / $height;
 my $shrink_ratio;
 if ($width_shrink < $height_shrink){
        $shrink_ratio = $width_shrink;
 } else {
        $shrink_ratio = $height_shrink;
 }
 my $actual_width = int($width * $shrink_ratio);
 my $actual_height = int($height * $shrink_ratio);
 
 print qq(<img src="$image" width="$actual_width" height="$actual_height">\n);

Description of the code

 use GD;

Load GD.

 my $im;
 $im =~ /\.jpe?g$/i and $im = GD::Image->newFromJpeg($imagefile);
 $im =~ /\.gif$/i   and $im = GD::Image->newFromGif($imagefile);
 $im =~ /\.png$/i   and $im = GD::Image->newFromPng($imagefile);
 $im =~ /\.gd$/i    and $im = GD::Image->newFromgd($imagefile);
 $im =~ /\.gd2$/i   and $im = GD::Image->newFromgd2($imagefile);

$image contains an image file. Based on the extention, select a constracter method which load an image file and generate an object.

Old versions of GD do not support newFromGif. And if the extention and actual image format is different, the GD reports an error. If the image file's size is 0, the GD also reports an error. These errors are fatal and script itself stops working. The error cause could be found from the server log file and must be fix the cause.

 my ($width, $height) = $im->getBounds();

Get the image size by getBound method. The widht is put into $width, the heigh is put into $height.

 my $width_shrink  = $max_width / $width;
 my $height_shrink = $max_height / $height;
 my $shrink_ratio;
 if ($width_shrink < $height_shrink){
        $shrink_ratio = $width_shrink;
 } else {
        $shrink_ratio = $height_shrink;
 }
 my $actual_width = int($width * $shrink_ratio + 0.5);
 my $actual_height = int($height * $shrink_ratio + 0.5);

$max_width and $max_height contain target sizes set prior. Get a shrink ratio of both width and height and take the smaller one. Based on the shrink ratio, a new width and a height are calcurated. In this example, 0.5 is added in order to round nearest integer value.

 print qq(<img src="$imagefile" width="$actual_width" height="$actual_height">\n);

Dipslay the image with the calcurated size onto the browser.