#!perl use strict; use warnings; use diagnostics; use File::Spec; use GD; # Edit only these five values my $WIDTHmm = 201; # printable area width in mm my $HEIGHTmm = 288; # printable area height in mm my $BORDERmm = 5; # white space between photos in mm my $FONTmm = 3; # font height in mm my $DPI = 96; # dots per inch # Remaining variables derived from the above my $DPmm = $DPI/25.4; # dots per mm my $WIDTH = $WIDTHmm * $DPmm; my $HEIGHT = $HEIGHTmm * $DPmm; my $BORDER = $BORDERmm * $DPmm; my $POINTS = $DPmm * $FONTmm; my $TEXTHEIGHT = $POINTS; print "$POINTS points\n"; my $dir = shift || "."; my $filemask = File::Spec->catfile( $dir, '*.jpg' ); my @image_filenames = sort(glob($filemask)); # sort just to be sure... # Dimension an almost square rectangle my $cols = int( sqrt(scalar @image_filenames) ); my $rows = (scalar @image_filenames) / $cols; if ( $rows != int($rows) ) { $rows = int($rows) + 1; } # Calculate the cell size in pixels # totalwidth = cols * cellwidth + ( cols - 1 ) * border # (totalwidth - ( cols - 1 ) * border) / cols = cellwidth my $cellWidth = ($WIDTH - ($cols - 1) * $BORDER ) / $cols; my $cellHeight = ($HEIGHT / $rows) - $BORDER; my $img = new GD::Image($WIDTH, $HEIGHT); $img->transparent($img->colorAllocate(255,255,255)); my $black = $img->colorAllocate(0,0,0); my ($row, $col) = (0,0); for my $imgname (@image_filenames) { my $imgsrc = GD::Image->newFromJpeg($imgname); my ($widthsrc,$heightsrc) = $imgsrc->getBounds(); # Scale the image, preserving the aspect ratio my $scalefactor = $cellWidth / $widthsrc; if ( $cellHeight / $heightsrc < $scalefactor ) { $scalefactor = $cellHeight / $heightsrc; } my $dstX = $col * ($cellWidth + $BORDER); my $dstY = $row * ($cellHeight + $BORDER); my $destW = $scalefactor * $widthsrc; my $destH = $scalefactor * $heightsrc; $img->copyResampled($imgsrc, $dstX, $dstY, 0, 0, $destW, $destH, $widthsrc, $heightsrc); my ($vol, $path, $file) = File::Spec->splitpath($imgname); my @ret = $img->stringFT($black,$ENV{'windir'}."\\Fonts\\arial.ttf",$POINTS,0,$dstX,$dstY+$destH+$TEXTHEIGHT,$file); unless ( @ret ) { print $@; } if (++$col >= $cols) { $col=0; ++$row; } # next picture position } open(my $fh, ">", "$dir\\Thumbs.png") or die "Couldn't open output file."; binmode $fh; print $fh $img->png; close $fh;