Deprecated WebGUI::Storage::Image. WebGUI::Storage can now do everything

WebGUI::Storage::Image can do.
This commit is contained in:
JT Smith 2008-11-26 16:06:18 +00:00
parent 2b3e88fd63
commit ee84cc7611
34 changed files with 399 additions and 470 deletions

View file

@ -548,7 +548,7 @@ sub www_view {
if($hash->{'isViewable'}) {
$hash->{'friend_profile_url'} = $friend->getProfileUrl;
if($friend->profileField("photo")) {
my $store = WebGUI::Storage::Image->get($session,$friend->profileField("photo"));
my $store = WebGUI::Storage->get($session,$friend->profileField("photo"));
my $file = $store->getFiles->[0];
$hash->{'friend_photo_url' } = $store->getUrl($file);
$hash->{'friend_photo_url_thumb'} = $store->getThumbnailUrl($file);

View file

@ -17,7 +17,7 @@ package WebGUI::AdSpace::Ad;
use strict;
use WebGUI::AdSpace;
use WebGUI::Macro;
use WebGUI::Storage::Image;
use WebGUI::Storage;
=head1 NAME
@ -81,7 +81,7 @@ Deletes this ad.
sub delete {
my $self = shift;
my $storage = WebGUI::Storage::Image->get($self->session, $self->get("storageId"));
my $storage = WebGUI::Storage->get($self->session, $self->get("storageId"));
$storage->delete if defined $storage;
$self->session->db->deleteRow("advertisement","adId",$self->getId);
$self = undef;
@ -264,7 +264,7 @@ sub set {
if ($self->get("type") eq "text") {
$self->{_properties}{renderedAd} = '<div style="position:relative; width:'.($adSpace->get("width")-2).'px; height:'.($adSpace->get("height")-2).'px; margin:0px; overflow:hidden; border:solid '.$self->get("borderColor").' 1px;"><a href="'.$self->session->url->gateway(undef, "op=clickAd;id=".$self->getId).'" style="position:absolute; padding: 3px; top:0px; left:0px; width:100%; height:100%; z-index:10; display:block; text-decoration:none; vertical-align:top; background-color:'.$self->get("backgroundColor").'; font-size: 13px; font-weight: normal;"><b><span style="color:'.$self->get("textColor").';">'.$self->get("title").'</span></b><br /><span style="color:'.$self->get("textColor").';">'.$self->get("adText").'</span></a></div>';
} elsif ($self->get("type") eq "image") {
my $storage = WebGUI::Storage::Image->get($self->session, $self->get("storageId"));
my $storage = WebGUI::Storage->get($self->session, $self->get("storageId"));
$self->{_properties}{renderedAd} = '<div style="position:relative; width:'.$adSpace->get("width").'px; height:'.$adSpace->get("height").'px; margin:0px; overflow:hidden; border:0px;"><a href="'.$self->session->url->gateway(undef, "op=clickAd;id=".$self->getId).'" style="position:absolute; padding: 3px; top:0px; left:0px; width:100%; height:100%; z-index:10; display:block; text-decoration:none; vertical-align:top;"><img src="'.$storage->getUrl($storage->getFiles->[0]).'" alt="'.$self->get("title").'" style="z-index:0;position:relative;border-style:none;border: 0px;" /></a></div>';
} elsif ($self->get("type") eq "rich") {
my $ad = $self->get("richMedia");

View file

@ -20,7 +20,7 @@ use Carp qw(croak);
use WebGUI::International;
use WebGUI::Asset::Template;
use WebGUI::Form;
use WebGUI::Storage::Image;
use WebGUI::Storage;
use Storable;
use base 'WebGUI::Asset';
@ -1214,10 +1214,10 @@ sub getStorageLocation {
my $self = shift;
unless (exists $self->{_storageLocation}) {
if ($self->get("storageId") eq "") {
$self->{_storageLocation} = WebGUI::Storage::Image->create($self->session);
$self->{_storageLocation} = WebGUI::Storage->create($self->session);
$self->update({storageId=>$self->{_storageLocation}->getId});
} else {
$self->{_storageLocation} = WebGUI::Storage::Image->get($self->session,$self->get("storageId"));
$self->{_storageLocation} = WebGUI::Storage->get($self->session,$self->get("storageId"));
}
}
return $self->{_storageLocation};
@ -1702,7 +1702,7 @@ sub purge {
my $self = shift;
my $sth = $self->session->db->read("select storageId from Event where assetId=?",[$self->getId]);
while (my ($storageId) = $sth->array) {
my $storage = WebGUI::Storage::Image->get($self->session,$storageId);
my $storage = WebGUI::Storage->get($self->session,$storageId);
$storage->delete if defined $storage;
}
$sth->finish;

View file

@ -26,7 +26,7 @@ use Tie::IxHash;
use WebGUI::DateTime;
use WebGUI::Friends;
use WebGUI::Utility;
use WebGUI::Storage::Image;
use WebGUI::Storage;
=head1 NAME
@ -44,7 +44,7 @@ use WebGUI::Asset::File::GalleryFile::Photo
=head2 Geometry '...' is invalid. Skipping.
makeResolutions will not pass invalid geometries to WebGUI::Storage::Image::resize().
makeResolutions will not pass invalid geometries to WebGUI::Storage::resize().
Valid geometries are one of the following forms:
^\d+$
@ -259,7 +259,7 @@ Image class.
=cut
sub getStorageClass {
return 'WebGUI::Storage::Image';
return 'WebGUI::Storage';
}
#----------------------------------------------------------------------------

View file

@ -16,7 +16,7 @@ package WebGUI::Asset::File::Image;
use strict;
use base 'WebGUI::Asset::File';
use WebGUI::Storage::Image;
use WebGUI::Storage;
use WebGUI::HTMLForm;
use WebGUI::Utility;
use WebGUI::Form::Image;
@ -189,7 +189,7 @@ Returns the class name of the WebGUI::Storage we should use for this asset.
=cut
sub getStorageClass {
return 'WebGUI::Storage::Image';
return 'WebGUI::Storage';
}
#-------------------------------------------------------------------

View file

@ -19,7 +19,7 @@ use WebGUI::Asset;
use WebGUI::Asset::File;
use WebGUI::Asset::File::Image;
use WebGUI::SQL;
use WebGUI::Storage::Image;
use WebGUI::Storage;
use WebGUI::TabForm;
use WebGUI::Utility;
@ -151,10 +151,10 @@ sub editSave {
##This is a hack. File uploads should go through the WebGUI::Form::File API
my $tempFileStorageId = WebGUI::Form::File->new($self->session,{name => 'file'})->getValue;
my $tempStorage = WebGUI::Storage::Image->get($self->session, $tempFileStorageId);
my $tempStorage = WebGUI::Storage->get($self->session, $tempFileStorageId);
foreach my $filename (@{$tempStorage->getFiles}) {
#my $storage = WebGUI::Storage::Image->create($self->session);
#my $storage = WebGUI::Storage->create($self->session);
#$storage->addFileFromFilesystem($tempStorage->getPath($filename));
#$storage->setPrivileges($self->getParent->get("ownerUserId"),$self->getParent->get("groupIdView"),$self->getParent->get("groupIdEdit"));

View file

@ -27,7 +27,7 @@ use WebGUI::Mail::Send;
use WebGUI::Operation;
use WebGUI::Paginator;
use WebGUI::SQL;
use WebGUI::Storage::Image;
use WebGUI::Storage;
use WebGUI::User;
use WebGUI::Utility;
use WebGUI::VersionTag;
@ -351,7 +351,7 @@ sub getAvatarUrl {
#Get avatar field, storage Id.
my $storageId = $user->profileField("avatar");
return '' unless $storageId;
my $avatar = WebGUI::Storage::Image->get($self->session,$storageId);
my $avatar = WebGUI::Storage->get($self->session,$storageId);
my $avatarUrl = '';
if ($avatar) {
#Get url from storage object.
@ -476,10 +476,10 @@ sub getStorageLocation {
my $self = shift;
unless (exists $self->{_storageLocation}) {
if ($self->get("storageId") eq "") {
$self->{_storageLocation} = WebGUI::Storage::Image->create($self->session);
$self->{_storageLocation} = WebGUI::Storage->create($self->session);
$self->update({storageId=>$self->{_storageLocation}->getId});
} else {
$self->{_storageLocation} = WebGUI::Storage::Image->get($self->session,$self->get("storageId"));
$self->{_storageLocation} = WebGUI::Storage->get($self->session,$self->get("storageId"));
}
}
return $self->{_storageLocation};

View file

@ -15,7 +15,7 @@ use Tie::CPHash;
use Tie::IxHash;
use WebGUI::Cache;
use WebGUI::HTMLForm;
use WebGUI::Storage::Image;
use WebGUI::Storage;
use WebGUI::SQL;
use WebGUI::Utility;
use JSON;
@ -443,11 +443,11 @@ sub getQuantityAvailable {
=head2 getThumbnailUrl ( [$store] )
Return a URL to the thumbnail for an image stored in this Product by creating
a WebGUI::Storage::Image object and calling its getThumbnailUrl method.
a WebGUI::Storage object and calling its getThumbnailUrl method.
=head3 $store
This should be a WebGUI::Storage::Image object. If it is not defined,
This should be a WebGUI::Storage object. If it is not defined,
then by default getThumbnailUrl will attempt to look up the URL for
the 'image1' property.
@ -463,7 +463,7 @@ sub getThumbnailUrl {
return $store->getThumbnailUrl($store->getFiles->[0]);
}
elsif ($self->get('image1')) {
$store = WebGUI::Storage::Image->get($self->session, $self->get('image1'));
$store = WebGUI::Storage->get($self->session, $self->get('image1'));
return $store->getThumbnailUrl($store->getFiles->[0]);
}
else {
@ -1451,19 +1451,19 @@ sub view {
}
#---image1
if ($image1) {
my $file = WebGUI::Storage::Image->get($session,$image1);
my $file = WebGUI::Storage->get($session,$image1);
$var{thumbnail1} = $self->getThumbnailUrl($file);
$var{image1} = $self->getFileUrl($file);
}
#---image2
if ($image2) {
my $file = WebGUI::Storage::Image->get($session,$image2);
my $file = WebGUI::Storage->get($session,$image2);
$var{thumbnail2} = $self->getThumbnailUrl($file);
$var{image2} = $self->getFileUrl($file);
}
#---image3
if ($image3) {
my $file = WebGUI::Storage::Image->get($session,$image3);
my $file = WebGUI::Storage->get($session,$image3);
$var{thumbnail3} = $self->getThumbnailUrl($file);
$var{image3} = $self->getFileUrl($file);
}

View file

@ -579,7 +579,7 @@ sub www_styleWizard {
my $logo;
my $logoContent = '';
if ($storageId) {
my $storage = WebGUI::Storage::Image->get($self->session,$storageId);
my $storage = WebGUI::Storage->get($self->session,$storageId);
$logo = $self->addChild({
className=>"WebGUI::Asset::File::Image",
title=>join(' ', $form->get("heading"), $i18n->get('logo')),

View file

@ -16,7 +16,7 @@ use WebGUI::International;
use WebGUI::Cache;
use WebGUI::Paginator;
use WebGUI::Asset::Wobject;
use WebGUI::Storage::Image;
use WebGUI::Storage;
our @ISA = qw(WebGUI::Asset::Wobject);
@ -173,10 +173,10 @@ sub getStorageLocation {
my $self = shift;
unless (exists $self->{_storageLocation}) {
if ($self->get("storageId") eq "") {
$self->{_storageLocation} = WebGUI::Storage::Image->create($self->session);
$self->{_storageLocation} = WebGUI::Storage->create($self->session);
$self->update({storageId=>$self->{_storageLocation}->getId});
} else {
$self->{_storageLocation} = WebGUI::Storage::Image->get($self->session,$self->get("storageId"));
$self->{_storageLocation} = WebGUI::Storage->get($self->session,$self->get("storageId"));
}
}
return $self->{_storageLocation};
@ -240,7 +240,7 @@ sub purge {
my $self = shift;
my $sth = $self->session->db->read("select storageId from Article where assetId=?",[$self->getId]);
while (my ($storageId) = $sth->array) {
my $storage = WebGUI::Storage::Image->get($self->session,$storageId);
my $storage = WebGUI::Storage->get($self->session,$storageId);
$storage->delete if defined $storage;
}
$sth->finish;

View file

@ -20,7 +20,7 @@ use WebGUI::User;
use WebGUI::Utility;
use WebGUI::Asset::Wobject;
use WebGUI::Image::Graph;
use WebGUI::Storage::Image;
use WebGUI::Storage;
use JSON;
our @ISA = qw(WebGUI::Asset::Wobject);
@ -461,7 +461,7 @@ sub view {
$graph->draw;
my $storage = WebGUI::Storage::Image->createTemp($self->session);
my $storage = WebGUI::Storage->createTemp($self->session);
my $filename = 'poll'.$self->session->id->generate.".png";
$graph->saveToStorageLocation($storage, $filename);

View file

@ -18,7 +18,7 @@ use WebGUI::Auth;
use WebGUI::HTMLForm;
use WebGUI::Macro;
use WebGUI::Mail::Send;
use WebGUI::Storage::Image;
use WebGUI::Storage;
use WebGUI::User;
use WebGUI::Utility;
use Encode ();

View file

@ -19,7 +19,7 @@ use Digest::MD5;
use WebGUI::Asset;
use WebGUI::HTMLForm;
use WebGUI::International;
use WebGUI::Storage::Image;
use WebGUI::Storage;
use WebGUI::VersionTag;
use WebGUI::Utility;
@ -173,7 +173,7 @@ sub handler {
my $url = $session->url;
my $logoUrl = $url->extras("plainblack.gif");
if (defined $storageId) {
my $storage = WebGUI::Storage::Image->get($session, $storageId);
my $storage = WebGUI::Storage->get($session, $storageId);
my $importNode = WebGUI::Asset->getImportNode($session);
my $logo = addAsset($importNode, {
title => $storage->getFiles->[0],

View file

@ -18,7 +18,7 @@ use strict;
use base 'WebGUI::Form::Control';
use WebGUI::Asset;
use WebGUI::International;
use WebGUI::Storage::Image;
use WebGUI::Storage;
use WebGUI::VersionTag;
=head1 NAME
@ -287,7 +287,7 @@ sub www_upload {
my $session = shift;
my $form = $session->form;
my @assetIds = $form->param("attachments");
my $storage = WebGUI::Storage::Image->createTemp($session);
my $storage = WebGUI::Storage->createTemp($session);
my $filename = $storage->addFileFromFormPost("attachment");
my $tempspace = WebGUI::Asset->getTempspace($session);
my $asset = "";

View file

@ -17,7 +17,7 @@ package WebGUI::Form::Captcha;
use strict;
use base 'WebGUI::Form::Text';
use WebGUI::International;
use WebGUI::Storage::Image;
use WebGUI::Storage;
=head1 NAME
@ -134,7 +134,7 @@ Renders a captcha field.
sub toHtml {
my $self = shift;
my $storage = WebGUI::Storage::Image->createTemp($self->session);
my $storage = WebGUI::Storage->createTemp($self->session);
my ($filename, $challenge) = $storage->addFileFromCaptcha;
$self->set("size", 6);
$self->set("maxlength", 6);

View file

@ -188,10 +188,10 @@ sub getValue {
elsif ($self->session->form->param($self->privateName('action')) eq 'upload') {
my $storage = undef;
if ($value ne "") {
$storage = WebGUI::Storage::Image->get($self->session, $value);
$storage = WebGUI::Storage->get($self->session, $value);
}
else {
$storage = WebGUI::Storage::Image->create($self->session);
$storage = WebGUI::Storage->create($self->session);
}
$storage->addFileFromFormPost($self->get("name")."_file",1000);
my @files = @{ $storage->getFiles };

View file

@ -17,7 +17,7 @@ package WebGUI::Form::Image;
use strict;
use base 'WebGUI::Form::File';
use WebGUI::International;
use WebGUI::Storage::Image;
use WebGUI::Storage;
use WebGUI::Form::YesNo;
use WebGUI::Utility qw/isIn/;
@ -134,7 +134,7 @@ Returns the WebGUI::Storage object for this control.
sub getStorageLocation {
my $self = shift;
my $value = $self->getOriginalValue;
my $storage = WebGUI::Storage::Image->get($self->session, $value) if ($value);
my $storage = WebGUI::Storage->get($self->session, $value) if ($value);
return $storage;
}
@ -151,7 +151,7 @@ sub getValue {
my $self = shift;
my $id = $self->SUPER::getValue(@_);
if (defined $id) {
my $storage = WebGUI::Storage::Image->get($self->session, $id);
my $storage = WebGUI::Storage->get($self->session, $id);
if (defined $storage) {
my @files = @{$storage->getFiles};
my @images = grep{$storage->isImage($_)} @files; # Put all filenames that isImage returns true for into @images
@ -184,7 +184,7 @@ sub getValueAsHtml {
my ($self) = @_;
my $value = $self->getOriginalValue;
return '' unless $value;
my $location = WebGUI::Storage::Image->get($self->session, $value);
my $location = WebGUI::Storage->get($self->session, $value);
my $file = shift @{ $location->getFiles };
my $fileValue = sprintf qq|<img src="%s" />&nbsp;%s|, $location->getUrl($file), $file;
return $fileValue;

View file

@ -373,7 +373,7 @@ Save the image to the specified storage location.
=head3 storage
An instanciated WebGUI::Storage::Image object.
An instanciated WebGUI::Storage object.
=head3 filename

View file

@ -58,8 +58,8 @@ sub process {
my $filename = shift;
my $output = "";
# Use WebGUI::Storage::Image because we might be getting an image
my $storage = WebGUI::Storage::Image->get( $session, $storageId );
# Use WebGUI::Storage because we might be getting an image
my $storage = WebGUI::Storage->get( $session, $storageId );
return "" if !$storage;
if ( !$filename ) {

View file

@ -16,7 +16,7 @@ use WebGUI::AdSpace::Ad;
use WebGUI::AdminConsole;
use WebGUI::International;
use WebGUI::HTMLForm;
use WebGUI::Storage::Image;
use WebGUI::Storage;
=head1 NAME
@ -214,7 +214,7 @@ sub www_editAd {
name=>"image"
);
if (defined $ad && $ad->get("storageId")) {
my $storage = WebGUI::Storage::Image->get($session, $ad->get("storageId"));
my $storage = WebGUI::Storage->get($session, $ad->get("storageId"));
$f->readOnly(value=>'<img src="'.$storage->getUrl($storage->getFiles->[0]).'" style="border: 0px;" />');
}
$f->fieldSetEnd;
@ -265,7 +265,7 @@ sub www_editAdSave {
} else {
my $ad = WebGUI::AdSpace::Ad->new($session, $session->form->param("adId"));
if (defined $storageId && $ad->get("storageId")) {
WebGUI::Storage::Image->get($session, $ad->get("storageId"))->delete;
WebGUI::Storage->get($session, $ad->get("storageId"))->delete;
}
$ad->set(\%properties);
}

View file

@ -17,7 +17,7 @@ use WebGUI::Asset::Wobject::Folder;
use WebGUI::Form::Group;
use WebGUI::HTMLForm;
use WebGUI::Pluggable;
use WebGUI::Storage::Image;
use WebGUI::Storage;
use WebGUI::Utility;
=head1 NAME

View file

@ -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;
}
#-------------------------------------------------------------------

View file

@ -15,24 +15,7 @@ package WebGUI::Storage::Image;
=cut
use strict;
use WebGUI::Storage;
use WebGUI::Utility;
use Carp qw(croak);
my $graphicsPackage;
BEGIN {
if (eval { require Graphics::Magick; 1 }) {
$graphicsPackage = 'Graphics::Magick';
}
elsif (eval { require Image::Magick; 1 }) {
$graphicsPackage = 'Image::Magick';
}
else {
croak "You must have either Graphics::Magick or Image::Magick installed to run WebGUI.\n";
}
}
our @ISA = qw(WebGUI::Storage);
use base 'WebGUI::Storage';
=head1 NAME
@ -41,383 +24,13 @@ Package WebGUI::Storage::Image
=head1 DESCRIPTION
Extends WebGUI::Storage to add image manipulation operations.
=head1 SYNOPSIS
use WebGUI::Storage::Image;
=head1 METHODS
These methods are available from this class:
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);
The use of this module is deprecated and is only here for backward compatibility. Use WebGUI::Storage instead.
=cut
#-------------------------------------------------------------------
=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 = $graphicsPackage->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 = $graphicsPackage eq 'Image::Magick' ? '#222222' : '#666666';
$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 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 copy ( [ storage ] )
Overriding the copy method so that thumbnail files are copied along with other image files
=head3 storage
Optionally pass a storage object to copy the files to.
=cut
sub copy {
my $self = shift;
my $newStorage = shift || WebGUI::Storage::Image->create($self->session);
# Storage::Image->getFiles excludes thumbnails from the filelist and we want to copy the thumbnails
my $filelist = $self->SUPER::getFiles(1);
return $self->SUPER::copy($newStorage, $filelist);
}
#-------------------------------------------------------------------
=head2 deleteFile ( filename )
Deletes the thumbnail for a file and the file from its storage location
=head3 filename
The name of the file to delete.
=cut
sub deleteFile {
my $self = shift;
my $filename = shift;
$self->SUPER::deleteFile('thumb-'.$filename);
$self->SUPER::deleteFile($filename);
}
#-------------------------------------------------------------------
=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 = $graphicsPackage->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 getFiles ( )
Returns an array reference of the files in this storage location.
=cut
sub getFiles {
my $self = shift;
my $files = $self->SUPER::getFiles(@_);
my @newFiles;
foreach my $file (@{$files}) {
next if $file =~ /^thumb-/;
push (@newFiles,$file);
}
return \@newFiles;
}
#-------------------------------------------------------------------
=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 = $graphicsPackage->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);
}
#-------------------------------------------------------------------
=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 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 = $graphicsPackage->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;
}
1;