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

@ -17,6 +17,7 @@ package WebGUI::Asset::File::Image;
use strict;
use WebGUI::Asset::File;
use WebGUI::Storage::Image;
use WebGUI::HTMLForm;
use WebGUI::HTTP;
use WebGUI::Session;
use WebGUI::Utility;
@ -129,6 +130,11 @@ sub getEditForm {
-label=>WebGUI::International::get('thumbnail', 'Image'),
-value=>'<a href="'.$self->getFileUrl.'"><img src="'.$self->getThumbnailUrl.'?noCache='.time().'" alt="thumbnail" /></a>'
);
my ($x, $y) = $self->getStorageLocation->getSizeInPixels($self->get("filename"));
$tabform->getTab("properties")->readOnly(
-label=>WebGUI::International::get('image size', 'Image'),
-value=>$x.' x '.$y
);
}
return $tabform;
}
@ -205,7 +211,40 @@ sub view {
sub www_edit {
my $self = shift;
return WebGUI::Privilege::insufficient() unless $self->canEdit;
return $self->getAdminConsole->render($self->getEditForm->print,"Edit Image");
$self->getAdminConsole->addSubmenuItem($self->getUrl('func=resize'),WebGUI::International::get("resize image","Image")) if ($self->get("filename"));
return $self->getAdminConsole->render($self->getEditForm->print,WebGUI::International::get("edit image","Image"));
}
#-------------------------------------------------------------------
sub www_resize {
my $self = shift;
return WebGUI::Privilege::insufficient() unless $self->canEdit;
if ($session{form}{newWidth} || $session{form}{newHeight}) {
$self->getStorageLocation->resize($self->get("filename"),$session{form}{newWidth},$session{form}{newHeight});
$self->setSize($self->getStorageLocation->getFileSize($self->get("filename")));
}
$self->getAdminConsole->addSubmenuItem($self->getUrl('func=edit'),WebGUI::International::get("edit image","Image"));
my $f = WebGUI::HTMLForm->new(-action=>$self->getUrl);
$f->hidden(
-name=>"func",
-value=>"resize"
);
my ($x, $y) = $self->getStorageLocation->getSizeInPixels($self->get("filename"));
$f->readOnly(
-label=>WebGUI::International::get('image size', 'Image'),
-value=>$x.' x '.$y
);
$f->integer(
-label=>WebGUI::International::get('new width','Image'),
-name=>"newWidth"
);
$f->integer(
-label=>WebGUI::International::get('new height','Image'),
-name=>"newHeight"
);
$f->submit;
my $image = '<div align="center"><img src="'.$self->getStorageLocation->getUrl($self->get("filename")).'" border="1" alt="'.$self->get("filename").'" /></div>';
return $self->getAdminConsole->render($f->print.$image,WebGUI::International::get("resize image","Image"));
}
#-------------------------------------------------------------------

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;

View file

@ -48,6 +48,43 @@ shown here.
context => q|label for Image asset form|,
lastUpdated => 1106765841
},
'image size' => {
message => q|Image Size|,
context => q|label for Image asset form|,
lastUpdated => 1106765841
},
'image size' => {
message => q|Image Size|,
context => q|label for Image asset form|,
lastUpdated => 1106765841
},
'edit image' => {
message => q|Edit Image|,
context => q|label to edit the image|,
lastUpdated => 1106765841
},
'resize image' => {
message => q|Resize Image|,
context => q|label to resize the image|,
lastUpdated => 1106765841
},
'new width' => {
message => q|New Width|,
context => q|label to resize the image|,
lastUpdated => 1106765841
},
'new height' => {
message => q|New Height|,
context => q|label to resize the image|,
lastUpdated => 1106765841
},
};