adding image asset type
This commit is contained in:
parent
fd76365c1b
commit
17177f68e6
2 changed files with 211 additions and 0 deletions
|
|
@ -177,3 +177,10 @@ CREATE TABLE commerceSettings (
|
|||
type varchar(10) NOT NULL default ''
|
||||
) TYPE=MyISAM;
|
||||
INSERT INTO template VALUES ('1','Default payment gateway selection template','<tmpl_if pluginsAvailable>\r\n <tmpl_var message><br>\r\n <tmpl_var formHeader>\r\n <table border=\"0\" cellspacing=\"0\" cellpadding=\"5\">\r\n <tmpl_loop pluginLoop>\r\n <tr>\r\n <td><tmpl_var formElement></td>\r\n <td align=\"left\"><tmpl_var name></td>\r\n </tr>\r\n </tmpl_loop>\r\n </table>\r\n <tmpl_var formSubmit>\r\n <tmpl_var formFooter>\r\n<tmpl_else>\r\n <tmpl_var noPluginsMessage>\r\n</tmpl_if>','Commerce/SelectPaymentGateway',1,1);
|
||||
|
||||
|
||||
create table ImageAsset (
|
||||
assetId varchar(22) not null primary key,
|
||||
thumbnailSize int not null default 50
|
||||
);
|
||||
|
||||
|
|
|
|||
204
lib/WebGUI/Asset/File/Image.pm
Normal file
204
lib/WebGUI/Asset/File/Image.pm
Normal file
|
|
@ -0,0 +1,204 @@
|
|||
package WebGUI::Asset::File::Image;
|
||||
|
||||
=head1 LEGAL
|
||||
|
||||
-------------------------------------------------------------------
|
||||
WebGUI is Copyright 2001-2004 Plain Black Corporation.
|
||||
-------------------------------------------------------------------
|
||||
Please read the legal notices (docs/legal.txt) and the license
|
||||
(docs/license.txt) that came with this distribution before using
|
||||
this software.
|
||||
-------------------------------------------------------------------
|
||||
http://www.plainblack.com info@plainblack.com
|
||||
-------------------------------------------------------------------
|
||||
|
||||
=cut
|
||||
|
||||
use strict;
|
||||
use WebGUI::Asset::File;
|
||||
use WebGUI::HTTP;
|
||||
use WebGUI::Session;
|
||||
use WebGUI::Storage;
|
||||
|
||||
# do a check to see if they've installed Image::Magick
|
||||
my $hasImageMagick = 1;
|
||||
eval " use Image::Magick; "; $hasImageMagick=0 if $@;
|
||||
|
||||
our @ISA = qw(WebGUI::Asset::File);
|
||||
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Package WebGUI::Asset::File::Image
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
Extends WebGUI::Asset::File to add image manipulation operations.
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
use WebGUI::Asset::File::Image;
|
||||
|
||||
|
||||
=head1 METHODS
|
||||
|
||||
These methods are available from this class:
|
||||
|
||||
=cut
|
||||
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 definition ( definition )
|
||||
|
||||
Defines the properties of this asset.
|
||||
|
||||
=head3 definition
|
||||
|
||||
A hash reference passed in from a subclass definition.
|
||||
|
||||
=cut
|
||||
|
||||
sub definition {
|
||||
my $class = shift;
|
||||
my $definition = shift;
|
||||
push(@{$definition}, {
|
||||
tableName=>'ImageAsset',
|
||||
className=>'WebGUI::Asset::File::Image',
|
||||
properties=>{
|
||||
thumbnailSize=>{
|
||||
fieldType=>'integer',
|
||||
defaultValue=>$session{setting}{thumbnailSize}
|
||||
}
|
||||
}
|
||||
});
|
||||
return $class->SUPER::definition($definition);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 generateThumbnail ( [ thumbnailSize ] )
|
||||
|
||||
Generates a thumbnail for this image.
|
||||
|
||||
=head3 thumbnailSize
|
||||
|
||||
A size, in pixels, of the maximum height or width of a thumbnail. If specified this will change the thumbnail size of the image. If unspecified the thumbnail size set in the properties of this asset will be used.
|
||||
|
||||
=cut
|
||||
|
||||
sub generateThumbnail {
|
||||
my $self = shift;
|
||||
my $thumbnailSize = shift;
|
||||
if (defined $thumbnailSize) {
|
||||
$self->update({thumbnailSize=>$thumbnailSize});
|
||||
}
|
||||
if ($self->getValue("filename") && $hasImageMagick) {
|
||||
my $storage = WebGUI::Storage->new($self->get("storageId"));
|
||||
$image = Image::Magick->new;
|
||||
$error = $image->Read($storage->getPath($storage->get("filename")));
|
||||
if ($error) {
|
||||
$self->_addError("Couldn't read image for thumnail creation: ".$error);
|
||||
return 0;
|
||||
}
|
||||
($x, $y) = $image->Get('width','height');
|
||||
$n = $self->get("thumbnailSize");
|
||||
if ($x > $n || $y > $n) {
|
||||
$r = $x>$y ? $x / $n : $y / $n;
|
||||
$image->Scale(width=>($x/$r),height=>($y/$r));
|
||||
}
|
||||
if (isIn($storage->getFileExtension($self->get("filename")), qw(tif tiff bmp))) {
|
||||
$error = $image->Write($storage->getPath.$session{os}{slash}.'thumb-'.$self->get("filename").'.png');
|
||||
} else {
|
||||
$error = $image->Write($storage->getPath.$session{os}{slash}.'thumb-'.$self->get("filename"));
|
||||
}
|
||||
if ($error) {
|
||||
$self->_addError("Couldn't create thumbnail: ".$error);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
$self->_addError("Can't generate a thumbnail when you haven't uploaded a file.");
|
||||
return 0; # couldn't generate thumbnail
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 getEditForm ()
|
||||
|
||||
Returns the TabForm object that will be used in generating the edit page for this asset.
|
||||
|
||||
=cut
|
||||
|
||||
sub getEditForm {
|
||||
my $self = shift;
|
||||
my $tabform = $self->SUPER::getEditForm();
|
||||
$tabform->getTab("properties")->integer(
|
||||
-name=>"thumbnailSize",
|
||||
-label=>"Thumbnail Size",
|
||||
-value=>$self->getValue("thumbnailSize")
|
||||
);
|
||||
if ($self->get("filename") ne "") {
|
||||
my $storage = WebGUI::Storage->new($self->get("storageId"));
|
||||
$tabform->getTab("properties")->readOnly(
|
||||
-label=>"Thumbnail",
|
||||
-value=>'<img src="'.$storage->getUrl($self->get("filename")).'" alt="thumbnail" />'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 getName
|
||||
|
||||
Returns the displayable name of this asset.
|
||||
|
||||
=cut
|
||||
|
||||
sub getName {
|
||||
return "Image";
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 www_editSave
|
||||
|
||||
Gathers data from www_edit and persists it.
|
||||
|
||||
=cut
|
||||
|
||||
sub www_editSave {
|
||||
my $self = shift;
|
||||
$self->SUPER::www_editSave();
|
||||
$self->generateThumbnail;
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 www_view
|
||||
|
||||
A web executable method that redirects the user to the specified page, or displays the edit interface when admin mode is enabled.
|
||||
|
||||
=cut
|
||||
|
||||
sub www_view {
|
||||
my $self = shift;
|
||||
if ($session{var}{adminOn}) {
|
||||
return $self->www_edit;
|
||||
}
|
||||
my $storage = WebGUI::Storage->new($self->get("storageId"));
|
||||
WebGUI::HTTP::setRedirect($storage->getUrl($self->get("filename")));
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
1;
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue