forked from surebert/surebert-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.php
More file actions
429 lines (336 loc) · 9.45 KB
/
Image.php
File metadata and controls
429 lines (336 loc) · 9.45 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php
/**
* This class is used to edit images.
*
* @author Paul Visco 10/26/2005
* @version 2.0 07/08/2007
* @package sb_Image
*
*
*/
class sb_Image{
/**
* The file path to the original image file being edited
*
* @var string
*/
public $path;
/**
* A resource pointer to the edited image resource
*
* @var resource
*/
public $edited; //a pointer to the edited image
/**
* A resource pointer to the original image resource
*
* @var resource
*/
public $original; // a pointer to the original image
/**
* An array of height data both original and dest
*
* @var array
*/
private $height = array(
'orig' =>0,
'dest' =>0
);
/**
* An array of width data both original and dest
*
* @var array
*/
private $width = array(
'orig' =>0,
'dest' =>0
);
/**
* The type of image file that is being manipulated as determined by $this->get_info
*
* @var string
*/
private $type; //gif, jpg, png
/**
* optionally sets the image by passing arguments to $this->set
*
* @param string $orig the file path to the image being edited
* @param string $dest optional, the file path to name the edited file should be saved as, without this the original file gets saved over with the edited version
* <code>
* $sb_Image = new sb_Image('orig.jpg', 'orig3.jpg');
*
* //$sb_Image->to_grayscale();
* $sb_Image->resize(200, -1);
* $sb_Image->display();
* $sb_Image->force_download();
* //$sb_Image->rotate(90);
* //$sb_Image->to_jpg();
*</code>
*/
public function __construct($orig='', $dest=''){
if(!empty($orig)){
$this->set($orig, $dest='');
}
}
/**
* Sets the image being edited
*
* @param string $orig the file path to the image being edited
* @param string $dest optional, the file path to name the edited file should be saved as, without this the original file gets saved over with the edited version
*/
public function set($orig, $dest=''){
if(!empty($dest)){
copy($orig, $dest);
$orig = $dest;
}
$this->path = $orig;
$this->get_info();
}
/**
* Gets the image file type, width, and height
*
*/
public function get_info(){
$file_info = @getimagesize($this->path);
//define the original width of the image
$this->width['orig'] = $file_info['0'];
//define the original height of the image
$this->height['orig'] = $file_info['1'];
//image type //1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM
switch ($file_info[2])
{
case "1":
$this->type = "gif";
break;
case "2":
$this->type = "jpg";
break;
case "3":
$this->type = "png";
break;
}
//////////////////////////
$this->original = imagecreatefromstring(file_get_contents($this->path));
}
public function copy($destination){
copy($this->path, $destination);
}
/**
* Dump the image data to the screen
*
*/
public function debug(){
//echo dimensions
echo '<pre>'.print_r($this, 1).'</pre>';
}
/**
* Resizes an the edited image to the specified width and height
*
* @param int $width can be * to make it relative to a specified height
* @param int $height can be * to make it relative to a specified width
*/
public function resize($width, $height){
//if the width is not specified, make it relative to the height
if($width == -1){
$this->width['dest'] = ($height * $this->width['orig']) / $this->height['orig'];
$this->height['dest'] = $height;
//if the height is not specified, make it relative to the width
} else if ($height == -1){
$this->width['dest'] = $width;
$this->height['dest'] = ($width * $this->height['orig']) / $this->width['orig'];
} else {
$this->width['dest'] = $width;
$this->height['dest'] = $height;
}
//set resize code depending on the type of image it is
switch ($this->type)
{
case "gif":
$this->edited = imagecreate($this->width['dest'], $this->height['dest']);
imagecopyresampled($this->edited, $this->original, 0, 0, 0, 0, $this->width['dest'], $this->height['dest'], $this->width['orig'], $this->height['orig']);
break;
case "jpg":
$this->edited = imagecreatetruecolor($this->width['dest'], $this->height['dest']);
imagecopyresampled($this->edited, $this->original, 0, 0, 0, 0, $this->width['dest'], $this->height['dest'], $this->width['orig'], $this->height['orig']);
break;
case "png":
$this->edited = imagecreatetruecolor ($this->width['dest'], $this->height['dest']);
//preserve the alpha if exists
imagealphablending($this->edited, false);
imagesavealpha($this->edited, true);
imagecopyresampled($this->edited, $this->original, 0, 0, 0, 0, $this->width['dest'], $this->height['dest'], $this->width['orig'], $this->height['orig']);
break;
}
}
/**
* Converts the image being edited to grayscale
*
*/
public function to_grayscale(){
$this->get_info();
$this->edited = imagecreate($this->width['orig'], $this->height['orig']);
//Creates the 256 color palette
for ($c=0;$c<256;$c++)
{
$palette[$c] = imagecolorallocate($this->edited,$c,$c,$c);
}
//Reads the origonal colors pixel by pixel
for ($y=0;$y<$this->height['orig'];$y++)
{
for ($x=0;$x<$this->width['orig'];$x++)
{
$rgb = imagecolorat($this->original,$x,$y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
//This is where we actually use yiq to modify our rbg values, and then convert them to our grayscale palette
$gs = $this->color_to_gray($r,$g,$b);
imagesetpixel($this->edited,$x,$y,$palette[$gs]);
}
}
}
/**
* Converts a color to grayscale
*
* @param int $r 0-255
* @param int $g 0-255
* @param int $b 0-255
* @return float grayscale color
*/
private function color_to_gray($r,$g,$b){
return (($r*0.299)+($g*0.587)+($b*0.114));
}
/**
* Rotates an image the number of degrees specified by $rotation
*
* @param int $rotation
*/
public function rotate($rotation){
if(isset($this->edited)){
echo 'yo';
$this->get_info();
$this->edited = imagerotate($this->edited, $rotation, 0);
} else {
$this->edited = imagerotate($this->original, $rotation, 0);
}
}
/**
* Writes text onto an image
*
* @param string $text the text that is writtenonto the image
* @param int $x the x position of the text on the image
* @param int $y the y position of the text on the image
* @param array $color the color of the text on the image expressed as an array(r,g,b);
*/
/**
* Write text onto an image
*
* @param string $text
* @param array $params color array(r,g,b), size, x, y
*/
public function write($text, $params=array()){
$color = (isset($params['color'])) ? $params['color'] : array(0, 0, 0);
$color = imagecolorallocate($this->edited, $color[0], $color[1], $color[2]);
$size = (isset($params['size']))? $params['size'] : 5;
$x = (isset($params['x']))? $params['x'] : 2;
$y = (isset($params['y']))? $params['y'] : 2;
imagestring($this->edited, $size, $x, $y, $text, $color);
}
/**
* Adds a datestamp to the picture
*
*/
public function date_stamp(){
$this->write(date('m/d/y H:i'), array('size'=>3, 'x'=>2, 'y'=>2, 'color'=>array(0,255,0)));
$this->write(date('m/d/y H:i'), array('size'=>3, 'x'=>3, 'y'=>3, 'color'=>array(0,0,0)));
}
/**
* Saves the image file being edited as a gif
*
*/
public function to_gif(){
imagegif($this->edited, $this->path);
}
/**
* Saves the image file being edited as a jpg
*
*/
public function to_jpg(){
imagejpeg($this->edited, $this->path, 96);
}
/**
* Saves the image file being edited as a png
*
*/
public function to_png(){
imagepng($this->edited, $this->path, 1);
}
/**
* Saves the edited image as a file based on the original images file type
*
*/
public function to_file(){
if ($this->type == "jpg")
{
$this->to_jpg();
} else if ($this->type == "png") {
$this->to_png();
} else if ($this->type == "gif") {
$this->to_gif();
}
}
/**
* Displays the edited image to screen as a dynamic image file
*
*/
public function display(){
if(isset($this->edited )){
$image = $this->edited;
} else {
$image = $this->original;
}
if ($this->type == "jpg")
{
header("Content-type: image/jpeg");
imagejpeg($image);
} else if ($this->type == "png") {
header("Content-type: image/png");
imagepng($image);
} else if ($this->type == "gif") {
header("Content-type: image/gif");
imagegif($image);
}
}
/**
* Forces the image being manipulated to the user as a force download
*
*/
public function force_download(){
if ($this->type == "jpg")
{
$this->to_jpg();
} else if ($this->type == "png") {
$this->to_png();
} else if ($this->type == "gif") {
$this->to_gif();
}
header('Content-Description: File Transfer');
header("Content-Type: application/octet-stream");
header('Content-Length: ' . filesize($this->path));
header('Content-Disposition: attachment; filename=' . basename($this->path));
readfile($this->path);
//remove the temp file
unlink($this->path);
}
/**
* Cleans up the image resources if they exist
*
*/
public function __destruct(){
if(isset($this->original) || isset($this->edited)){
imagedestroy($this->original);
imagedestroy($this->edited);
}
}
}
?>