Skip to content

Commit

Permalink
Refactored thumbnail for gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
kthakore committed May 24, 2011
1 parent 24d7bd0 commit a05e817
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/WebGUI/Graphics.pm
Expand Up @@ -6,6 +6,9 @@ use strict;
use warnings;
use Carp qw( croak );
use Image::Magick;
use GD;
use GD::Thumbnail;


sub new
{
Expand Down Expand Up @@ -95,6 +98,43 @@ sub Annotate
my $self = shift; return $self->{core}->Annotate( @_ );
}

sub genThumbnailRefactor {

my $filename = shift;
my $thumbnailSize = shift;
my $thumbname = shift;

# Open the image or return the error message
my $image = GD::Image->new( $filename ) || return "Couldn't read image for thumbnail creation: $!";
my ($x, $y) = $image->getBounds();
my $thumb = GD::Thumbnail->new || return $!;
my $raw = $image->gd;
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 }
$raw = $thumb->create( $filename, $x, $y);
}

my $error = write_gd( $thumbname, $raw );
return $error;
}

sub write_gd
{
my $filename = shift;
my $raw = shift;

open IMG, ">$filename" or return $!;
binmode IMG;
print IMG $raw;
close IMG;

return 0;
}

1;

8 changes: 8 additions & 0 deletions lib/WebGUI/Storage.pm
Expand Up @@ -827,8 +827,13 @@ sub generateThumbnail {
$self->session->log->warn("Can't generate a thumbnail for something that's not an image.");
return 0;
}
=pod
my $image = WebGUI::Graphics->new;
my $error = $image->Read($self->getPath($filename));
=cut
my $error = WebGUI::Graphics::genThumbnailRefactor( $self->getPath($filename), $thumbnailSize, $self->getPath.'/'.'thumb-'.$filename );
=pod
if ($error) {
$self->session->log->error("Couldn't read image for thumbnail creation: ".$error);
return 0;
Expand All @@ -845,11 +850,14 @@ sub generateThumbnail {
$image->Sharpen('0.0x1.0');
}
$error = $image->Write($self->getPath.'/'.'thumb-'.$filename);
=cut

if ($error) {
$self->session->log->error("Couldn't create thumbnail: ".$error);
return 0;
}
return 1;

}


Expand Down

0 comments on commit a05e817

Please sign in to comment.