-
Notifications
You must be signed in to change notification settings - Fork 1
/
IMA.php
256 lines (223 loc) · 5.78 KB
/
IMA.php
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
// CLI Version of Zebra_Image by aancw
// https://github.com/aancw/IMA-PHP
// This file need Zebra_Image library
// Thanks to Zebra_Image. Github : https://github.com/stefangabos/Zebra_Image/
require dirname(__FILE__).'/Zebra_Image/Zebra_Image.php';
$argument1 = "";
if( isset($argv[1]) )
$argument1 = $argv[1];
function CheckArgumentIsEmpty( $argument )
{
if( empty($argument) )
return true;
else
return false;
}
function ShowHelp( )
{
echo "Run like this \"php IMP.php -f <Full Filename Path> -m <method>\"\n";
echo "Method :\n";
echo "1. Cropping Image\n";
echo "2. Resize Image\n";
echo "3. Rotate Image\n";
echo "4. Flipped Vertically\n";
echo "5. Flipped Horizontally\n";
echo "6. Sepia Filter\n";
echo "Choose what you want : ";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
switch( trim($line) )
{
case 1:
echo "Run php IMA.php -f <Full Filename Path> -m 1 <start_x>,<start_y>,<end_x>,<end_y>\n";
echo "start_x\tx coordinate to start cropping from\n";
echo "start_y\ty coordinate to start cropping from\n";
echo "end_x\tx coordinate where to end the cropping\n";
echo "end_y\ty coordinate where to end the cropping\n";
break;
case 2:
echo "Run php IMA.php -f <Full Filename Path> -m 2 <width>,<height>\n";
echo "Width\tWidth base on pixel\n";
echo "Height\tHeight base on pixel\n";
break;
case 3:
echo "Run php IMA.php -f <Full Filename Path> -m 3 <DegreesValue>\n";
break;
case 4:
echo "Run php IMA.php -f <Full Filename Path> -m 4 \n";
break;
case 5:
echo "Run php IMA.php -f <Full Filename Path> -m 5 \n";
break;
case 6:
echo "Run php IMA.php -f <Full Filename Path> -m 6 \n";
break;
default:
echo "Nothing to do here";
break;
}
exit;
}
function show_error($error_code, $source_path, $target_path)
{
// if there was an error, let's see what the error is about
switch ($error_code) {
case 1:
echo 'Source file "' . $source_path . '" could not be found!';
break;
case 2:
echo 'Source file "' . $source_path . '" is not readable!';
break;
case 3:
echo 'Could not write target file "' . $source_path . '"!';
break;
case 4:
echo $source_path . '" is an unsupported source file format!';
break;
case 5:
echo $target_path . '" is an unsupported target file format!';
break;
case 6:
echo 'GD library version does not support target file format!';
break;
case 7:
echo 'GD library is not installed!';
break;
case 8:
echo '"chmod" command is disabled via configuration!';
break;
}
}
function ImageProcessing($filenamePath, $method, $option)
{
$image = new Zebra_Image();
// indicate a source image
$image->source_path = $filenamePath;
$ext = substr($image->source_path, strrpos($image->source_path, '.') + 1);
$filename = basename( $filenamePath, ".".$ext);
$methodText = "";
$methodInfo = "";
$width = 0;
$height = 0;
$sy = 0;
$sx = 0;
$ey = 0;
$ex = 0;
$degrees = 0;
switch( $method )
{
case 1:
$methodText = "crop";
$methodInfo = "Cropping";
break;
case 2:
$methodText = "resize";
$methodInfo = "Resizing";
break;
case 3:
$methodText = "rotate";
$methodInfo = "Rotating";
break;
case 4:
$methodText = "flip-v";
$methodInfo = "Flip vertically";
break;
case 5:
$methodText = "flip-h";
$methodInfo = "Flip horizontally";
break;
case 6:
$methodText = "sepia";
$methodInfo = "Sepia filter";
break;
default:
echo "Nothing to do here";exit;
break;
}
echo "IMA-PHP by aancw\n";
echo "Zebra_Image CLI Version\n";
echo "Processing image " . $image->source_path ."\n";
$image->target_path = 'results/'.$filename. "-" .$methodText. "." . $ext;
$exp = explode(',',$option) ;
if( $methodText === "crop" )
{
if( isset($exp[0]) && isset($exp[1]) && isset($exp[2]) && isset($exp[3]) )
{
$sx = $exp[0];
$sy = $exp[1];
$ey = $exp[2];
$ex = $exp[3];
}
if (!$image->crop($sx, $sy, $ey, $ex)) show_error($image->error, $image->source_path, $image->target_path);
}
elseif( $methodText === "resize" )
{
if( isset($exp[0]) && isset($exp[1]) )
{
$width = $exp[0];
$height = $exp[1];
}
if (!$image->resize($width, $height, ZEBRA_IMAGE_BOXED, -1)) show_error($image->error, $image->source_path, $image->target_path);
}
elseif( $methodText === "rotate" )
{
if( isset($exp[0]) )
{
$degrees = $exp[0];
if (!$image->rotate($degrees)) show_error($image->error, $image->source_path, $image->target_path);
}
}elseif( $methodText === "flip-v" )
{
if (!$image->flip_vertical()) show_error($image->error, $image->source_path, $image->target_path);
}elseif( $methodText === "flip-h" )
{
if (!$image->flip_horizontal()) show_error($image->error, $image->source_path, $image->target_path);
}elseif( $methodText === "sepia" )
{
$image->apply_filter(array(
array('grayscale'),
array('colorize', 90, 60, 40),
));
}
echo $methodInfo . " image is done and result has been saved to " .dirname(__FILE__)."/".$image->target_path;
}
if (!is_dir('results') || !is_writable('results'))
{
echo "Please create the results folder at ".dirname(__FILE__)." and make sure it is writable!\n";
exit;
}else
{
if( CheckArgumentIsEmpty( $argument1 ) )
ShowHelp();
else
{
$filePath = "";
$method = 0;
$optionValue = "";
for($i = 0;$i < 6;$i++)
{
if( isset( $argv[$i] ) )
{
if( $argv[$i] === "-f" )
{
if( isset($argv[$i+1]) )
$filePath = $argv[$i+1];
}
if( $argv[$i] === "-m" )
{
if( isset($argv[$i+1]) && isset($argv[$i+2]) )
if( is_numeric( $argv[$i+1] ) )
{
$method = $argv[$i+1];
$optionValue = $argv[$i+2];
}
}
if( $argv[$i] === "-h" )
ShowHelp();
}
}
ImageProcessing($filePath, $method, $optionValue);
}
}
?>