Script που φτιαχνει thumbnails απο εικονες…


$ perl thumbs.pl directory_name
$ cd directory_name
$ firefox thumbs.html &

script code:


#!/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[
			image: $_
		];
	}
	print FILE qq[];
	close FILE or die $!;
}
else {
	die "there is no such directory!n";
}

Advertisements
Script που φτιαχνει thumbnails απο εικονες…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s