post
poster: toraton
description: Simple gallery (unfinished)
language: Perl
[download]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/perl

use CGI qw(:standard);
use Data::Dumper;

my @extensions = ('jpg', 'png', 'gif');

sub main() {

    print header();
    print start_html();

    opendir(GALLERY, '.');
    foreach my $file (readdir(GALLERY)) {
            foreach my $ext (@extensions) {
                if ($file =~ /$ext$/i) {
                    my $info = get_image_info($file);
                    print img({-src => $info->{filename},
                        -width => int($info->{width} * 0.10),
                        -height => int($info->{height} * 0.10)
                    });
                }
            }
        }
        print br();
    }

    closedir(GALLERY);
}

{
    # Requires ImageMagick's 'identify' tool.
    sub get_image_info($) {
        my $image = shift(@_) || undef;
    my $info = {};

    my $output = `identify $image`;

        if ($output =~ /(.*)\s+(.*?)\s+(\d+)x(\d+)\s+
                    (.*?)\s+(.*?)\s+/x) {
        $info->{filename} = $1;
        $info->{format}   = $2;
        $info->{width}    = $3;
        $info->{height}   = $4;
        $info->{class}    = $5;
        $info->{filesize} = $6;
    }

    return $info;
    }
}

main();