change image resize to AssetHelper
This commit is contained in:
parent
4adaa093a5
commit
b84fd2adaf
2 changed files with 212 additions and 129 deletions
|
|
@ -171,6 +171,26 @@ override getEditForm => sub {
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
|
=head2 getHelpers ( )
|
||||||
|
|
||||||
|
Add the image helpers
|
||||||
|
|
||||||
|
=cut
|
||||||
|
|
||||||
|
override getHelpers => sub {
|
||||||
|
my ( $self ) = @_;
|
||||||
|
|
||||||
|
my $helpers = super();
|
||||||
|
$helpers->{resize} = {
|
||||||
|
className => 'WebGUI::AssetHelper::Image::Resize',
|
||||||
|
label => 'Resize Image',
|
||||||
|
};
|
||||||
|
|
||||||
|
return $helpers;
|
||||||
|
};
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
=head2 getThumbnailUrl
|
=head2 getThumbnailUrl
|
||||||
|
|
||||||
Returns the URL to the thumbnail of the image stored in the Asset.
|
Returns the URL to the thumbnail of the image stored in the Asset.
|
||||||
|
|
@ -255,31 +275,30 @@ override setFile => sub {
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
=head2 www_edit
|
#=head2 www_edit
|
||||||
|
|
||||||
Override the master class to add image editing controls to the edit screen.
|
#Override the master class to add image editing controls to the edit screen.
|
||||||
Also adds the Image template form variable.
|
#Also adds the Image template form variable.
|
||||||
|
|
||||||
=cut
|
#=cut
|
||||||
|
|
||||||
sub www_edit {
|
#sub www_edit {
|
||||||
my $self = shift;
|
# my $self = shift;
|
||||||
my $session = $self->session;
|
# my $session = $self->session;
|
||||||
return $session->privilege->insufficient() unless $self->canEdit;
|
# return $session->privilege->insufficient() unless $self->canEdit;
|
||||||
return $session->privilege->locked() unless $self->canEditIfLocked;
|
# return $session->privilege->locked() unless $self->canEditIfLocked;
|
||||||
my $i18n = WebGUI::International->new($session, 'Asset_Image');
|
# my $i18n = WebGUI::International->new($session, 'Asset_Image');
|
||||||
if ($self->filename) {
|
# if ($self->filename) {
|
||||||
my $ac = $self->getAdminConsole;
|
# my $ac = $self->getAdminConsole;
|
||||||
# These are asset helpers now, not functions
|
# These are asset helpers now, not functions
|
||||||
$ac->addSubmenuItem($self->getUrl('func=resize'), $i18n->get("resize image"));
|
# $ac->addSubmenuItem($self->getUrl('func=rotate'), $i18n->get("rotate image"));
|
||||||
$ac->addSubmenuItem($self->getUrl('func=rotate'), $i18n->get("rotate image"));
|
# $ac->addSubmenuItem($self->getUrl('func=crop'), $i18n->get("crop image"));
|
||||||
$ac->addSubmenuItem($self->getUrl('func=crop'), $i18n->get("crop image"));
|
# $ac->addSubmenuItem($self->getUrl('func=annotate'), $i18n->get("annotate image"));
|
||||||
$ac->addSubmenuItem($self->getUrl('func=annotate'), $i18n->get("annotate image"));
|
# $ac->addSubmenuItem($self->getUrl('func=undo'), $i18n->get("undo image"));
|
||||||
$ac->addSubmenuItem($self->getUrl('func=undo'), $i18n->get("undo image"));
|
# }
|
||||||
}
|
# my $tabform = $self->getEditForm;
|
||||||
my $tabform = $self->getEditForm;
|
# return $self->getAdminConsole->render($tabform->toHtml,$i18n->get("edit image"));
|
||||||
return $self->getAdminConsole->render($tabform->toHtml,$i18n->get("edit image"));
|
#}
|
||||||
}
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
@ -587,113 +606,6 @@ sub www_rotate {
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
=head2 www_resize
|
|
||||||
|
|
||||||
Displays a form for the user to resize this image. If either of the C<newWidth> or
|
|
||||||
C<newHeight> form variables are true, also does the resizing.
|
|
||||||
|
|
||||||
Returns the user to the resize form.
|
|
||||||
|
|
||||||
=cut
|
|
||||||
|
|
||||||
sub www_resize {
|
|
||||||
my $self = shift;
|
|
||||||
my $session = $self->session;
|
|
||||||
return $session->privilege->insufficient() unless $self->canEdit;
|
|
||||||
return $session->privilege->locked() unless $self->canEditIfLocked;
|
|
||||||
if ($session->form->process("newWidth") || $session->form->process("newHeight")) {
|
|
||||||
my $tag = WebGUI::VersionTag->getWorking( $session );
|
|
||||||
my $newSelf = $self->addRevision({ tagId => $tag->getId, status => "pending" });
|
|
||||||
$newSelf->setVersionLock;
|
|
||||||
delete $newSelf->{_storageLocation};
|
|
||||||
$newSelf->getStorageLocation->resize($newSelf->filename,$session->form->process("newWidth"),$session->form->process("newHeight"));
|
|
||||||
$newSelf->setSize($newSelf->getStorageLocation->getFileSize($newSelf->filename));
|
|
||||||
$self = $newSelf;
|
|
||||||
$self->generateThumbnail;
|
|
||||||
WebGUI::VersionTag->autoCommitWorkingIfEnabled($session, { allowComments => 0 });
|
|
||||||
}
|
|
||||||
|
|
||||||
my ($x, $y) = $self->getStorageLocation->getSizeInPixels($self->filename);
|
|
||||||
|
|
||||||
##YUI specific datatable CSS
|
|
||||||
my ($style, $url) = $session->quick(qw(style url));
|
|
||||||
|
|
||||||
$style->setCss($url->extras('yui/build/fonts/fonts-min.css'));
|
|
||||||
$style->setCss($url->extras('yui/build/resize/assets/skins/sam/resize.css'));
|
|
||||||
$style->setScript($url->extras('yui/build/yahoo-dom-event/yahoo-dom-event.js'));
|
|
||||||
$style->setScript($url->extras('yui/build/element/element-min.js'));
|
|
||||||
$style->setScript($url->extras('yui/build/dragdrop/dragdrop-min.js'));
|
|
||||||
$style->setScript($url->extras('yui/build/resize/resize-min.js'));
|
|
||||||
$style->setScript($url->extras('yui/build/animation/animation-min.js'));
|
|
||||||
|
|
||||||
my $resize_js = qq(
|
|
||||||
<script>
|
|
||||||
(function() {
|
|
||||||
var Dom = YAHOO.util.Dom,
|
|
||||||
Event = YAHOO.util.Event;
|
|
||||||
|
|
||||||
var resize = new YAHOO.util.Resize('yui_img', {
|
|
||||||
handles: 'all',
|
|
||||||
knobHandles: true,
|
|
||||||
height: '${y}px',
|
|
||||||
width: '${x}px',
|
|
||||||
proxy: true,
|
|
||||||
ghost: true,
|
|
||||||
status: true,
|
|
||||||
draggable: false,
|
|
||||||
ratio: true,
|
|
||||||
animate: true,
|
|
||||||
animateDuration: .75,
|
|
||||||
animateEasing: YAHOO.util.Easing.backBoth
|
|
||||||
});
|
|
||||||
|
|
||||||
resize.on('startResize', function() {
|
|
||||||
this.getProxyEl().innerHTML = '<img src="' + this.get('element').src + '" style="height: 100%; width: 100%;">';
|
|
||||||
Dom.setStyle(this.getProxyEl().firstChild, 'opacity', '.25');
|
|
||||||
}, resize, true);
|
|
||||||
|
|
||||||
resize.on('resize', function(e) {
|
|
||||||
element = document.getElementById('newWidth_formId');
|
|
||||||
element.value = e.width;
|
|
||||||
|
|
||||||
element = document.getElementById('newHeight_formId');
|
|
||||||
element.value = e.height;
|
|
||||||
}, resize, true);
|
|
||||||
})();
|
|
||||||
</script>
|
|
||||||
);
|
|
||||||
|
|
||||||
my $i18n = WebGUI::International->new($session,"Asset_Image");
|
|
||||||
$self->getAdminConsole->addSubmenuItem($self->getUrl('func=edit'),$i18n->get("edit image"));
|
|
||||||
my $f = WebGUI::FormBuilder->new($session);
|
|
||||||
$f->addField( "hidden",
|
|
||||||
-name=>"func",
|
|
||||||
-value=>"resize"
|
|
||||||
);
|
|
||||||
$f->addField( "readOnly",
|
|
||||||
-label=>$i18n->get('image size'),
|
|
||||||
-hoverHelp=>$i18n->get('image size description'),
|
|
||||||
-value=>$x.' x '.$y,
|
|
||||||
);
|
|
||||||
$f->addField( "integer",
|
|
||||||
-label=>$i18n->get('new width'),
|
|
||||||
-hoverHelp=>$i18n->get('new width description'),
|
|
||||||
-name=>"newWidth",
|
|
||||||
-value=>$x,
|
|
||||||
);
|
|
||||||
$f->addField( "integer",
|
|
||||||
-label=>$i18n->get('new height'),
|
|
||||||
-hoverHelp=>$i18n->get('new height description'),
|
|
||||||
-name=>"newHeight",
|
|
||||||
-value=>$y,
|
|
||||||
);
|
|
||||||
$f->addField( "submit", name => "submit" );
|
|
||||||
my $image = '<div align="center" class="yui-skin-sam"><img src="'.$self->getStorageLocation->getUrl($self->filename).'" style="border-style:none;" alt="'.$self->filename.'" id="yui_img" /></div>'.$resize_js;
|
|
||||||
return $self->getAdminConsole->render($f->toHtml.$image,$i18n->get("resize image"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
|
||||||
|
|
||||||
=head2 www_crop
|
=head2 www_crop
|
||||||
|
|
||||||
Display a form that allows the user to Crop their images. Also does the
|
Display a form that allows the user to Crop their images. Also does the
|
||||||
|
|
|
||||||
171
lib/WebGUI/AssetHelper/Image/Resize.pm
Normal file
171
lib/WebGUI/AssetHelper/Image/Resize.pm
Normal file
|
|
@ -0,0 +1,171 @@
|
||||||
|
package WebGUI::AssetHelper::Image::Resize;
|
||||||
|
|
||||||
|
use base 'WebGUI::AssetHelper';
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
|
=head2 process ( )
|
||||||
|
|
||||||
|
Open a dialog to resize the image
|
||||||
|
|
||||||
|
=cut
|
||||||
|
|
||||||
|
sub process {
|
||||||
|
my ( $self ) = @_;
|
||||||
|
my $asset = $self->asset;
|
||||||
|
my $session = $self->session;
|
||||||
|
|
||||||
|
my $i18n = WebGUI::International->new($session, 'WebGUI');
|
||||||
|
if (! $asset->canEdit) {
|
||||||
|
return { error => $i18n->get('38'), };
|
||||||
|
}
|
||||||
|
elsif ( ! $asset->canEditIfLocked ) {
|
||||||
|
return { error => $i18n->get('asset locked') };
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
openDialog => $self->getUrl( 'resize' )
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
|
=head2 www_resize
|
||||||
|
|
||||||
|
Displays a form for the user to resize this image.
|
||||||
|
|
||||||
|
=cut
|
||||||
|
|
||||||
|
sub www_resize {
|
||||||
|
my ( $self ) = @_;
|
||||||
|
my $asset = $self->asset;
|
||||||
|
my $session = $self->session;
|
||||||
|
return $session->privilege->insufficient() unless $asset->canEdit;
|
||||||
|
return $session->privilege->locked() unless $asset->canEditIfLocked;
|
||||||
|
|
||||||
|
my ( $x, $y ) = $asset->getStorageLocation->getSizeInPixels( $asset->filename );
|
||||||
|
|
||||||
|
##YUI specific datatable CSS
|
||||||
|
my ( $style, $url ) = $session->quick(qw(style url));
|
||||||
|
|
||||||
|
$style->setCss( $url->extras('yui/build/fonts/fonts-min.css') );
|
||||||
|
$style->setCss( $url->extras('yui/build/resize/assets/skins/sam/resize.css') );
|
||||||
|
$style->setScript( $url->extras('yui/build/yahoo-dom-event/yahoo-dom-event.js') );
|
||||||
|
$style->setScript( $url->extras('yui/build/element/element-min.js') );
|
||||||
|
$style->setScript( $url->extras('yui/build/dragdrop/dragdrop-min.js') );
|
||||||
|
$style->setScript( $url->extras('yui/build/resize/resize-min.js') );
|
||||||
|
$style->setScript( $url->extras('yui/build/animation/animation-min.js') );
|
||||||
|
|
||||||
|
my $resize_js = qq(
|
||||||
|
<script>
|
||||||
|
(function() {
|
||||||
|
var Dom = YAHOO.util.Dom,
|
||||||
|
Event = YAHOO.util.Event;
|
||||||
|
|
||||||
|
var resize = new YAHOO.util.Resize('yui_img', {
|
||||||
|
handles: 'all',
|
||||||
|
knobHandles: true,
|
||||||
|
height: '${y}px',
|
||||||
|
width: '${x}px',
|
||||||
|
proxy: true,
|
||||||
|
ghost: true,
|
||||||
|
status: true,
|
||||||
|
draggable: false,
|
||||||
|
ratio: true,
|
||||||
|
animate: true,
|
||||||
|
animateDuration: .75,
|
||||||
|
animateEasing: YAHOO.util.Easing.backBoth
|
||||||
|
});
|
||||||
|
|
||||||
|
resize.on('startResize', function() {
|
||||||
|
this.getProxyEl().innerHTML = '<img src="' + this.get('element').src + '" style="height: 100%; width: 100%;">';
|
||||||
|
Dom.setStyle(this.getProxyEl().firstChild, 'opacity', '.25');
|
||||||
|
}, resize, true);
|
||||||
|
|
||||||
|
resize.on('resize', function(e) {
|
||||||
|
element = document.getElementById('newWidth_formId');
|
||||||
|
element.value = e.width;
|
||||||
|
|
||||||
|
element = document.getElementById('newHeight_formId');
|
||||||
|
element.value = e.height;
|
||||||
|
}, resize, true);
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
);
|
||||||
|
|
||||||
|
my $i18n = WebGUI::International->new( $session, "Asset_Image" );
|
||||||
|
my $f = $self->getForm( 'resizeSave' );
|
||||||
|
$f->addField(
|
||||||
|
"readOnly",
|
||||||
|
label => $i18n->get('image size'),
|
||||||
|
hoverHelp => $i18n->get('image size description'),
|
||||||
|
value => $x . ' x ' . $y,
|
||||||
|
);
|
||||||
|
$f->addField(
|
||||||
|
"integer",
|
||||||
|
label => $i18n->get('new width'),
|
||||||
|
hoverHelp => $i18n->get('new width description'),
|
||||||
|
name => "newWidth",
|
||||||
|
value => $x,
|
||||||
|
);
|
||||||
|
$f->addField(
|
||||||
|
"integer",
|
||||||
|
label => $i18n->get('new height'),
|
||||||
|
hoverHelp => $i18n->get('new height description'),
|
||||||
|
name => "newHeight",
|
||||||
|
value => $y,
|
||||||
|
);
|
||||||
|
$f->addField( "submit", name => "submit" );
|
||||||
|
my $image
|
||||||
|
= '<div align="center" class="yui-skin-sam"><img src="'
|
||||||
|
. $asset->getStorageLocation->getUrl( $asset->filename )
|
||||||
|
. '" style="border-style:none;" alt="'
|
||||||
|
. $asset->filename
|
||||||
|
. '" id="yui_img" /></div>'
|
||||||
|
. $resize_js;
|
||||||
|
my $output = '<h1>' . $i18n->get('resize image') . '</h1>' . $f->toHtml . $image;
|
||||||
|
return $style->process( $output, "PBtmplBlankStyle000001" );
|
||||||
|
} ## end sub www_resize
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
=head2 www_resizeSave ( )
|
||||||
|
|
||||||
|
Resize the image to the user's specifications and close the dialog
|
||||||
|
|
||||||
|
=cut
|
||||||
|
|
||||||
|
sub www_resizeSave {
|
||||||
|
my ( $self ) = @_;
|
||||||
|
my $asset = $self->asset;
|
||||||
|
my $session = $self->session;
|
||||||
|
return $session->privilege->insufficient() unless $asset->canEdit;
|
||||||
|
return $session->privilege->locked() unless $asset->canEditIfLocked;
|
||||||
|
my $tag = WebGUI::VersionTag->getWorking($session);
|
||||||
|
$asset = $asset->addRevision( { tagId => $tag->getId, status => "pending" } );
|
||||||
|
$asset->setVersionLock;
|
||||||
|
delete $asset->{_storageLocation};
|
||||||
|
$asset->getStorageLocation->resize(
|
||||||
|
$asset->filename,
|
||||||
|
$session->form->process("newWidth"),
|
||||||
|
$session->form->process("newHeight")
|
||||||
|
);
|
||||||
|
$asset->setSize( $asset->getStorageLocation->getFileSize( $asset->filename ) );
|
||||||
|
$asset->generateThumbnail;
|
||||||
|
WebGUI::VersionTag->autoCommitWorkingIfEnabled( $session, { allowComments => 0 } );
|
||||||
|
# We're in admin mode, close the dialog
|
||||||
|
my $text = '<script type="text/javascript">';
|
||||||
|
|
||||||
|
if ( ref $helper eq 'HASH' ) {
|
||||||
|
# Process the output as JSON
|
||||||
|
$text .= sprintf 'parent.admin.processPlugin( %s );', JSON->new->encode( $helper );
|
||||||
|
}
|
||||||
|
|
||||||
|
# Close dialog last so that script above runs!
|
||||||
|
$text .= 'parent.admin.closeModalDialog();'
|
||||||
|
. '</script>';
|
||||||
|
|
||||||
|
$self->session->output->print( $text, 1); # skipMacros
|
||||||
|
}
|
||||||
|
|
||||||
|
1;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue