#!/usr/bin/perl use strict; use warnings; use Image::Imlib2; if (!@ARGV) { print "Usage: create_square.pl file1.jpg [file2.jpg ...]\n"; exit; } for (@ARGV) { my $filename = $_; my $image = Image::Imlib2->load($filename); my $thumb = create_square($image, 75); my $thumbname = $filename; $thumbname =~ s/\.(....?)$/_t\.$1/; $thumb->image_set_format("jpeg"); $thumb->set_quality(84); $thumb->save($thumbname); } sub create_square { my ($image, $size) = @_; my $width = $image->get_width; my $height = $image->get_height; my $cropped_image; if ($width > $height) { $cropped_image = $image->crop(($width - $height)/2, 0, $height, $height); } else { $cropped_image = $image->crop(0, ($height - $width)/2, $width, $width); } return $cropped_image->create_scaled_image($size, $size); } __END__ =head1 NAME create_square.pl - Creates square thumbnail images =head1 SYNOPSIS create_square.pl file1.jpg [file2.jpg ...] =head1 DESCRIPTION Creates square thumbnail images from one or more jpeg, png or tiff image (anything that B can handle). It saves the thumbnails in the same directory as the files are found, adding a suffix to the name. For the file file1.jpg, the thumbnail will be named file1_t.jpg. =head1 TODO Safer loading and saving =head1 AUTHOR Jon Aslund L =head1 COPYRIGHT Copyright (C) 2005, Jon Aslund This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.