-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageManipulation.class.php
More file actions
467 lines (436 loc) · 18.2 KB
/
ImageManipulation.class.php
File metadata and controls
467 lines (436 loc) · 18.2 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<?php
/**
* Image Manipulation Library
*
* This library is used for image manipulation functionalities
* like "Fit", "Fill", "Stretch", "Center". We have developed
* this image library in flexible and very user-friendly
* usage.
*
* @author Siva Perumal <siva7170430@gmail.com>
* @author Selva Vignesh <selvavignesh128@gmail.com>
* @version 1.1
* @license Open Source
*/
class ImageManipulation{
private $file_from="";
private $image_transparent=true;
private $file_type="";
private $manipulation_action="";
private $result=false;
private $msg="";
// image file original
private $original_filename="";
private $original_filesize="";
private $original_filewidth="";
private $original_fileheight="";
private $original_fileaspect_ratio="";
private $original_filepath=""; // always be temp file if it is uploaded; Otherwise, it will be path or URL
private $original_filetype="";
// image file original (manipulated)
private $modified_filex="";
private $modified_filey="";
private $modified_filewidth="";
private $modified_fileheight="";
private $modified_fileaspect_ratio="";
private $modified_filepath=""; // always be temp file if it is uploaded; Otherwise, it will be path or URL
// image file processed/processing (destination)
private $proc_filename="";
private $proc_filesize="";
private $proc_filewidth="";
private $proc_fileheight="";
private $proc_fileaspect_ratio="";
private $proc_filequality=""; // in percentage
private $proc_filepath="";
// image file processed/processing workspace (white paper)
private $whitepaper_width="";
private $whitepaper_height="";
private $whitepaper_aspect_ratio="";
private $whitepaper_color=[255,255,255];
private $transparency_color=[0,0,0,127];
// some constant
const IMAGE_TYPE_PNG="png";
const IMAGE_TYPE_JPEG="jpeg";
const IMAGE_TYPE_JPG="jpg"; // no use
const IMAGE_TYPE_GIF="gif";
const IMAGE_ACTION_FIT="fit";
const IMAGE_ACTION_FILL="fill";
const IMAGE_ACTION_CENTER="center";
const IMAGE_ACTION_STRETCH="stretch";
/**
* ImageManipulation constructor.
*
* This library usage:
*
* $image=new ImageManipulation();
* $image->ImageFrom("image.jpg");
* $test=$image->IsOkay();
* echo var_dump($test[0])."-".$test[1];
* $image->ImageType(ImageManipulation::IMAGE_TYPE_GIF);
* $image->ImagePreserverTransparent(true);
* $image->ImageManipulationAction(ImageManipulation::IMAGE_ACTION_FILL);
* $image->ImageQuality(80);
* $image->ImageResize(1000,1000);
* $image->WorkSpaceColor(0,255,0);
* $image->Output("../img1.gif");
*/
public function __construct()
{
$this->file_type=self::IMAGE_TYPE_JPG;
$this->manipulation_action=self::IMAGE_ACTION_FILL;
$this->result=false;
$this->whitepaper_color=[255,255,255];
$this->transparency_color=[0,0,0,127];
$this->msg="Please select image file path/URL!";
}
/**
* We should pass path of image into this method
*
* @param string $file_from Image file path
*/
public function ImageFrom($file_from){
$this->file_from=$file_from;
if(!file_exists($this->file_from)){
$this->result=false;
$this->msg="File is not found!";
}
else{
$file_mime=exif_imagetype($this->file_from);
if($file_mime!=false){
switch($file_mime){
case IMAGETYPE_GIF:
$this->original_filetype=self::IMAGE_TYPE_GIF;
break;
case IMAGETYPE_JPEG:
$this->original_filetype=self::IMAGE_TYPE_JPEG;
$this->proc_filequality=90;
break;
case IMAGETYPE_PNG:
$this->original_filetype=self::IMAGE_TYPE_PNG;
$this->proc_filequality=8;
break;
default:
$this->result=false;
$this->msg="Unsupported MIME types!";
}
$temp_array=[self::IMAGE_TYPE_GIF,self::IMAGE_TYPE_JPEG,self::IMAGE_TYPE_PNG];
if(in_array($this->original_filetype,$temp_array)){
$this->result=true;
$this->msg="Input file is found!";
}
else{
$this->result=false;
$this->msg="Unsupported file!";
}
}
else{
$this->result=false;
$this->msg="Unsupported file!";
}
}
}
/**
* This method will return the status of the functionality
* whether is going good or not.
*
* @return array
*/
public function IsOkay(){
return [$this->result,$this->msg];
}
/**
* We may set the file type either PNG or GIF or JPG.
* This image library will convert the image type to
* this type.
*
* Default: JPG
*
* @param string $file_type File type : Use ImageManipulation::IMAGE_TYPE_GIF, ImageManipulation::IMAGE_TYPE_JPG, ImageManipulation::IMAGE_TYPE_JPEG, ImageManipulation::IMAGE_TYPE_PNG
*/
public function ImageType($file_type){
$this->file_type=$file_type;
}
/**
* If you use final file type is PNG/GIF, the you may set
* this option to preserve the transparency of the image
*
* Default: true
*
* @param bool $transparent Set either TRUE or FALSE
*/
public function ImagePreserverTransparent($transparent){
$this->image_transparent=$transparent;
}
/**
* We may set the image manipulation action here. This library has four
* actions. The output of the image will be based on this action selection
*
* Default: ImageManipulation::IMAGE_ACTION_FILL
*
* @param string $file_action File action : Use ImageManipulation::IMAGE_ACTION_FIT or ImageManipulation::IMAGE_ACTION_FILL or ImageManipulation::IMAGE_ACTION_CENTER or ImageManipulation::IMAGE_ACTION_STRETCH
*/
public function ImageManipulationAction($file_action){
$this->manipulation_action=$file_action;
}
/**
* We may also set image quality. Based on this image
* quality value, final image output will be generated
*
* Note: There is no quality option provided for GIF
* image output.
*
* Default: 90 for JPEG; 8 for PNG
*
* @param int $file_quality Pass between 0 to 100 for JPEG; Pass 0 to 9 for PNG
*/
public function ImageQuality($file_quality){
$this->proc_filequality=$file_quality;
}
/**
* This method used to specify the image width & height.
*
* @param int $file_resize_width Final output image width
* @param int $file_resize_height Final output image height
*/
public function ImageResize($file_resize_width, $file_resize_height){
$this->proc_filewidth=$file_resize_width;
$this->proc_fileheight=$file_resize_height;
}
/**
*
*/
private function calculation(){
list($this->original_filewidth, $this->original_fileheight)=getimagesize($this->file_from);
$sw=$this->original_filewidth;
$sh=$this->original_fileheight;
$sar=($sw/$sh);
$dw=$this->proc_filewidth;
$dh=$this->proc_fileheight;
$dar=($dw/$dh);
if($this->manipulation_action==self::IMAGE_ACTION_FIT){
$temp_sw=$dw;
$temp_sh=($temp_sw/$sar);
if($dh>$temp_sh){
$this->modified_filewidth=$temp_sw;
$this->modified_fileheight=$temp_sh;
$this->modified_filex=0;
$this->modified_filey=($this->proc_fileheight-$this->modified_fileheight)/2;
}
else{
$temp_sh=$dh;
$temp_sw=($temp_sh*$sar);
if($dw>$temp_sw){
$this->modified_filewidth=$temp_sw;
$this->modified_fileheight=$temp_sh;
$this->modified_filex=($this->proc_filewidth-$this->modified_filewidth)/2;
$this->modified_filey=0;
}
else{
$this->modified_filewidth=$this->proc_filewidth;
$this->modified_fileheight=$this->proc_fileheight;
$this->modified_filex=0;
$this->modified_filey=0;
}
}
}
elseif($this->manipulation_action==self::IMAGE_ACTION_FILL){
if($sar<$dar){
$temp_sw=$dw;
$temp_sh=($temp_sw/$sar);
$this->modified_filewidth=$temp_sw;
$this->modified_fileheight=$temp_sh;
$this->modified_filex=0;
$this->modified_filey=($this->proc_fileheight-$this->modified_fileheight)/2;
}
elseif($sar>$dar){
$temp_sh=$dh;
$temp_sw=($temp_sh*$sar);
$this->modified_filewidth=$temp_sw;
$this->modified_fileheight=$temp_sh;
$this->modified_filex=($this->proc_filewidth-$this->modified_filewidth)/2;
$this->modified_filey=0;
}
else{
$this->modified_filewidth=$this->proc_filewidth;
$this->modified_fileheight=$this->proc_fileheight;
$this->modified_filex=0;
$this->modified_filey=0;
}
}
elseif($this->manipulation_action==self::IMAGE_ACTION_CENTER){
$this->modified_filewidth=$sw;
$this->modified_fileheight=$sh;
$this->modified_filex=($this->proc_filewidth-$this->modified_filewidth)/2;
$this->modified_filey=($this->proc_fileheight-$this->modified_fileheight)/2;
}
elseif($this->manipulation_action==self::IMAGE_ACTION_STRETCH){
$this->modified_filewidth=$this->proc_filewidth;
$this->modified_fileheight=$this->proc_fileheight;
$this->modified_filex=0;
$this->modified_filey=0;
}
}
/**
* This will execute final process of this image library
* It will automatically generate image based on all values
* given to this library and default values.
*
* Default: (empty)
*
* @param string $output (Optional) If you want to show final output to browser, please leave this parameter to empty. Otherwise, please specify the path to write on disk.
* @param bool $file_overwrite (Optional) If true, file will be overwritten. If no, file will not overwrite but error will show at the end.
* @throws Exception When image class does not meet sufficient to proceed next, we will throw that issue/missing requirement.
*/
public function Output($output="",$file_overwrite=true){
$this->proc_filepath=$output;
if($this->result!=false){
if($this->proc_filepath!=""){
$outputPath=pathinfo($this->proc_filepath, PATHINFO_DIRNAME);
if(file_exists($outputPath)){
$this->result=true;
$this->msg="Input file is found. Output path is found";
if(file_exists($this->proc_filepath) && !$file_overwrite){
$this->result=false;
$this->msg="Input file is found. ";
}
elseif(file_exists($this->proc_filepath) && $file_overwrite){
$this->result=true;
$this->msg="Input file is found. Output path is found and there is file exist with this name. So, it will overwrite that file.";
}
else{
$this->result=true;
$this->msg="Input file is found. Output path is found and there is no file with this name.";
}
if($this->proc_filewidth<1){
$this->result=false;
$this->msg="Output file width should have at least 1 or more pixels length";
}
if($this->proc_fileheight<1){
$this->result=false;
$this->msg="Output file height should have at least 1 or more pixels length";
}
if($this->proc_fileheight<1){
$this->result=false;
$this->msg="Output file height should have at least 1 or more pixels length";
}
if($this->original_filetype==self::IMAGE_TYPE_JPEG){
if($this->proc_filequality>100 || $this->proc_filequality<0)
{
$this->result=false;
$this->msg="JPEG image quality should be range between 0 to 100";
}
}
if($this->original_filetype==self::IMAGE_TYPE_PNG){
if($this->proc_filequality>9 || $this->proc_filequality<0)
{
$this->result=false;
$this->msg="PNG image quality should be range between 0 to 9";
}
}
}
else{
$this->result=false;
$this->msg="Input file is found.";
}
}
else{
$this->result=true;
$this->msg="Input file is found. Output path is not specified.";
}
if($this->result!=false){
$this->calculation();
$this->ImageProceedAndOutput();
}
else{
throw new Exception($this->msg);
}
}
}
/**
*
*/
private function ImageProceedAndOutput(){
$dstimage=imagecreatetruecolor($this->proc_filewidth,$this->proc_fileheight);
if(!$this->image_transparent || $this->original_filetype!=self::IMAGE_TYPE_PNG){
$white = imagecolorallocate($dstimage, $this->whitepaper_color[0], $this->whitepaper_color[1], $this->whitepaper_color[2]);
imagefill($dstimage,0,0,$white);
}
switch($this->original_filetype){
case self::IMAGE_TYPE_PNG:
$srcimage=imagecreatefrompng($this->file_from);
if($this->image_transparent){
$transparent=imagecolorallocatealpha($dstimage, $this->transparency_color[0], $this->transparency_color[1], $this->transparency_color[2], $this->transparency_color[3]);
imagefill($dstimage,0,0,$transparent);
imagesavealpha($dstimage, true);
imagealphablending($dstimage, true);
}
break;
case self::IMAGE_TYPE_JPEG:
$srcimage=imagecreatefromjpeg($this->file_from);
break;
case self::IMAGE_TYPE_GIF:
$srcimage=imagecreatefromgif($this->file_from);
if($this->image_transparent){
$transparent=imagecolorallocatealpha($dstimage, $this->transparency_color[0], $this->transparency_color[1], $this->transparency_color[2], $this->transparency_color[3]);
imagefill($dstimage,0,0,$transparent);
imagesavealpha($dstimage, true);
imagealphablending($dstimage, false);
}
break;
}
imagecopyresampled($dstimage,$srcimage,$this->modified_filex,$this->modified_filey,0,0,$this->modified_filewidth,$this->modified_fileheight, $this->original_filewidth,$this->original_fileheight);
if($this->proc_filepath==""){
switch($this->file_type){
case self::IMAGE_TYPE_PNG:
header("Content-type: image/png");
imagepng($dstimage);
break;
case self::IMAGE_TYPE_JPEG:
case self::IMAGE_TYPE_JPG:
header("Content-type: image/jpeg");
imagejpeg($dstimage);
break;
case self::IMAGE_TYPE_GIF:
header("Content-type: image/gif");
imagegif($dstimage);
break;
}
}
else{
switch($this->file_type){
case self::IMAGE_TYPE_PNG:
imagepng($dstimage,$this->proc_filepath,$this->proc_filequality);
break;
case self::IMAGE_TYPE_JPEG:
case self::IMAGE_TYPE_JPG:
imagejpeg($dstimage,$this->proc_filepath,$this->proc_filequality);
break;
case self::IMAGE_TYPE_GIF:
imagegif($dstimage,$this->proc_filepath);
break;
}
}
}
/**
* This is workspace color. You can see this color on
* ImageManipulation::IMAGE_ACTION_FIT
*
* @param int $red Specify color value from 0 to 255
* @param int $green Specify color value from 0 to 255
* @param int $blue Specify color value from 0 to 255
*/
public function WorkSpaceColor($red, $green, $blue){
$this->whitepaper_color=[$red,$green,$blue];
}
/**
* This is transparency color of PNG or GIF images.
*
* @param int $red Specify color value from 0 to 255
* @param int $green Specify color value from 0 to 255
* @param int $blue Specify color value from 0 to 255
* @param int $alpha Specify alpha/opacity value from 0 to 255
*/
public function TransparencyColor($red, $green, $blue,$alpha){
$this->transparency_color=[$red,$green,$blue,$alpha];
}
}