I fiddled a bit around with GD and came up with the following code (it is untested, since ActiveState does not have GD):
#!perl
use strict;
use warnings;
use GD;
my $filename = '/path/to/img/img.jpg';
my $outfilename = '/path/to/img/thumb/img.jpg';
my $image = new GD::Image $filename or die "Could not open '$filename'
+: $!\n";
my ($width, $height) = $image->getBounds;
my ($newwidth, $newheight);
if (($width > 300) || ($height > 300)) {
if ($width > $height) {
($newwidth, $newheight) = (300, int((300 * $height) / $width));
} else {
($newwidth, $newheight) = (int((300 * $width) / $height), 300);
}
} else {
($newwidth, $newheight) = ($width, $height);
}
my $newimg = $image->copyResized(
$image, # source image
0, 0, # destination position
0, 0, # original position
$newwidth, $newheight,
$width, $height
);
$newimg->();
open OUTFILE, '>', $outfilename or die "Failed to open '$outfilename'
+for output: $!\n";
print OUTFILE $newimg->jpeg;
close OUTFILE;
I hope it works and suits your needs.
Cheers,
CombatSquirrel.
Update: For files not supplied by
ActiveState, have a look at
PPM::Repositories, thanks to
PodMaster.
Entropy is the tendency of everything going to hell.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.