|
|
OverviewGenerate a new image file with specified size using GD. Also refer to Making thumbnails with a restriction of file size.
Flow
A sample code
use GD;
my $im;
$im =~ /\.jpe?g$/i and $im = GD::Image->newFromJpeg($imagefile,1);
$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 $target_im = new GD::Image($new_width,$new_height,1);
$target_im->copyResized($im,0,0,0,0,$new_width,$new_height,
$width,$height);
open(IMAGE, "> $new_image");
binmode(IMAGE);
if ($imagefile =~ /\.jpe?g$/i){
print IMAGE $target_im->jpeg(80);
} elsif ($imagefile =~ /\.gif$/i) {
print IMAGE $target_im->gif();
} elsif ($imagefile =~ /\.png$/i) {
print IMAGE $target_im->png();
} elsif ($imagefile =~ /\.gd$/i) {
print IMAGE $target_im->gd();
} elsif ($imagefile =~ /\.gd2$/i) {
print IMAGE $target_im->gd2();
}
close(IMAGE);
Description of the codeuse GD; Load GD. my $im; $im =~ /\.jpe?g$/i and $im = GD::Image->newFromJpeg($imagefile,1); $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. $im = GD::Image->newFromJpeg($imagefile,1); The second argument of the newFromJpeg is set when Truecolor is selected. According to the document, Truecolor is automatically selected when an object is created from a file. However, generated files seem different than not specifing this argument. Therefore, in this example use it. 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 $target_im = new GD::Image($new_width,$new_height,1); Generate a new object with the target width and height. The third arugument also indicates to choose Truecolor.
$target_im->copyResized($im,0,0,0,0,$new_width,$new_height,
$width,$height);
Copy the original image object into the new size-modified image object using copyResized. For reference, copyResizse could take the following parameters.
$im->copyResized($sourceImage,$dstX,$dstY,
$srcX,$srcY,$destW,$destH,$srcW,$srcH)
$sourceImage : Original image object
$dstX : Target image's start X
$dstY : Target image's start Y
$srcX : Original image's start X
$srcY : Original image's start Y
$destW : Target image width
$destH : Target image heigh
$srcW : Original image width
$srcH : Original image height
open(IMAGE, "> $new_image");
binmode(IMAGE);
if ($imagefile =~ /\.jpe?g$/i){
print IMAGE $target_im->jpeg(80);
} elsif ($imagefile =~ /\.gif$/i) {
print IMAGE $target_im->gif();
} elsif ($imagefile =~ /\.png$/i) {
print IMAGE $target_im->png();
} elsif ($imagefile =~ /\.gd$/i) {
print IMAGEfile $target_im->gd();
} elsif ($imagefile =~ /\.gd2$/i) {
print IMAGE $target_im->gd2();
}
close(IMAGE);
Write out the object into a file. To write out the same format as the original file, this example uses the extention to identify the format. It is not necessary to be the same format as the original one. If you want to convert the format, you can do it now. The written file is a shrunk thumbnail. $target_im->jpeg(80); The argument of the jpeg method means Quality. It takes a number between 0 and 100. Bigger number gets better quality. If it is omitted, jpeg method is supposed to choose the best quality. However, from my experience, it did not work that well. Since 100 becomes too big file size, a number 80 though 90 should be good enough. |