Initial commit 0.001
authorMarius Gavrilescu <marius@ieval.ro>
Mon, 31 Jul 2017 14:10:06 +0000 (17:10 +0300)
committerMarius Gavrilescu <marius@ieval.ro>
Mon, 31 Jul 2017 14:10:06 +0000 (17:10 +0300)
Changes [new file with mode: 0644]
MANIFEST [new file with mode: 0644]
Makefile.PL [new file with mode: 0644]
README [new file with mode: 0644]
gallery.pl [new file with mode: 0755]
lib/App/Gallery.pm [new file with mode: 0644]
t/100x400.png [new file with mode: 0644]
t/800x200.png [new file with mode: 0644]
t/App-Gallery.t [new file with mode: 0644]
t/example-tmpl [new file with mode: 0644]

diff --git a/Changes b/Changes
new file mode 100644 (file)
index 0000000..8bfad70
--- /dev/null
+++ b/Changes
@@ -0,0 +1,4 @@
+Revision history for Perl extension App::Gallery.
+
+0.001 2017-07-31T22:10+08:00
+ - Initial release
diff --git a/MANIFEST b/MANIFEST
new file mode 100644 (file)
index 0000000..3a7eb96
--- /dev/null
+++ b/MANIFEST
@@ -0,0 +1,10 @@
+Changes
+gallery.pl
+lib/App/Gallery.pm
+Makefile.PL
+MANIFEST
+README
+t/100x400.png
+t/800x200.png
+t/App-Gallery.t
+t/example-tmpl
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644 (file)
index 0000000..5b7e815
--- /dev/null
@@ -0,0 +1,22 @@
+use 5.014000;
+use ExtUtils::MakeMaker;
+
+WriteMakefile(
+       NAME              => 'App::Gallery',
+       VERSION_FROM      => 'lib/App/Gallery.pm',
+       ABSTRACT_FROM     => 'lib/App/Gallery.pm',
+       AUTHOR            => 'Marius Gavrilescu <marius@ieval.ro>',
+       EXE_FILES         => ['gallery.pl'],
+       MIN_PERL_VERSION  => '5.14.0',
+       LICENSE           => 'perl',
+       SIGN              => 1,
+       PREREQ_PM         => {
+               qw/Image::Magick 0/,
+       },
+       META_ADD         => {
+               dynamic_config => 0,
+               resources      => {
+                       repository   => 'https://git.ieval.ro/?p=app-gallery.git',
+               },
+       }
+);
diff --git a/README b/README
new file mode 100644 (file)
index 0000000..3a5a61a
--- /dev/null
+++ b/README
@@ -0,0 +1,33 @@
+App-Gallery version 0.001
+=========================
+
+gallery.pl creates basic image galleries. Pass an output directory and
+a list of images to the script. The images will be hard linked into
+the directory (or copied if hard linking fails), then thumbnails will
+be created for the images, and finally an F<index.html> file linking
+to all the images will be created in the directory.
+
+INSTALLATION
+
+To install this module type the following:
+
+   perl Makefile.PL
+   make
+   make test
+   make install
+
+DEPENDENCIES
+
+This module requires these other modules and libraries:
+
+* Image::Magick
+
+COPYRIGHT AND LICENCE
+
+Copyright (C) 2017 by Marius Gavrilescu
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.24.2 or,
+at your option, any later version of Perl 5 you may have available.
+
+
diff --git a/gallery.pl b/gallery.pl
new file mode 100755 (executable)
index 0000000..15d0d45
--- /dev/null
@@ -0,0 +1,78 @@
+#!/usr/bin/perl -w
+use v5.14;
+
+use App::Gallery;
+use Getopt::Long;
+
+my %args;
+
+GetOptions (
+       'out|o=s' => \$args{out},
+       'tmpl=s' => \$args{tmpl},
+       'title=s' => \$args{title},
+       'width=i' => \$args{width},
+       'height=i' => \$args{height},
+);
+
+die "Argument --out PATH is mandatory\n" unless $args{out};
+
+App::Gallery->run(\%args, @ARGV);
+
+__END__
+
+=encoding utf-8
+
+=head1 NAME
+
+gallery.pl - very basic image gallery script
+
+=head1 SYNOPSIS
+
+  gallery.pl --out DIR [--tmpl TEMPLATE]
+    [--width PIXELS] [--height PIXELS] [--title TITLE] IMAGE...
+
+=head1 DESCRIPTION
+
+gallery.pl creates basic image galleries. Pass an output directory and
+a list of images to the script. The images will be hard linked into
+the directory (or copied if hard linking fails), then thumbnails will
+be created for the images, and finally an F<index.html> file linking
+to all the images will be created in the directory.
+
+=head1 OPTIONS
+
+=over
+
+=item B<--out> I<path>
+
+Directory to create everything in. Created if it does not exist. Mandatory.
+
+=item B<--tmpl> I<template>
+
+Path to template file, in HTML::Template::Compiled format.
+
+=item B<--width> I<width>
+
+Maximum width of thumbnails, in pixels. Defaults to 600.
+
+=item B<--height> I<height>
+
+Maximum height of thumbnails, in pixels. Defaults to 600.
+
+=item B<--title> I<title>
+
+Title of HTML page. Defaults to 'Gallery'.
+
+=back
+
+=head1 AUTHOR
+
+Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (C) 2017 by Marius Gavrilescu
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.14.2 or,
+at your option, any later version of Perl 5 you may have available.
diff --git a/lib/App/Gallery.pm b/lib/App/Gallery.pm
new file mode 100644 (file)
index 0000000..17f2f5e
--- /dev/null
@@ -0,0 +1,118 @@
+package App::Gallery;
+
+use 5.014000;
+use strict;
+use warnings;
+
+use File::Basename qw/fileparse/;
+use File::Copy qw/cp/;
+use File::Path qw/make_path/;
+use File::Slurp;
+use File::Spec::Functions qw/catdir catfile/;
+use HTML::Template::Compiled;
+use Image::Magick;
+
+our $VERSION = '0.001';
+
+my $default_template;
+my %default_args = (tmpl => '', title => 'Gallery', width => 600, height => 600);
+
+sub run {
+       my (undef, $args, @images) = @_;
+       my %args = (%default_args, %$args);
+       my $full = catfile $args{out}, 'full';
+       my $thumb = catfile $args{out}, 'thumb';
+       my $tmpl = HTML::Template::Compiled->new(
+               (($args{tmpl} // '') eq '')
+                 ? (scalarref => \$default_template)
+                 : (filename => $args{tmpl}),
+               default_escape => 'HTML',
+       );
+       make_path $full, $thumb;
+
+       for my $path (@images) {
+               my $basename = fileparse $path;
+               my $thumb_path = catfile $thumb, $basename;
+               my $dest_path = catfile $full, $basename;
+
+               link $path, $dest_path or cp $path, $dest_path or die "$!";
+
+               my $img = Image::Magick->new;
+               $img->Read($path);
+               my ($width, $height) = $img->Get('width', 'height');
+               my $aspect_ratio = $width / $height;
+               if ($width > $args{width}) {
+                       $width = $args{width};
+                       $height = $width / $aspect_ratio;
+               }
+               if ($height > $args{height}) {
+                       $height = $args{height};
+                       $width = $height * $aspect_ratio;
+               }
+               $img->Thumbnail(width => $width, height => $height);
+               $img->Write($thumb_path);
+       }
+
+       $tmpl->param(
+               title => $args{title},
+               images => [map { scalar fileparse $_ } @images]
+       );
+
+       my $index = catfile $args{out}, 'index.html';
+       write_file $index, $tmpl->output;
+}
+
+$default_template = <<'EOF';
+<!DOCTYPE html>
+<title><tmpl_var title></title>
+<meta charset="utf-8">
+<style>
+.imgwrap {
+        display: inline-block;
+        margin: 6px 3px;
+        vertical-align: center;
+        text-align: center;
+}
+</style>
+<link rel="stylesheet" href="style.css">
+
+<h1><tmpl_var title></h1>
+<div>
+<tmpl_loop images><div class=imgwrap><a href='full/<tmpl_var _>'><img src='thumb/<tmpl_var _>'></a></div>
+</tmpl_loop></div>
+EOF
+
+1;
+__END__
+
+=encoding utf-8
+
+=head1 NAME
+
+App::Gallery - Very basic picture gallery
+
+=head1 SYNOPSIS
+
+  use App::Gallery;
+
+=head1 DESCRIPTION
+
+App::Gallery is a script for creating a very basic picture gallery out
+of a list of pictures.
+
+=head1 SEE ALSO
+
+=head1 AUTHOR
+
+Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (C) 2017 by Marius Gavrilescu
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.24.2 or,
+at your option, any later version of Perl 5 you may have available.
+
+
+=cut
diff --git a/t/100x400.png b/t/100x400.png
new file mode 100644 (file)
index 0000000..cc442e1
Binary files /dev/null and b/t/100x400.png differ
diff --git a/t/800x200.png b/t/800x200.png
new file mode 100644 (file)
index 0000000..64d3639
Binary files /dev/null and b/t/800x200.png differ
diff --git a/t/App-Gallery.t b/t/App-Gallery.t
new file mode 100644 (file)
index 0000000..45b5815
--- /dev/null
@@ -0,0 +1,70 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+use File::Temp qw/tempdir/;
+use File::Slurp;
+use File::Spec::Functions;
+use Image::Magick;
+
+use Test::More tests => 11;
+BEGIN { use_ok('App::Gallery') };
+
+sub test_img_size {
+       my ($width, $height, $file) = @_;
+       my $image = Image::Magick->new;
+       $image->Read($file);
+       my ($actual_width, $actual_height) = $image->Get(qw/width height/);
+       is $width, $actual_width, 'image width';
+       is $height, $actual_height, 'image height';
+}
+
+my @imgs = <t/*.png>;
+my $dir = tempdir ('app-gallery.XXXX', TMPDIR => 1, CLEANUP => 1);
+my $dir1 = catdir $dir, 'test1';
+my $dir2 = catdir $dir, 'test2';
+
+App::Gallery->run({out => $dir1, title => 'Some title', width => 200, height => 200}, @imgs);
+
+my $html = read_file catfile $dir1, 'index.html';
+is $html, <<'EOF', 'index.html as expected';
+<!DOCTYPE html>
+<title>Some title</title>
+<meta charset="utf-8">
+<style>
+.imgwrap {
+        display: inline-block;
+        margin: 6px 3px;
+        vertical-align: center;
+        text-align: center;
+}
+</style>
+<link rel="stylesheet" href="style.css">
+
+<h1>Some title</h1>
+<div>
+<div class=imgwrap><a href='full/100x400.png'><img src='thumb/100x400.png'></a></div>
+<div class=imgwrap><a href='full/800x200.png'><img src='thumb/800x200.png'></a></div>
+</div>
+EOF
+
+test_img_size (50, 200, catfile $dir1, 'thumb', '100x400.png');
+test_img_size (200, 50, catfile $dir1, 'thumb', '800x200.png');
+
+App::Gallery->run({out => $dir2, tmpl => catfile 't', 'example-tmpl'}, @imgs);
+
+$html = read_file catfile $dir2, 'index.html';
+is $html, <<'EOF', 'index.html as expected';
+<!DOCTYPE html>
+<title>Gallery</title>
+<meta charset="utf-8">
+<link rel="stylesheet" href="style.css">
+
+<div>
+<div><a href='full/100x400.png'><img src='thumb/100x400.png'></a></div>
+<div><a href='full/800x200.png'><img src='thumb/800x200.png'></a></div>
+</div>
+EOF
+
+test_img_size (100, 400, catfile $dir2, 'thumb', '100x400.png');
+test_img_size (600, 150, catfile $dir2, 'thumb', '800x200.png');
diff --git a/t/example-tmpl b/t/example-tmpl
new file mode 100644 (file)
index 0000000..fa3f64c
--- /dev/null
@@ -0,0 +1,8 @@
+<!DOCTYPE html>
+<title><tmpl_var title></title>
+<meta charset="utf-8">
+<link rel="stylesheet" href="style.css">
+
+<div>
+<tmpl_loop images><div><a href='full/<tmpl_var _>'><img src='thumb/<tmpl_var _>'></a></div>
+</tmpl_loop></div>
This page took 0.018407 seconds and 4 git commands to generate.