#!/usr/bin/perl -w use strict; use Image::Magick; use File::Glob qw(:globally :nocase); use File::Basename; use CGI; my $realdir = qw( /var/www/html/thumbnailer/images/ ); # The dir that holds the origional images my @ext = qw( jpg png gif); # Image extentions to look for my $savedir = qw( /var/www/html/thumbnailer/thumbs/ ); # Dir to save thumbnails my $serverdir = qw( /thumbnailer/ ); # Relative server-path my $thumbdir = qw( thumbs/ ); # Holds the thumbnails for the webpage my $imagedir = qw( images/ ); # Holds the real images for the webpage my $x_size = 150; # Size in pixels. my $y_size = 150; # Size in pixels. my $resize = 1; # Resize before crop. my $aspect_S = 0.5; # Only reseze if aspect-ratio is above this value, else thumbnail becomes to blurred. my $aspect_H = 2; # Only resize if aspect-ratio is below this value, else thumbnaik becomes to blurred. my $overwrite = 0; # Allways remake (overwrite) the thumbnails. my $cols = 5; # Max horizontal thumbs. my $rows = 10; # Max vertical thumbs. my $cgi = new CGI; main(); cleanUp(); sub main { my $content = ""; my $files = readDir(); my $thumbs_per_page = $rows * $cols; my $total = scalar(@$files) ? scalar(@$files) : 0; my $pages = $total / $thumbs_per_page; my $currentPage = $cgi->param('p') ? $cgi->param('p') : 1; my $hasPrevious = $currentPage-1 ? 1 : 0; my $hasNext = ($currentPage < $pages) ? 1 : 0 ; my $startImage = (($currentPage-1) * $thumbs_per_page) ; my $nav = ""; my $c = 1; my $i = 0; foreach my $file (@$files) { $i++; if ($i >= $total) { $nav .= ""; if ($hasPrevious) { $nav .= "Previous<\/a>\ \ "; } if ($hasNext) { $nav .= "Next<\/a>"; } $nav .= "<\/td><\/tr>"; } next if ($i <= $startImage || $i > ($startImage + $thumbs_per_page)); if ($c > $cols) { $content .= "<\/tr>\n"; $c = 1; } # Check if the file alreaddy exists: my $filename = "thumb_" . fileparse($file); if (!-e $savedir . $filename || $overwrite) { # Make new thumbnails... my $image = Image::Magick->new; $image->Read($file); my ($x,$y) = $image->Get('width', 'height'); # Enlarge image if thumbnail > origional, or resize before crop is enabled... if ($x < $x_size || $resize) { my $aspectratio = $y / $x; # Only resize if aspect-ratio is between given apect ratio-borders if ($aspectratio > $aspect_S && $aspectratio < $aspect_H || $x < $x_size) { $x = $x_size; $y=$x * $aspectratio; $image->Resize(width => $x, height => $y, filter => 'Cubic', blur => 1); } } if ($y < $y_size) { my $aspectratio = $x / $y; $y = $y_size; $x=$y * $aspectratio; $image->Resize(width => $x, height => $y, filter => 'Cubic', blur => 1); } # Get center (offset) of image, and crop to $x_size * $y_size. my $ox = ($x - $x_size) / 2; my $oy = ($y - $y_size) / 2; $image->Crop("${x_size}x${y_size}+$ox+$oy"); $image->Write($savedir.$filename); $content .= " \"\" <\/a><\/td> "; } else { # Skip writing... $content .= " \"\" <\/a><\/td> "; } $c++; } $content .= "<\/tr>\n" . $nav; printHtml($content); } sub printHtml { my ($content) = @_; # my $cgi = new CGI; print $cgi->header(-type => 'text/html', ); print $cgi->start_html( -title => 'Testpage', -BGCOLOR => '#ffffff',); print "\n"; print $content; print "\n<\/table>\n"; print $cgi->end_html; } sub readDir { my $files="*.{" . join(",",@ext) . "}"; my @files= glob($realdir.$files); return \@files; } sub cleanUp { undef $cgi, $realdir, @ext, $serverdir, $savedir, $x_size, $cols; undef $y_size, $resize, $aspect_S , $aspect_H, $overwrite, $rows; undef $thumbdir, $imagedir; }