File: imgsize

package info (click to toggle)
libimage-size-perl 3.300-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 2,324 kB
  • sloc: perl: 988; xml: 987; makefile: 2
file content (206 lines) | stat: -rwxr-xr-x 4,677 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/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);

my $error = 0;

if (@ARGV > 1)
{
    foreach (@ARGV)
    {
        print STDOUT sprintf("%s: %s\n", $_, $rtn->($_));
    }
}
else
{
    printf "%s\n", $rtn->($ARGV[0]);
}

exit $error;

sub return_html
{
    my $file = shift;

    my ($width, $height, $err) = imgsize($file);

    return (defined $width) ?
        qq(width="$width" height="$height") : ("error: $err", $error = 1);
}

sub return_attr
{
    my $file = shift;

    my ($width, $height, $err) = imgsize($file);

    return (defined $width) ?
        "(-width => $width, -height => $height)" : ("error: $err", $error = 1);
}

sub return_imgsize
{
    my $file = shift;

    my ($width, $height, $err) = imgsize($file);

    return (defined $width) ? "$width $height" : ("error: $err", $error = 1);
}

sub return_fmt
{
    my $file = shift;

    my ($width, $height, $err) = imgsize($file);

    return (defined $width) ?
        sprintf($opts{f}, $width, $height, $err) : ("error: $err", $error = 1);
}

__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<Image::Size|Image::Size>.

=head1 REQUIRED ARGUMENTS

B<imgsize> 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<width="40" height="30"> 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<img()>
method of the CGI module (see L<CGI|CGI>).

=item C<-f> B<fmt>

Pass the string specified in I<fmt> to C<sprintf> and thus use it to format
the results to your taste. C<sprintf> 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<imgsize> always exits

=head1 BUGS

Please report any bugs or feature requests to
C<bug-image-size at rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Image-Size>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Image-Size>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Image-Size>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Image-Size>

=item * Search CPAN

L<http://search.cpan.org/dist/Image-Size>

=item * Project page on GitHub

L<http://github.com/rjray/image-size>

=back

=head1 LICENSE AND COPYRIGHT

Copying and distribution are permitted under the terms of the Artistic
License 2.0 (L<http://www.opensource.org/licenses/artistic-license-2.0.php>) or
the GNU LGPL 2.1 (L<http://www.opensource.org/licenses/lgpl-2.1.php>).

=head1 SEE ALSO

L<Image::Size|Image::Size>

=head1 AUTHOR

Randy J. Ray C<< <rjray@blackperl.com> >>.