‰PNG  IHDR @ @ ªiqÞ pHYs   šœ —tEXtComment #!/usr/bin/env perl ############################################################################### # # This file copyright (c) 2010-2012 by Randy J. Ray, all rights reserved # # Copying and distribution are permitted under the terms of the Artistic # License 2.0 (http://www.opensource.org/licenses/artistic-license-2.0.php) or # the GNU LGPL (http://www.opensource.org/licenses/lgpl-2.1.php). # ############################################################################### # # No-brainer to size an image supplied on the command-line. All the real # work is done in Image::Size. use strict; use warnings; use vars qw($VERSION); use File::Basename 'basename'; use Getopt::Std; use Image::Size qw(:all); $VERSION = '1.001'; $VERSION = eval $VERSION; ## no critic(ProhibitStringyEval) my $cmd = basename $0; my ($rtn, %opts); getopts('hraf:', \%opts); # Usage reporting: if -h, or no @ARGV, or more than one of the rest... if ($opts{h} || (! @ARGV) || (($opts{a} && $opts{r}) || ($opts{a} && $opts{f}) || ($opts{r} && $opts{f}))) { die "Usage: $cmd [ -r | -a | -f fmt ] file ...\n"; } $rtn = \&return_html; $opts{a} && ($rtn = \&return_attr); $opts{r} && ($rtn = \&return_imgsize); $opts{f} && ($rtn = \&return_fmt); if (@ARGV > 1) { foreach (@ARGV) { printf "$_: %s\n", $rtn->($_); } } else { printf "%s\n", $rtn->($ARGV[0]); } exit 0; sub return_html { my $file = shift; my ($width, $height, $err) = imgsize($file); return (defined $width) ? qq(width="$width" height="$height") : "error: $err"; } sub return_attr { my $file = shift; my ($width, $height, $err) = imgsize($file); return (defined $width) ? "(-width => $width, -height => $height)" : "error: $err"; } sub return_imgsize { my $file = shift; my ($width, $height, $err) = imgsize($file); return (defined $width) ? "$width $height" : "error: $err"; } sub return_fmt { my $file = shift; my ($width, $height, $err) = imgsize($file); return (defined $width) ? sprintf($opts{f}, $width, $height, $err) : "error: $err"; } __END__ =head1 NAME imgsize - read the dimensions of an image in several popular formats =head1 USAGE imgsize [ -r | -a | -f fmt ] file [ file ... ] =head1 DESCRIPTION No-brainer to size an image supplied on the command-line. All the real work is done in L. =head1 REQUIRED ARGUMENTS B expects file names on the command-line. If more than one file is provided, the file name will be included in the output: % imgsize dot.gif width="16" height="16" % imgsize dot.gif dash.gif dot.gif: width="16" height="16" dash.gif: width="32" height="8" =head1 OPTIONS By default, the width and height are returned as attributes for an IMG tag in HTML, essentially C or similar. The following options may be used to return alternate formats (all report width first, then height): =over =item C<-r> Return "raw" format data. Just the numbers separated by a single space. =item C<-a> Return a Perl-style list of attributes suitable for passing to the C method of the CGI module (see L). =item C<-f> B Pass the string specified in I to C and thus use it to format the results to your taste. C will be passed two numbers, so any other formatting directives will be lost. The numbers are passed as width first, then height. =back =head1 EXIT STATUS B always exits =head1 BUGS Please report any bugs or feature requests to C, or through the web interface at L. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. =head1 SUPPORT =over 4 =item * RT: CPAN's request tracker L =item * AnnoCPAN: Annotated CPAN documentation L =item * CPAN Ratings L =item * Search CPAN L =item * Project page on GitHub L =back =head1 LICENSE AND COPYRIGHT Copying and distribution are permitted under the terms of the Artistic License 2.0 (L) or the GNU LGPL 2.1 (L). =head1 SEE ALSO L =head1 AUTHOR Randy J. Ray C<< >>.