image resizer

This commit is contained in:
JT Smith 2005-03-04 18:49:09 +00:00
parent a83e8b85c1
commit 7db5db5833
4 changed files with 173 additions and 2 deletions

View file

@ -57,6 +57,7 @@ Generates a captcha image (105px x 26px) and returns the filename and challenge
=cut
sub addFileFromCaptcha {
my $self = shift;
my $challenge;
$challenge.= ('A'..'Z')[26*rand] foreach (1..6);
my $filename = "captcha.".WebGUI::Id::generate().".png";
@ -111,6 +112,7 @@ sub generateThumbnail {
if ($x > $n || $y > $n) {
my $r = $x>$y ? $x / $n : $y / $n;
$image->Scale(width=>($x/$r),height=>($y/$r));
$image->Sharpen('0.0x1.0');
}
$error = $image->Write($self->getPath.$session{os}{slash}.'thumb-'.$filename);
if ($error) {
@ -143,6 +145,38 @@ sub getFiles {
#-------------------------------------------------------------------
=head2 getSizeInPixels ( filename )
Returns the width and height in pixels of the specified file.
=head3 filename
The name of the file to get the size of.
=cut
sub getSizeInPixels {
my $self = shift;
my $filename = shift;
unless (defined $filename) {
WebGUI::ErrorHandler::warn("Can't check the size when you haven't specified a file.");
return 0;
}
unless ($self->isImage($filename)) {
WebGUI::ErrorHandler::warn("Can't check the size of something that's not an image.");
return 0;
}
my $image = Image::Magick->new;
my $error = $image->Read($self->getPath($filename));
if ($error) {
WebGUI::ErrorHandler::warn("Couldn't read image for resizing: ".$error);
return 0;
}
return $image->Get('width','height');
}
#-------------------------------------------------------------------
=head2 getThumbnailUrl ( filename )
Returns the URL to a thumbnail for a given image.
@ -179,5 +213,64 @@ sub isImage {
}
#-------------------------------------------------------------------
=head2 resize ( filename [, width, height ] )
Resizes the specified image by the specified height and width. If either is omitted the iamge will be scaleed proportionately to the non-omitted one.
=head3 filename
The name of the file to resize.
=head3 width
The new width of the image in pixels.
=head3 height
The new height of the image in pixels.
=cut
sub resize {
my $self = shift;
my $filename = shift;
my $width = shift;
my $height = shift;
unless (defined $filename) {
WebGUI::ErrorHandler::warn("Can't resize when you haven't specified a file.");
return 0;
}
unless ($self->isImage($filename)) {
WebGUI::ErrorHandler::warn("Can't resize something that's not an image.");
return 0;
}
unless ($width || $height) {
WebGUI::ErrorHandler::warn("Can't resize with no resizing parameters.");
return 0;
}
my $image = Image::Magick->new;
my $error = $image->Read($self->getPath($filename));
if ($error) {
WebGUI::ErrorHandler::warn("Couldn't read image for resizing: ".$error);
return 0;
}
my ($x, $y) = $image->Get('width','height');
if ($width && !$height) { # proportional scale by width
$height = $width / $x * $y;
} elsif (!$width && $height) { # proportional scale by height
$width = $height * $x / $y;
}
$image->Scale(width=>$width, height=>$height);
$error = $image->Write($self->getPath($filename));
if ($error) {
WebGUI::ErrorHandler::warn("Couldn't create thumbnail: ".$error);
return 0;
}
return 1;
}
1;