Deprecated WebGUI::Storage::Image. WebGUI::Storage can now do everything
WebGUI::Storage::Image can do.
This commit is contained in:
parent
2b3e88fd63
commit
ee84cc7611
34 changed files with 399 additions and 470 deletions
|
|
@ -26,6 +26,12 @@ use File::Spec;
|
|||
use Storable ();
|
||||
use WebGUI::Utility qw(isIn);
|
||||
|
||||
BEGIN {
|
||||
unless (eval { require Image::Magick; 1 }) {
|
||||
croak "You must have Image::Magick installed to run WebGUI.\n";
|
||||
}
|
||||
}
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Package WebGUI::Storage
|
||||
|
|
@ -70,6 +76,12 @@ This package provides a mechanism for storing and retrieving files that are not
|
|||
$store->renameFile($filename, $newFilename);
|
||||
$store->setPrivileges($userId, $groupIdView, $groupIdEdit);
|
||||
|
||||
my $boolean = $self->generateThumbnail($filename);
|
||||
my $url = $self->getThumbnailUrl($filename);
|
||||
my $boolean = $self->isImage($filename);
|
||||
my ($captchaFile, $challenge) = $self->addFileFromCaptcha;
|
||||
$self->resize($imageFile, $width, $height);
|
||||
|
||||
=head1 METHODS
|
||||
|
||||
These methods are available from this package:
|
||||
|
|
@ -147,6 +159,62 @@ sub _changeOwner {
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 addFileFromCaptcha ( )
|
||||
|
||||
Generates a captcha image (125px x 26px) and returns the filename and challenge string (6 random characters). For more information about captcha, consult the Wikipedia here: http://en.wikipedia.org/wiki/Captcha
|
||||
|
||||
=cut
|
||||
|
||||
sub addFileFromCaptcha {
|
||||
my $self = shift;
|
||||
my $error = "";
|
||||
my $challenge;
|
||||
$challenge.= ('A'..'Z')[rand(26)] foreach (1..6);
|
||||
my $filename = "captcha.".$self->session->id->generate().".gif";
|
||||
my $image = Image::Magick->new();
|
||||
$error = $image->Set(size=>'125x26');
|
||||
if($error) {
|
||||
$self->session->errorHandler->warn("Error setting captcha image size: $error");
|
||||
}
|
||||
$error = $image->ReadImage('xc:white');
|
||||
if($error) {
|
||||
$self->session->errorHandler->warn("Error initializing image: $error");
|
||||
}
|
||||
$error = $image->AddNoise(noise=>"Multiplicative");
|
||||
if($error) {
|
||||
$self->session->errorHandler->warn("Error adding noise: $error");
|
||||
}
|
||||
# AddNoise generates a different average color depending on library. This is ugly, but the best I can see for now
|
||||
my $textColor = '#222222';
|
||||
$error = $image->Annotate(font=>$self->session->config->getWebguiRoot."/lib/default.ttf", pointsize=>30, skewY=>0, skewX=>0, gravity=>'center', fill=>$textColor, antialias=>'true', text=>$challenge);
|
||||
if($error) {
|
||||
$self->session->errorHandler->warn("Error Annotating image: $error");
|
||||
}
|
||||
$error = $image->Draw(primitive=>"line", points=>"0,5 105,21", stroke=>$textColor, antialias=>'true', strokewidth=>2);
|
||||
if($error) {
|
||||
$self->session->errorHandler->warn("Error drawing line: $error");
|
||||
}
|
||||
$error = $image->Blur(geometry=>"9");
|
||||
if($error) {
|
||||
$self->session->errorHandler->warn("Error blurring image: $error");
|
||||
}
|
||||
$error = $image->Set(type=>"Grayscale");
|
||||
if($error) {
|
||||
$self->session->errorHandler->warn("Error setting grayscale: $error");
|
||||
}
|
||||
$error = $image->Border(fill=>'black', width=>1, height=>1);
|
||||
if($error) {
|
||||
$self->session->errorHandler->warn("Error setting border: $error");
|
||||
}
|
||||
$error = $image->Write($self->getPath($filename));
|
||||
if($error) {
|
||||
$self->session->errorHandler->warn("Error writing image: $error");
|
||||
}
|
||||
return ($filename, $challenge);
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 addFileFromFilesystem( pathToFile )
|
||||
|
||||
Grabs a file from the server's file system and saves it to a storage location and returns a URL compliant filename. If there are errors encountered during the add, then it will return undef instead.
|
||||
|
|
@ -317,6 +385,36 @@ sub addFileFromScalar {
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 adjustMaxImageSize ( $file )
|
||||
|
||||
Adjust the size of an image according to the C<maxImageSize> setting in the Admin
|
||||
Console.
|
||||
|
||||
=head3 $file
|
||||
|
||||
The name of the file to check for a maximum file size violation.
|
||||
|
||||
=cut
|
||||
|
||||
sub adjustMaxImageSize {
|
||||
my $self = shift;
|
||||
my $file = shift;
|
||||
my $max_size = shift || $self->session->setting->get("maxImageSize");
|
||||
my ($w, $h) = $self->getSizeInPixels($file);
|
||||
if($w > $max_size || $h > $max_size) {
|
||||
if($w > $h) {
|
||||
$self->resize($file, $max_size);
|
||||
}
|
||||
else {
|
||||
$self->resize($file, 0, $max_size);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 clear ( )
|
||||
|
||||
Clears a storage locations of all files except the .wgaccess file
|
||||
|
|
@ -482,6 +580,7 @@ sub deleteFile {
|
|||
return undef
|
||||
if $filename =~ m{\.\./}; ##prevent deleting files outside of this object
|
||||
unlink($self->getPath($filename));
|
||||
unlink($self->getPath('thumb-'.$filename));
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -529,6 +628,59 @@ sub get {
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 generateThumbnail ( filename, [ thumbnailSize ] )
|
||||
|
||||
Generates a thumbnail for this image.
|
||||
|
||||
=head3 filename
|
||||
|
||||
The file to generate a thumbnail for.
|
||||
|
||||
=head3 thumbnailSize
|
||||
|
||||
The size in pixels of the thumbnail to be generated. If not specified the thumbnail size in the global settings will be used.
|
||||
|
||||
=cut
|
||||
|
||||
sub generateThumbnail {
|
||||
my $self = shift;
|
||||
my $filename = shift;
|
||||
my $thumbnailSize = shift || $self->session->setting->get("thumbnailSize") || 100;
|
||||
unless (defined $filename) {
|
||||
$self->session->errorHandler->error("Can't generate a thumbnail when you haven't specified a file.");
|
||||
return 0;
|
||||
}
|
||||
unless ($self->isImage($filename)) {
|
||||
$self->session->errorHandler->warn("Can't generate a thumbnail for something that's not an image.");
|
||||
return 0;
|
||||
}
|
||||
my $image = Image::Magick->new;
|
||||
my $error = $image->Read($self->getPath($filename));
|
||||
if ($error) {
|
||||
$self->session->errorHandler->error("Couldn't read image for thumbnail creation: ".$error);
|
||||
return 0;
|
||||
}
|
||||
my ($x, $y) = $image->Get('width','height');
|
||||
my $n = $thumbnailSize;
|
||||
if ($x > $n || $y > $n) {
|
||||
my $r = $x>$y ? $x / $n : $y / $n;
|
||||
$x /= $r;
|
||||
$y /= $r;
|
||||
if($x < 1) { $x = 1 } # Dimentions < 1 cause Scale to fail
|
||||
if($y < 1) { $y = 1 }
|
||||
$image->Scale(width=>$x,height=>$y);
|
||||
$image->Sharpen('0.0x1.0');
|
||||
}
|
||||
$error = $image->Write($self->getPath.'/'.'thumb-'.$filename);
|
||||
if ($error) {
|
||||
$self->session->errorHandler->error("Couldn't create thumbnail: ".$error);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 getErrorCount ( )
|
||||
|
||||
Returns the number of errors that have been generated on this object instance.
|
||||
|
|
@ -683,6 +835,8 @@ sub getFiles {
|
|||
if (!$showAll) {
|
||||
# if not showing all, filter out files beginning with a period
|
||||
@list = grep { $_ !~ /^\./ } @list;
|
||||
# filter out thumbnails
|
||||
@list = grep { $_ !~ /^thumb-/ } @list;
|
||||
}
|
||||
}
|
||||
return \@list;
|
||||
|
|
@ -772,6 +926,64 @@ sub getPathFrag {
|
|||
return join '/', @{ $self->{_pathParts} };
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=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) {
|
||||
$self->session->errorHandler->error("Can't check the size when you haven't specified a file.");
|
||||
return 0;
|
||||
}
|
||||
unless ($self->isImage($filename)) {
|
||||
$self->session->errorHandler->error("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) {
|
||||
$self->session->errorHandler->error("Couldn't read image to check the size of it: ".$error);
|
||||
return 0;
|
||||
}
|
||||
return $image->Get('width','height');
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 getThumbnailUrl ( filename )
|
||||
|
||||
Returns the URL to a thumbnail for a given image.
|
||||
|
||||
=head3 filename
|
||||
|
||||
The file to retrieve the thumbnail for.
|
||||
|
||||
=cut
|
||||
|
||||
sub getThumbnailUrl {
|
||||
my $self = shift;
|
||||
my $filename = shift;
|
||||
if (! defined $filename) {
|
||||
$self->session->errorHandler->error("Can't make a thumbnail url without a filename.");
|
||||
return '';
|
||||
}
|
||||
if (! isIn($filename, @{ $self->getFiles() })) {
|
||||
$self->session->errorHandler->error("Can't make a thumbnail for a file named '$filename' that is not in my storage location.");
|
||||
return '';
|
||||
}
|
||||
return $self->getUrl("thumb-".$filename);
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
|
|
@ -797,6 +1009,28 @@ sub getUrl {
|
|||
return $url;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 isImage ( filename )
|
||||
|
||||
Checks to see that the file specified is an image. Returns a 1 or 0 depending upon the result.
|
||||
|
||||
=head3 filename
|
||||
|
||||
The file to check.
|
||||
|
||||
=cut
|
||||
|
||||
sub isImage {
|
||||
my $self = shift;
|
||||
my $filename = shift;
|
||||
return isIn($self->getFileExtension($filename), qw(jpeg jpg gif png))
|
||||
}
|
||||
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 renameFile ( filename, newFilename )
|
||||
|
|
@ -821,6 +1055,85 @@ sub renameFile {
|
|||
rename $self->getPath($filename), $self->getPath($newFilename);
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=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.
|
||||
|
||||
=head3 density
|
||||
|
||||
The new image density in pixels per inch.
|
||||
|
||||
=cut
|
||||
|
||||
# TODO: Make this take a hash reference with width, height, and density keys.
|
||||
|
||||
sub resize {
|
||||
my $self = shift;
|
||||
my $filename = shift;
|
||||
my $width = shift;
|
||||
my $height = shift;
|
||||
my $density = shift;
|
||||
unless (defined $filename) {
|
||||
$self->session->errorHandler->error("Can't resize when you haven't specified a file.");
|
||||
return 0;
|
||||
}
|
||||
unless ($self->isImage($filename)) {
|
||||
$self->session->errorHandler->error("Can't resize something that's not an image.");
|
||||
return 0;
|
||||
}
|
||||
unless ($width || $height || $density) {
|
||||
$self->session->errorHandler->error("Can't resize with no resizing parameters.");
|
||||
return 0;
|
||||
}
|
||||
my $image = Image::Magick->new;
|
||||
my $error = $image->Read($self->getPath($filename));
|
||||
if ($error) {
|
||||
$self->session->errorHandler->error("Couldn't read image for resizing: ".$error);
|
||||
return 0;
|
||||
}
|
||||
|
||||
# First, change image density
|
||||
if ( $density ) {
|
||||
$self->session->errorHandler->info( "Setting $filename to $density" );
|
||||
$image->Set( density => "${density}x${density}" );
|
||||
}
|
||||
|
||||
# Next, resize dimensions
|
||||
if ( $width || $height ) {
|
||||
$self->session->errorHandler->info( "Resizing $filename to w:$width h:$height" );
|
||||
my ($x, $y) = $image->Get('width','height');
|
||||
if (!$height) { # proportional scale by width
|
||||
$height = $width / $x * $y;
|
||||
}
|
||||
elsif (!$width) { # proportional scale by height
|
||||
$width = $height * $x / $y;
|
||||
}
|
||||
$image->Resize( height => $height, width => $width );
|
||||
}
|
||||
|
||||
# Write our changes to disk
|
||||
$error = $image->Write($self->getPath($filename));
|
||||
if ($error) {
|
||||
$self->session->errorHandler->error("Couldn't resize image: ".$error);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue