Bootylicious-Plugin-Gallery

 view release on metacpan or  search on metacpan

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

    foreach my $img (@all_imgs) {
        my $hashed_file = $self->_get_hashed_filename($c, $img);
        next unless $hashed_file;

        $self->_cache_image(
            $c,
            {   'source_file' => "$opts->{gallerydir}/$img",
                'cached_file' => "$opts->{cached_dir}/$hashed_file"
            }
        );

        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>



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