Journal(2005) | Blog(2006) | RandomLink | WhoAmI | LiveBookmark | HomePage

<<Previous: When perl is not quite fast enough  >>Next: 如何寻找用户真实IP

Gallery缩略图制作

Category: Script   Keywords: thumbnails Gallery

描述

此文档用以生成某目录下所有图片的缩略图,并可生成索引/index
Download gallery.pl

先决条件

use Image::Magick;

Script


#!/usr/bin/perl -w
use strict;
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
use Image::Magick;

#settings
my $thumbs_width = 100; #thumbs's width
my $thumbs_height = 100; #thumbs's height
my $dir = 'E:/Fayland/Gallery'; #the to-do dir
my $url = 'http://localhost/Gallery'; #the relevant url
my $need_index = 1; # 1 - produce the index.html, 0 - ingore

my $query = new CGI;
print $query->header;

my @gallery;
(-d "$dir") or die "Cann't find $dir";
opendir(DIR,"$dir");
@gallery = readdir(DIR);
closedir(DIR);
@gallery = grep(/(gif|jpe?g|png)$/, @gallery);

my $i;
open(FH, ">$dir/index.html") if ($need_index);
foreach (@gallery) {
    $_ =~ m/(.*).(.*)/;
    my $thumbsfile = "$1.png";
    unless (-e "$dir/thumbs/$thumbsfile") {
        my $image=Image::Magick->new;
        my $x = $image->Read("$dir/$_");
        warn "$x" if "$x";
        $x = $image->Resize(width=>$thumbs_width, height=>$thumbs_height);
        $x = $image->Write("$dir/thumbs/$thumbsfile");
    }
    print "<a href='$url/$_'><img src='$url/thumbs/$thumbsfile' width='100' height='100' border='0' alt='$_' /></a>";
    print FH "<a href='$_'><img src='thumbs/$thumbsfile' width='100' height='100' border='0' alt='$_' /></a>" if ($need_index);
    $i++;
    if ($i % 5 == 0) {
        print "<br>";
        print FH "<br>" if ($need_index);
    }
}
close(FH) if ($need_index);

print "<p>Visit the <a href='$url/index.html'>IndexPage</a></p>" if ($need_index);

License

The same as Perl.

<<Previous: When perl is not quite fast enough  >>Next: 如何寻找用户真实IP

Options: +Del.icio.us

Related items Created on 2004-10-30 19:03:51, Last modified on 2005-06-02 01:51:45
Copyright 2004-2005 All Rights Reserved. Powered by Eplanet && Catalyst 5.62.