Skip to content

Instantly share code, notes, and snippets.

@hikiko hikiko/thumbs.pl
Last active Mar 16, 2019

Embed
What would you like to do?
old script - supposed to create thumbs from images and thumbs.html file (not tested recently)
#!/usr/bin/perl
use strict;
use File::Path qw(rmtree);
use File::Copy;
use File::MimeInfo;
use Image::Magick;
my @files;
my @thumbs;
my $dir;
my $thdir;
my $ft;
my $filetype;
my $im;
my $image;
my $x;
$dir = $ARGV[0];
if ( -d $dir ) {
opendir(DIR,$dir) or die $!;
@files = (readdir DIR) or die $!;
closedir(DIR) or die $!;
$thdir = $dir."thumbs/";
if ( -d $thdir ) {
rmtree($thdir,0,0) or die $!;
}
$ft = File::MimeInfo->new();
mkdir( $thdir ) or die $!;
foreach ( @files ) {
next if ( $_ eq "." || $_ eq ".." || -d "$dir$_" );
$filetype = $ft->globs($_);
if ( $filetype eq "image/jpeg"||$filetype eq "image/png"|| $filetype eq "image/gif" ) {
copy($dir."$_",$thdir."$_") or die $!;
}
}
opendir(DIR2,$thdir) or die $!;
@thumbs = (readdir DIR2) or die $!;
@thumbs = sort(@thumbs);
closedir(DIR2) or die $!;
if ( -f $dir."thumbs.html" ) {
unlink($dir."thumbs.html");
}
open FILE, ">>", $dir."thumbs.html" or die $!;
print FILE qq[];
foreach ( @thumbs ) {
$im = Image::Magick->new();
next if ( $_ eq "." || $_ eq "..");
$x = $im->Read("$thdir$_");
die $x."\n" if ( $x );
$x = $im->Resize(width=>200, height=>200);
die $x."\n" if ( $x );
$x = $im->Write("$thdir$_");
die $x."\n" if ( $x );
$im = "";
print FILE qq[
<a href="$_"><img src="thumbs/$_" alt="image: $_" /></a>
];
}
print FILE qq[];
close FILE or die $!;
}
else {
die "there is no such directory!\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.