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