1#!/usr/bin/env perl 2 3############################################################################### 4# 5# This file copyright (c) 2010-2012 by Randy J. Ray, all rights reserved 6# 7# Copying and distribution are permitted under the terms of the Artistic 8# License 2.0 (http://www.opensource.org/licenses/artistic-license-2.0.php) or 9# the GNU LGPL (http://www.opensource.org/licenses/lgpl-2.1.php). 10# 11############################################################################### 12# 13# No-brainer to size an image supplied on the command-line. All the real 14# work is done in Image::Size. 15 16use strict; 17use warnings; 18use vars qw($VERSION); 19 20use File::Basename 'basename'; 21use Getopt::Std; 22 23use Image::Size qw(:all); 24 25$VERSION = '1.001'; 26$VERSION = eval $VERSION; ## no critic(ProhibitStringyEval) 27 28my $cmd = basename $0; 29my ($rtn, %opts); 30 31getopts('hraf:', \%opts); 32 33# Usage reporting: if -h, or no @ARGV, or more than one of the rest... 34if ($opts{h} || (! @ARGV) || 35 (($opts{a} && $opts{r}) || ($opts{a} && $opts{f}) || 36 ($opts{r} && $opts{f}))) 37{ 38 die "Usage: $cmd [ -r | -a | -f fmt ] file ...\n"; 39} 40 41$rtn = \&return_html; 42$opts{a} && 43 ($rtn = \&return_attr); 44$opts{r} && 45 ($rtn = \&return_imgsize); 46$opts{f} && 47 ($rtn = \&return_fmt); 48 49if (@ARGV > 1) 50{ 51 foreach (@ARGV) 52 { 53 printf "$_: %s\n", $rtn->($_); 54 } 55} 56else 57{ 58 printf "%s\n", $rtn->($ARGV[0]); 59} 60 61exit 0; 62 63sub return_html 64{ 65 my $file = shift; 66 67 my ($width, $height, $err) = imgsize($file); 68 69 return (defined $width) ? 70 qq(width="$width" height="$height") : "error: $err"; 71} 72 73sub return_attr 74{ 75 my $file = shift; 76 77 my ($width, $height, $err) = imgsize($file); 78 79 return (defined $width) ? 80 "(-width => $width, -height => $height)" : "error: $err"; 81} 82 83sub return_imgsize 84{ 85 my $file = shift; 86 87 my ($width, $height, $err) = imgsize($file); 88 89 return (defined $width) ? "$width $height" : "error: $err"; 90} 91 92sub return_fmt 93{ 94 my $file = shift; 95 96 my ($width, $height, $err) = imgsize($file); 97 98 return (defined $width) ? 99 sprintf($opts{f}, $width, $height, $err) : "error: $err"; 100} 101 102__END__ 103 104=head1 NAME 105 106imgsize - read the dimensions of an image in several popular formats 107 108=head1 USAGE 109 110 imgsize [ -r | -a | -f fmt ] file [ file ... ] 111 112=head1 DESCRIPTION 113 114No-brainer to size an image supplied on the command-line. All the real 115work is done in L<Image::Size|Image::Size>. 116 117=head1 REQUIRED ARGUMENTS 118 119B<imgsize> expects file names on the command-line. If more than one file is 120provided, the file name will be included in the output: 121 122 % imgsize dot.gif 123 width="16" height="16" 124 % imgsize dot.gif dash.gif 125 dot.gif: width="16" height="16" 126 dash.gif: width="32" height="8" 127 128=head1 OPTIONS 129 130By default, the width and height are returned as attributes for an IMG tag in 131HTML, essentially C<width="40" height="30"> or similar. The following options 132may be used to return alternate formats (all report width first, then height): 133 134=over 135 136=item C<-r> 137 138Return "raw" format data. Just the numbers separated by a single space. 139 140=item C<-a> 141 142Return a Perl-style list of attributes suitable for passing to the C<img()> 143method of the CGI module (see L<CGI|CGI>). 144 145=item C<-f> B<fmt> 146 147Pass the string specified in I<fmt> to C<sprintf> and thus use it to format 148the results to your taste. C<sprintf> will be passed two numbers, so any 149other formatting directives will be lost. The numbers are passed as width 150first, then height. 151 152=back 153 154=head1 EXIT STATUS 155 156B<imgsize> always exits 157 158=head1 BUGS 159 160Please report any bugs or feature requests to 161C<bug-image-size at rt.cpan.org>, or through the web interface at 162L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Image-Size>. I will be 163notified, and then you'll automatically be notified of progress on 164your bug as I make changes. 165 166=head1 SUPPORT 167 168=over 4 169 170=item * RT: CPAN's request tracker 171 172L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Image-Size> 173 174=item * AnnoCPAN: Annotated CPAN documentation 175 176L<http://annocpan.org/dist/Image-Size> 177 178=item * CPAN Ratings 179 180L<http://cpanratings.perl.org/d/Image-Size> 181 182=item * Search CPAN 183 184L<http://search.cpan.org/dist/Image-Size> 185 186=item * Project page on GitHub 187 188L<http://github.com/rjray/image-size> 189 190=back 191 192=head1 LICENSE AND COPYRIGHT 193 194Copying and distribution are permitted under the terms of the Artistic 195License 2.0 (L<http://www.opensource.org/licenses/artistic-license-2.0.php>) or 196the GNU LGPL 2.1 (L<http://www.opensource.org/licenses/lgpl-2.1.php>). 197 198=head1 SEE ALSO 199 200L<Image::Size|Image::Size> 201 202=head1 AUTHOR 203 204Randy J. Ray C<< <rjray@blackperl.com> >>. 205