forked from spicyjack/perl-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate.pl
executable file
·174 lines (152 loc) · 5.69 KB
/
rotate.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
#!/usr/bin/perl -w
# created 08/05/02 for rotating images, keeping their ctimes intact
#
# (C) 2002 by Brian Manning <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# TODO
# - write a switch that you can turn on that will rotate all files with the
# base dcp_#### name, instead of individual files
# use directives
use Getopt::Std;
# begin
my (%opts, $filedate, $rotate_direction);
my ($basename, $systemstr); # base image file name, system call string
&getopts("dhq129f:", \%opts);
$JPEGTRAN="/usr/bin/convert";
# check for command line options
if ( exists $opts{h} ) { &ShowHelp; exit 0;}
# are we debugging?
if ( exists $opts{d} ) { $DEBUG = 1; }
# are we debugging?
if ( exists $opts{q} ) { $QUIET = 1; }
if ( ! -r $opts{f} ) {
die "rotate.pl: error - $! - exiting\n";
} # if ( ! -r $opts{f} )
# check for existence of jpegtrans
if ( ! -x $JPEGTRAN ) {
warn "Please install ImageMagick, and re-run this script.\n";
} # if ( ! -x $JPEGTRAN )
if ( exists $opts{f} &&
( exists $opts{1} || exists $opts{2} || exists $opts{9} ) ) {
# pull the file date for putting back on later
$filedate = &FileDate($opts{f}, "mtime");
# move the old file to a new file with a temp name
warn "rotate.pl: creating tempfile \"tmp$opts{f}\"\n" if ! $QUIET;
rename($opts{f}, "tmp$opts{f}");
# check for half size/thumbnail images as well
# get the base of the filename
# set up basename variable first
$basename = $opts{f};
$basename =~ s/\.jpg$//i;
# now if it exists, move it
if ( -r "$basename.half.jpg" ) {
rename("$basename.half.jpg", "tmp$basename.half.jpg");
warn " : halfsize file found, renaming\n" if ! $QUIET;
} # if ( exists "$basename.half.jpg" )
if ( -r "$basename.8th.jpg" ) {
rename("$basename.8th.jpg", "tmp$basename.8th.jpg");
warn " : eighthsize file found, renaming\n" if ! $QUIET;
} # if ( exists "$basename.8th.jpg" )
# how far are we rotating?
if ( exists $opts{1} ) {
# rotate 180
warn "rotate.pl: Rotating $opts{f} 180 degrees\n" if ! $QUIET;
$rotate_direction = "180";
} elsif ( exists $opts{2} ) {
#rotate 270
warn "rotate.pl: Rotating $opts{f} 270 degrees\n" if ! $QUIET;
$rotate_direction = "270";
} elsif ( exists $opts{9} ) {
#rotate 90
warn "rotate.pl: Rotating $opts{f} 90 degrees\n" if ! $QUIET;
$rotate_direction = "90";
} # if ( exists $opts{1} )
# rotate the original file; build the system string
$systemstr = qq($JPEGTRAN -rotate "$rotate_direction");
$systemstr .= " tmp$opts{f} $opts{f}";
# now do it
if ($DEBUG) {
warn "rotate.pl: would have called jpegtrans with:\n";
warn $systemstr . "\n";
} else {
system($systemstr) == 0 or die "rotate failed: $?";
} # if ($DEBUG)
# got a half size file?
if ( -r "tmp$basename.half.jpg" ) {
# yep, rotate it
warn "rotate.pl: half file found, rotating\n" if ! $QUIET;
$systemstr = "$JPEGTRAN -rot $rotate_direction";
$systemstr .= " -trim tmp$basename.half.jpg";
$systemstr .= " > $basename.half.jpg";
system($systemstr) == 0 or die "rotate failed: $?";
} # if (exists "$basename.half.jpg" )
# got a 8th size file?
if ( -r "tmp$basename.8th.jpg" ) {
# yep, rotate it
warn "rotate.pl: eigth file found, rotating\n" if ! $QUIET;
$systemstr = "$JPEGTRAN -rot $rotate_direction";
$systemstr .= " -trim tmp$basename.8th.jpg";
$systemstr .= " > $basename.8th.jpg";
system($systemstr) == 0 or die "rotate failed: $?";
} # if (exists "$basename.8th.jpg" )
# remove the temp file
warn "rotate.pl: deleting \"tmp$opts{f}\"\n" if ! $QUIET;
if (! $DEBUG) { unlink("tmp$opts{f}");}
# check for and delete the smaller files as well
if ( -r "tmp$basename.half.jpg") {
if (! $DEBUG) { unlink("tmp$basename.half.jpg"); }
} # if (exists "tmp$basename.half.jpg")
if ( -r "tmp$basename.8th.jpg") {
if (! $DEBUG) { unlink("tmp$basename.8th.jpg"); }
} # if (exists "tmp$basename.8th.jpg")
# re-date the newly rotated file
warn "rotate.pl: resetting timestamp on $opts{f}\n" if ! $QUIET;
utime($filedate, $filedate, $opts{f});
} else {
&ShowHelp;
exit 0;
} # if ( exists $opts{f} &&
### end of main script ###
############
# FileDate #
############
sub FileDate {
# pulls file's mtime using stat()
@filestat = stat($_[0]);
if ( $_[1] eq "year") {
$filedate = (localtime($filestat[9]))[5] + 1900;
} elsif ( $_[1] eq "mtime" ) {
$filedate = $filestat[9];
} else {
$filedate = localtime($filestat[9]);
} # if ( $_[1] eq "year")
return $filedate;
} # sub filedate
############
# ShowHelp #
############
sub ShowHelp {
# shows the help message
warn "Usage: rotate.pl [options]\n";
warn "[options] may consist of\n";
warn " -h show this help\n";
warn " -d run in debug mode (extra noisy output)\n";
warn " -2 rotate 270 degrees (90 degrees counterclockwise)\n";
warn " -1 rotate 180 degrees (flip picture on it's vertical axis)\n";
warn " -9 rotate 90 degrees (90 degrees clockwise)\n";
warn " -f filename of the image to rotate\n";
} # sub ShowHelp
# end of line