-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.php
322 lines (286 loc) · 10.8 KB
/
image.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/**
* The Image Manupulation class
* Currently can only rotate and flip an image
* More will be added soon
* @author Swashata <[email protected]>
* @license Free
*/
class ImageManipulation {
/**
* The image container variable
* Should be false if no image present
* Otherwise should hold the GD Image Object
* @var &gd.return.identifier
*/
public $img;
/**
* The type of the image loaded
* Should be false or any of the jpg|jpeg|png|gif
* @var string
*/
public $type;
/**
* The constructor function
*
* Set the default false value to both the variable
*/
public function __construct() {
$this->img = false;
$this->type = false;
}
/**
* The loader function
* Loads the image from a file to the $this->img variable
*
* Also checks if the file exists and of valid image type by checking the extension
* @param string $filename The path of the file to be loaded
* @return boolean true on success, false otherwise
*/
public function load($filename) {
//first check if the file exists
if(!file_exists($filename))
return false;
//extract the extension & thereby check if valid image
$ext = '';
//we check the extension, it can be 4 characters (for jpeg) and must be atleast 3 characters
if(preg_match('/.*\.([A-Za-z]?[A-Za-z]{3})$/', $filename, $ext)) {
//var_dump($ext);
$ext = $ext[1];
//var_dump($ext);
} else {
//invalid image
return false;
}
$this->type = $ext;
switch($ext) {
case 'jpg' :
case 'jpeg' :
$this->img = imagecreatefromjpeg($filename);
break;
case 'png' :
$this->img = imagecreatefrompng($filename);
break;
case 'gif' :
$this->img = imagecreatefromgif($filename);
break;
default :
return false; //failure on invalid extension
}
imagealphablending($this->img, true);
imagesavealpha($this->img, true);
return true; //if we are at this point, then everything went fine
}
/**
* Saves the image in a file
*
* The image from img variable is saved
*
* @param string $filename The path where the file is to be saved
* @param string $type The type of the saved file. Use inherit for original, or use jpg|jpeg|png|gif
* @param int $jpeg_quality The quality of the jpeg image (used only if being saved as jpeg), 0 - 100 (max quality)
* @param int $png_compression The compression of the PNG image (used only if being saved as png), 0 (max quality, min compression) - 9
* @return boolean true on success, false otherwise
*/
public function save_image($filename, $type = 'inherit', $jpeg_quality = 100, $png_compression = 0) {
$ext = 'inherit' == $type ? $this->type : $type;
switch($ext) {
case 'jpg' :
case 'jpeg' :
imagejpeg($this->img, $filename, $jpeg_quality);
break;
case 'png' :
imagepng($this->img, $filename, $png_compression = 0);
break;
case 'gif' :
imagegif($this->img, $filename);
break;
default :
return false;
}
return true;
}
/**
* Output the image directly into the browser.
* Also stops execution of other scripts by calling die() function
*
* @uses die
* @param string $type The type of the saved file. Use inherit for original, or use jpg|jpeg|png|gif
* @param int $jpeg_quality The quality of the jpeg image (used only if being saved as jpeg), 0 - 100 (max quality)
* @param int $png_compression The compression of the PNG image (used only if being saved as png), 0 (max quality, min compression) - 9
* @return boolean false on failure
*/
public function output($type = 'inherit', $jpeg_quality = 100, $png_compression = 0) {
if($this->img == false)
return false;
header('Content-type: image/' . $this->type);
$this->save_image(null, $type, $jpeg_quality, $png_compression);
switch($this->type) {
case 'jpg' :
case 'jpeg' :
imagejpeg($this->img, null, $jpeg_quality);
break;
case 'png' :
imagepng($this->img, null, $png_compression);
break;
case 'gif' :
imagegif($this->img);
break;
default :
return false;
}
die();
}
/**
* Flip the image horizontally, vertically or both
*
* Manipulates the image object saved in the img variable
*
* @param string $type Flipping type, can be hori|horizontal|vert|vertical|both
* @return boolean true on success, false on failure
*/
public function flip_image($type) {
//little bit of error check
if(false === $this->img) {
return false;
}
//first get the height and width
$width = imagesx($this->img);
$height = imagesy($this->img);
//create the empty destination image
$dest = imagecreatetruecolor($width, $height);
imagealphablending($dest, false);
imagesavealpha($dest, true);
//now work with the type and do the necessary flipping
switch($type) {
case 'vert' : //vertical flip
case 'vertical' :
for($i = 0; $i < $height; $i++) {
/**
* What we do here is pixel wise row flipping
* The first row of pixels of the source image (ie, when $i = 0)
* goes to the last row of pixels of the destination image
*
* So, mathematically, for the row $i of the source image
* the corresponding row of the destination should be
* $height - $i - 1
* -1, because y and x both co-ordinates are calculated from zero
*/
imagecopy($dest, $this->img, 0, ($height - $i - 1), 0, $i, $width, 1);
}
break;
case 'hori' : //horizontal flip
case 'horizontal' :
for($i = 0; $i < $width; $i++) {
/**
* Here we apply the same logic for other direction
* The first column of pixels of the source image
* goes to the last column of pixels of the destination image
*
* So, for the $i -th column of the source
* the column of the destination would be
* $width - $i - 1
*/
imagecopy($dest, $this->img, ($width - $i - 1), 0, $i, 0, 1, $height);
}
break;
case 'both' :
//we simply return using recursive call
if($this->flip_image('horizontal') && $this->flip_image('vertical'))
return true;
else
return false;
break;
default :
return false;
}
//now make the changes
imagedestroy($this->img);
$this->img = $dest;
return true;
}
/**
* Rotate the image to the given angle, filled with given color and alpha
*
* Rotates in clockwise direction and takes hexadecimal color code as input
*
* @param int $angle The rotational angle
* @param string $bgd_color The background color code in hex. Optional, default is ffffff (white)
* @param int $alpha The alpha value, 0 for opaque, 127 for transparent, anything between for translucent
* @return void
*/
public function rotate_image($angle, $bgd_color = 'ffffff', $alpha = 0) {
if($angle == 0)
return;
$angle = abs($angle);
//make the value for clockwise rotation
$r_angle = 360 - ($angle % 360);
extract($this->hex_to_rgb($bgd_color));
$color = imagecolorallocatealpha($this->img, $red, $green, $blue, $alpha);
$dest = imagerotate($this->img, $r_angle, $color);
if(false !== $dest) {
imagealphablending($dest, true);
imagesavealpha($dest, true);
imagedestroy($this->img);
$this->img = $dest;
}
}
/**
* Converts hexadecimal to RGB color array
* @link http://www.anyexample.com/programming/php/php_convert_rgb_from_to_html_hex_color.xml
* @uses hexdec http://php.net/manual/en/function.hexdec.php
* @access private
* @param string $color 6 or 3 character long hexadecimal code
* @return array with red, green, blue keys and corresponding values
*/
private function hex_to_rgb($color) {
if($color[0] == '#')
$color = substr($color, 1);
if(strlen($color) == 6) {
list($r, $g, $b) = array(
$color[0].$color[1],
$color[2].$color[3],
$color[4].$color[5]
);
} elseif (strlen($color) == 3) {
list($r, $g, $b) = array(
$color[0].$color[0],
$color[1].$color[1],
$color[2].$color[2]
);
} else {
return array('red' => 255, 'green' => 255, 'blue' => 255);
}
$r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
return array('red' => $r, 'green' => $g, 'blue' => $b);
}
}
/**
* Usage demonstration
*/
/**
* Set expire for the demo
*/
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
//get out GET parameters (safely)
$ro = isset($_GET['ro']) ? (int) $_GET['ro'] : 0;
$fl = isset($_GET['fl']) ? $_GET['fl'] : null;
$type = isset($_GET['type']) ? $_GET['type'] : 'jpg';
if(!in_array($type, array('jpg', 'png', 'gif')))
$type = 'jpg';
//load the image
$img = new ImageManipulation();
$img->load('linux.' . $type);
if(!empty($ro)) {
$img->rotate_image($ro, 'ffffff', 127);
}
if(!empty($fl) && 'none' != $fl) {
$img->flip_image($fl);
}
$img->output();
/**
* Icons from IconShock
* <a href="http://www.iconshock.com">icon sets by iconshock</a><br />
*/
?>