Bootylicious-Plugin-Gallery

 view release on metacpan or  search on metacpan

lib/Bootylicious/Plugin/Gallery.pm  view on Meta::CPAN

package Bootylicious::Plugin::Gallery;

use strict;
use warnings;
use base 'Mojolicious::Plugin';

use Digest::MD5 qw( md5_hex );
use Image::Magick::Thumbnail::Fixed;

our $VERSION = '0.07';

my %content_types = (
    'jpg'  => 'image/jpeg',
    'jpeg' => 'image/jpeg',
    'gif'  => 'image/gif',
    'png'  => 'image/png'
);

__PACKAGE__->attr('public_uri'     => '/');
__PACKAGE__->attr('string_to_replace' => '%INSERT_GALLERY_HERE%');
__PACKAGE__->attr('columns'        => 3);
__PACKAGE__->attr('thumb_width'    => 144);
__PACKAGE__->attr('thumb_height'   => 144);
__PACKAGE__->attr('bgcolor'        => 'white');
__PACKAGE__->attr('padding'        => 4);
__PACKAGE__->attr('imagetypes'     => join('|', keys %content_types))
  ;    #  build the list of valid image types


sub register {
    my ($self, $app, $args) = @_;
    $args ||= {};
    
    $self->public_uri($args->{'public_uri'} ) if $args->{'public_uri'};
    $self->string_to_replace($args->{'string_to_replace'} ) if $args->{'string_to_replace'};
    $self->columns($args->{'columns'} ) if $args->{'columns'};
    $self->thumb_width($args->{'thumb_width'} ) if $args->{'thumb_width'};
    $self->thumb_height($args->{'thumb_height'} ) if $args->{'thumb_height'};
    $self->bgcolor($args->{'bgcolor'} ) if $args->{'bgcolor'};
    $self->padding($args->{'padding'} ) if $args->{'padding'};
    $self->imagetypes($args->{'imagetypes'} ) if $args->{'imagetypes'};
    
    $app->plugins->add_hook(after_dispatch => sub { shift; $self->gallery(@_) });
}

sub gallery {
    my $self = shift;
    my $c    = shift;
    my $path = $c->req->url->path;

    return unless $path =~ /^\/articles/;

    $c->app->log->debug('imagetypes ' . $self->imagetypes);
    my $publicdir = $c->app->home->rel_dir(main::config('publicdir'));

    my $article = $c->stash('article');
    my  $gallery_name =   sprintf("%d%02d%02d-%s",$article->{year}, $article->{month}, $article->{day},  $article->{name});
    my $gallerydir = $publicdir . '/' . $gallery_name;

    #not gallery article
    unless (-d $gallerydir) {
        $c->app->log->debug("Not a gallery article: $gallerydir");
        return;
    }

    my $cached_dir = $gallerydir . '/' . 'thumbs';
    if (!-d $cached_dir) {
        unless (mkdir($cached_dir)) {

lib/Bootylicious/Plugin/Gallery.pm  view on Meta::CPAN

        my $thumbnail_url = $self->_build_thumb_url($c, $hashed_file);
        my $large_url = $self->_build_img_url($c, $img);

        push(@images,
            {'thumbnail_url' => $thumbnail_url, 'large_url' => $large_url});
    }
    $c->stash('images' => \@images);
    $c->stash(
        'columns' => $self->columns,
        'padding' => $self->padding,
        'bgcolor' => $self->bgcolor
    );

    my $gallery_html =
      $c->render_partial('gallery', template_class => __PACKAGE__);
    my $body        = $c->res->body;
    my $str_replace = $self->string_to_replace;
    $body =~ s/$str_replace/$gallery_html/;
    $c->res->body($body);
}

sub _build_thumb_url {
    my $self = shift;
    my $c    = shift;
    my $file = shift;

    return $self->public_uri . $c->stash('gallery_name') . '/thumbs/' . $file;
}

sub _build_img_url {
    my $self = shift;
    my $c    = shift;
    my $file = shift;

    return $self->public_uri . $c->stash('gallery_name') . '/' . $file;
}

sub _get_hashed_filename {
    my ($self, $c, $img_path) = @_;

    my ($extension) = $img_path =~ m|\.(\w+)$| or return undef;
    return (md5_hex($img_path) . ".$extension");
}

sub _cache_image {
    my ($self, $c, $opts) = @_;

    return
      if (-e $opts->{cached_file})
      && ((stat($opts->{source_file}))[9] < (stat($opts->{cached_file}))[9]);


    return $self->_create_thubnail($c, $opts);
}

sub _create_thubnail {
    my $self = shift;
    my $c    = shift;
    my $opts = shift;

    my $t = Image::Magick::Thumbnail::Fixed->new();

    $t->thumbnail(
        input   => $opts->{source_file},
        output  => $opts->{cached_file},
        width   => $self->thumb_width,
        height  => $self->thumb_height,
        bgcolor => $self->bgcolor,
    );

    return;
}


sub _find_images {
    my ($self, $c, $path) = @_;

    unless (opendir(DIR, $path)) {
        $c->app->log->warn("Couldn't open dir $path: $!");
        return ();
    }

    my @images;
    my $imagetypes = $self->imagetypes;
    foreach my $dentry (readdir(DIR)) {
        my $can_read = -r "$path/$dentry";
        if ($dentry =~ m{\.(?:$imagetypes)$}io && $can_read) {
            push(@images, $dentry);
        }
    }

    unless (closedir(DIR)) {
        $c->app->log->warn("Couldn't close dir $path: $!");
    }

    return sort { $a cmp $b } @images;
}


1;
__DATA__

@@ gallery.html.ep
% my $count = 1;
% my $pad = $padding / 2;

<center><table cellpadding='<%= $pad %>'><tr>
% foreach my $img (@{$images}) {
   <td><a target=_blank href='<%= $img->{large_url} %>'>
   <img border='0' src='<%= $img->{thumbnail_url} %>'></a></td>
%   if ( $count % $columns == 0 ) {
      </tr><tr>
%   }
%   $count++;
% }
</tr></table></center>


__END__

=head1 NAME



( run in 1.889 second using v1.01-cache-2.11-cpan-39bf76dae61 )