-
Notifications
You must be signed in to change notification settings - Fork 33
/
resize_images.pl
184 lines (146 loc) · 4.6 KB
/
resize_images.pl
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
#!/usr/bin/perl
# Author: Trizen
# Date: 30 October 2023
# Edit: 10 August 2024
# https://github.com/trizen
# Resize images to a given width or height, keeping aspect ratio.
use 5.036;
use Imager qw();
use File::Find qw(find);
use List::Util qw(min max);
use Getopt::Long qw(GetOptions);
my $width = 'auto';
my $height = 'auto';
my $min = 'auto';
my $max = 'auto';
my $qtype = 'mixing';
my $outdir = undef;
my $img_formats = '';
my $preserve_attr = 0;
my @img_formats = qw(
jpeg
jpg
png
);
sub usage ($code) {
local $" = ",";
print <<"EOT";
usage: $0 [options] [dirs | files]
options:
-w --width=i : resize images to this width
-h --height=i : resize images to this height
--min=i : resize images to have the smallest side equal to this
--max=i : resize images to have the largest side equal to this
-q --quality=s : quality of scaling: 'normal', 'preview' or 'mixing' (default: $qtype)
-f --formats=s,s : specify more image formats (default: @img_formats)
-p --preserve! : preserve original file timestamps and permissions
-o --outdir=s : create resized images into this directory
examples:
$0 --min=1080 *.jpg # smallest side = 1080 pixels
$0 --height=1080 *.jpg # height = 1080 pixels
EOT
exit($code);
}
GetOptions(
'w|width=i' => \$width,
'h|height=i' => \$height,
'minimum=i' => \$min,
'maximum=i' => \$max,
'q|quality=s' => \$qtype,
'f|formats=s' => \$img_formats,
'p|preserve!' => \$preserve_attr,
'o|outdir=s' => \$outdir,
'help' => sub { usage(0) },
)
or die("Error in command line arguments");
push @img_formats, map { quotemeta } split(/\s*,\s*/, $img_formats);
my $img_formats_re = do {
local $" = '|';
qr/\.(@img_formats)\z/i;
};
if (defined($outdir)) {
if (not -d $outdir) {
require File::Path;
File::Path::make_path($outdir)
or die "Can't create output directory <<$outdir>>: $!";
}
require File::Basename;
require File::Spec::Functions;
}
sub resize_image ($image) {
my $img = Imager->new(file => $image) or do {
warn "Failed to load <<$image>>: ", Imager->errstr();
return;
};
my ($curr_width, $curr_height) = ($img->getwidth, $img->getheight);
if ($min ne 'auto' and $min > 0) {
if (min($curr_width, $curr_height) <= $min) {
say "Image too small to resize";
return;
}
if ($curr_width < $curr_height) {
$img = $img->scale(xpixels => $min, qtype => $qtype);
}
else {
$img = $img->scale(ypixels => $min, qtype => $qtype);
}
}
elsif ($max ne 'auto' and $max > 0) {
if (max($curr_width, $curr_height) <= $max) {
say "Image too small to resize";
return;
}
if ($curr_height > $curr_width) {
$img = $img->scale(ypixels => $max, qtype => $qtype);
}
else {
$img = $img->scale(xpixels => $max, qtype => $qtype);
}
}
elsif ($height ne 'auto' and $height > 0) {
if ($curr_height <= $height) {
say "Image too small to resize";
return;
}
$img = $img->scale(ypixels => $height, qtype => $qtype);
}
elsif ($width ne 'auto' and $width > 0) {
if ($curr_width <= $width) {
say "Image too small to resize";
return;
}
$img = $img->scale(xpixels => $width, qtype => $qtype);
}
else {
die "No --width or --height specified...";
}
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($image);
# Create resized image into $outdir directory
if (defined($outdir)) {
$image = File::Spec::Functions::catfile($outdir, File::Basename::basename($image));
}
$img->write(file => $image) or do {
warn "Failed to rewrite image: ", $img->errstr;
return;
};
# Set the original ownership of the image
chown($uid, $gid, $image);
if ($preserve_attr) {
# Set the original modification time
utime($atime, $mtime, $image)
or warn "Can't change timestamp: $!\n";
# Set original permissions
chmod($mode & 07777, $image)
or warn "Can't change permissions: $!\n";
}
return 1;
}
@ARGV || usage(1);
find {
no_chdir => 1,
wanted => sub {
(/$img_formats_re/o && -f) || return;
say "Resizing: $_";
resize_image($_);
}
} => @ARGV;