diff --git a/docs/changelog/6.x.x.txt b/docs/changelog/6.x.x.txt index 82c7c9fe0..7605fce45 100644 --- a/docs/changelog/6.x.x.txt +++ b/docs/changelog/6.x.x.txt @@ -8,6 +8,8 @@ - Added isAdminOn(), switchOnAdmin(), and switchOffAdmin() to WebGUI::Session to eliminate all of the previously cryptic means of doing those things. - Added a temporary file storage mechanism to WebGUI::Storage. + - Added an image resizer to the image asset. + - Image Magick is now required to run WebGUI. - Fixed resetting votes on Poll would crash it. - Fixed not being able to set display title and other yes no questions to no. - Fixed a bug where URLs would become unreachable when using SSL. @@ -33,7 +35,7 @@ - bugfix [ 1154247 ] Title and menuTitle set to 'untitled' if url is changed - bugfix [ 1150982 ] Subscribe to forum thread causes error - bugfix [ 1151216 ] The Latest News is blank on demo.plainblack.com - - Image Magick is now required to run WebGUI. + - bugfix [ 1149458 ] CVS 6.3 -> ENV variables have changed for mod_perl 2.0 6.3.0 diff --git a/lib/WebGUI/Asset/File/Image.pm b/lib/WebGUI/Asset/File/Image.pm index a6d45e9ae..fa8b86a33 100644 --- a/lib/WebGUI/Asset/File/Image.pm +++ b/lib/WebGUI/Asset/File/Image.pm @@ -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=>'thumbnail' ); + 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 = '
'.$self->get(
'; + return $self->getAdminConsole->render($f->print.$image,WebGUI::International::get("resize image","Image")); } #------------------------------------------------------------------- diff --git a/lib/WebGUI/Storage/Image.pm b/lib/WebGUI/Storage/Image.pm index 34a3ff2e6..224161c0e 100644 --- a/lib/WebGUI/Storage/Image.pm +++ b/lib/WebGUI/Storage/Image.pm @@ -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; diff --git a/lib/WebGUI/i18n/English/Image.pm b/lib/WebGUI/i18n/English/Image.pm index a9a7a4bb7..ebe17a731 100644 --- a/lib/WebGUI/i18n/English/Image.pm +++ b/lib/WebGUI/i18n/English/Image.pm @@ -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 + }, + };