Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: thumbnails generator

by Lu. (Hermit)
on Apr 21, 2008 at 09:22 UTC ( [id://681886]=note: print w/replies, xml ) Need Help??


in reply to thumbnails generator

You don't really need to check whether the image is 800x600 or 600x800, actually. Image::Magick will do that on its own if you convert to '108x108', converting the longest side to 108 px, and the shortest to whatever it would proportionally amount to.

Here is some code I've been using to generate an HTML index for images files, with thumbnails :
#!/usr/bin/perl use strict; use warnings; use utf8; use Image::Magick; my $dir = $ENV{'PWD'}; chomp(my @files = `ls $dir`); if (-e "$dir/thumbs") { print STDERR "The directory $dir/thumbs already exists. Files inside + may be replaced.\n Do you want to continue ? (y/n)"; chomp(my $choice = <STDIN>); if ($choice =~ /^n/) { print STDERR "Aborted by user.\n"; exit(1); } else { if (!($choice =~ /^y/)) { print "Please enter 'y' or 'n' :"; } } } mkdir "$dir/thumbs" || die "Unable to create directory $dir/thumbs :$! +"; if (!(-e "$dir/index.html")) { open (OUT, ">:utf8", "index.html") || die "Unable to open file $dir/ +index.html : $!" } print OUT <<HERE; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w +3.org/TR/xhtml1/xhtml1-strict.dtd"> <html encoding="utf8"> <head> </head> <body> <h1>Contents of directory $dir</h1> HERE foreach my $file (@files) { if ($file =~ /\.(jpg|jpeg|bmp|gif|png)/) { my $image = Image::Magick->new(); $image->Read($file); $image->Resize('150x150'); $image->Write("thumbs/t_$file"); print OUT "<a href=\"$file\"><img src=\"thumbs/t_$file\"></a>\n"; } } print OUT "\n</body>\n</html>\n"; close OUT;
Lu.

Replies are listed 'Best First'.
Re^2: thumbnails generator
by spx2 (Deacon) on Apr 21, 2008 at 10:20 UTC
    very nice. except for the fact that Image::Magick wouldn't let itself installed on my system. I used cpan and I got some errors which I was not able to fix. so that's why I went in the shell scripting phase.

      It's a while since I was using it, but my recollection is that Image Magick's install provided the Perl bindings.


      Perl is environmentally friendly - it saves trees
      as GrandFather said, if you install Image Magick, the Perl bindings will be installed as well.

      cpan didn't work for me either, so I just built Image Magick from source, and now it works very well.

      Lu.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://681886]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-20 02:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found