forked from samayo/bulletproof
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBulletProof.php
More file actions
707 lines (562 loc) · 21 KB
/
BulletProof.php
File metadata and controls
707 lines (562 loc) · 21 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
<?php
namespace ImageUploader;
/**
* BulletProof: A PHP Image Uploader.
*
* You can also: shrink/resize, add watermark, delete or crop Images,
* during and after image uploads.
*
* This class is heavily commented, to be as much friendly as possible.
*
* Please help out by posting out some bugs/flaws if you encounter any. Thanks!
*
* @author Simon _eQ <https://github.com/simon-eQ>
* @license Luke 3:11 ( Free )
* @link https://github.com/simon-eQ/BulletProof
*
*/
class ImageUploaderException extends \Exception
{
}
class BulletProof
{
/*--------------------------------------------------------------------------
| UPLOAD: Image upload class properties |
*--------------------------------------------------------------------------*/
/**
* Set a group of default image types to upload.
*
* @var array
*/
private $fileTypesToUpload = [ "jpg", "jpeg", "png", "gif" ];
/**
* Set a default file size to upload. Values are in bytes. Remember: 1kb ~ 1000 bytes.
*`
* @var array
*/
private $allowedUploadSize = [ "min" => 1, "max" => 30000 ];
/**
* Default & maximum allowed height and width image to upload.
*
* @var array
*/
private $imageDimension = [ "height"=>1000, "width"=>1000 ];
/**
* Set a default folder to upload images, if it does not exist, it will be created.
*
* @var string
*/
private $fileUploadDirectory = "uploads";
/**
* To get the real image/mime type. i.e gif, jpeg, png, ....
*
* @var string
*/
private $getMimeType;
/*--------------------------------------------------------------------------
| IMAGE RESIZE & CROP | Properties |
---------------------------------------------------------------------------*/
/**
* Image dimensions for resizing or shrinking ex: array("height"=>100, "width"=>100)
*
* @var array
*/
private $imageShrinkDimensions = [];
/**
* New image dimensions for image cropping ex: array("height"=>100, "width"=>100)
*
* @var array
*/
private $imageCropDimension = [];
/*-----------------------------------------------------------------------------
| WATERMARK | Image Watermark Properties |
---------------------------------------- -----------------------------------*/
/**
* Name of the image to use as a watermark. ( best to use a png image )
*
* @var
*/
private $getImageToWatermark;
/**
* Watermark Position. (Where to put the watermark). ex: 'center', 'top-right', 'bottom-left'....
*
* @var
*/
private $getWatermarkPosition;
/**
* Size, store ( Width & Height ) of the watermark ex: 'array("height"=>40, "width"=>20)'.
*
* @var
*/
private $getWatermarkDimensions;
/*--------------------------------------------------------------------------
| UPLOADING | General Uploading Methods |
---------------------------------------------------------------------------*/
/**
* For passing the image/mime types to upload.
*
* @param array $fileTypes - ex: ['jpg', 'doc', 'txt'].
* @return $this
*/
public function fileTypes(array $fileTypes)
{
$this->fileTypesToUpload = $fileTypes;
return $this;
}
/**
* Minimum and Maximum allowed image size for upload (in bytes),
*
* @param array $fileSize - ex: ['min'=>500, 'max'=>1000]
* @return $this
*/
public function limitSize(array $fileSize)
{
$this->allowedUploadSize = $fileSize;
return $this;
}
/**
* Default & maximum allowed height and width image to download.
*
* @param array $dimensions
* @return $this
*/
public function limitDimension(array $dimensions){
$this->imageDimension = $dimensions;
return $this;
}
/**
* Get the real file's Extension/mime type
*
* @param $imageName
* @return mixed
* @throws ImageUploaderException
*/
public function getMimeType($imageName)
{
if(!file_exists($imageName)){
throw new ImageUploaderException("File " . $imageName . " does not exist");
}
$listOfMimeTypes = [
1 => "gif", "jpeg", "png", "swf", "psd",
"bmp", "tiff", "tiff", "jpc", "jp2",
"jpx", "jb2", "swc", "iff", "wbmp",
"xmb", "ico"
];
if(isset($listOfMimeTypes[ exif_imagetype($imageName) ])){
return $listOfMimeTypes[ exif_imagetype($imageName) ];
}
}
/**
* Handy method for getting image dimensions (W & H) in pixels.
*
* @param $getImage - The image name
* @return array
*/
private function getImagePixels($getImage)
{
list($width, $height) = getImageSize($getImage);
return array($width, $height);
}
/**
* Rename file either from method or by generating a random one.
*
* @param $isNameProvided - A new name for the file.
* @return string
*/
private function createFileName($isNameProvided)
{
if ($isNameProvided) {
return $isNameProvided . "." . $this->getMimeType;
}
return uniqid(true)."_".str_shuffle(implode(range("E", "Q"))) . "." . $this->getMimeType;
}
/**
* Get the specified upload dir, if it does not exist, create a new one.
*
* @param $nameOfDir - directory name where you want your files to be uploaded
* @return $this
* @throws ImageUploaderException
*/
public function folder($nameOfDir)
{
if (!file_exists($nameOfDir) && !is_dir($nameOfDir)) {
$createFolder = mkdir("" . $nameOfDir, 0777);
if (!$createFolder) {
throw new ImageUploaderException("Folder " . $nameOfDir . " could not be created");
}
}
$this->fileUploadDirectory = $nameOfDir;
return $this;
}
/**
* For getting common error messages from FILES[] array during upload.
*
* @return array
*/
private function commonFileUploadErrors()
{
return $array(
"...", //UPLOAD_ERR_OK
"File is larger than the specified amount set by the server", //UPLOAD_ERR_INI_SIZE
"Files is larger than the specified amount specified by browser", //UPLOAD_ERR_FORM_SIZE
"File could not be fully uploaded. Please try again later", //UPLOAD_ERR_PARTIAL
"File is not found", //UPLOAD_ERR_NO_FILE
"Can't write to disk, as per server configuration", //UPLOAD_ERR_NO_TMP_DIR
"Failed to write file to disk. Introduced in PHP", //UPLOAD_ERR_CANT_WRITE
"A PHP extension has halted this file upload" //UPLOAD_ERR_EXTENSION
);
}
/*--------------------------------------------------------------------------
| WATERMARK | Image Watermark methods |
*--------------------------------------------------------------------------*/
/**
* Get the watermark image and its position.
*
* @param $watermark - the watermark name, ex: 'logo.png'
* @param $positionToWatermark - position to put the watermark, ex: 'center'
* @return $this
* @throws ImageUploaderException
*/
public function watermark($watermark, $positionToWatermark)
{
if (!file_exists($watermark)) {
throw new ImageUploaderException(" Please provide valid image to use as watermark ");
}
$this->getImageToWatermark = $watermark;
$this->getWatermarkPosition = $positionToWatermark;
return $this;
}
/**
* Calculate position and apply image watermark.
*
* The objective is to let position of watermarking be passed in simple English words like:
* 'center', 'right-top', 'bottom-left'.. as the second argument for the 'watermark()' method
* then take that word and do the real offset & marginal-calculation in this method.
*
* @param $imageToUpload
* @throws ImageUploaderException
*/
private function applyImageWatermark($imageToUpload)
{
if (!$this->getImageToWatermark) {
return;
}
// Calculate the watermark position
$watermark = $this->getImageToWatermark;
$position = $this->getWatermarkPosition;
list($imgWidth, $imgHeight) = $this->getImagePixels($imageToUpload);
list($watWidth, $watHeight) = $this->getImagePixels($watermark);
switch ($position) {
case "center":
$bottomPosition = (int)ceil($imgHeight / 2);
$rightPosition = (int)ceil($imgWidth / 2) - (int)ceil($watWidth / 2);
break;
case "bottom-left":
$bottomPosition = 5;
$rightPosition = (int)round($imgWidth - $watWidth);
break;
case "top-left":
$bottomPosition = (int)round($imgHeight - $watHeight);
$rightPosition = (int)round($imgWidth - $watWidth);
break;
case "top-right":
$bottomPosition = (int)round($imgHeight - $watHeight);
$rightPosition = 5;
break;
default:
// bottom-right
$bottomPosition = 2;
$rightPosition = 2;
break;
}
// Apply the watermark using the calculated position
$this->getWatermarkDimensions = $this->getImagePixels($watermark);
$imageType = $this->getMimeType($imageToUpload);
$watermark = imagecreatefrompng($watermark);
switch ($imageType) {
case "jpeg":
case "jpg":
$createImage = imagecreatefromjpeg($imageToUpload);
break;
case "png":
$createImage = imagecreatefrompng($imageToUpload);
break;
case "gif":
$createImage = imagecreatefromgif($imageToUpload);
break;
default:
$createImage = imagecreatefromjpeg($imageToUpload);
break;
}
$sx = imagesx($watermark);
$sy = imagesy($watermark);
imagecopy(
$createImage,
$watermark,
imagesx($createImage) - $sx - $rightPosition,
imagesy($createImage) - $sy - $bottomPosition,
0,
0,
imagesx($watermark),
imagesy($watermark)
);
switch ($imageType) {
case "jpeg":
case "jpg":
imagejpeg($createImage, $imageToUpload);
break;
case "png":
imagepng($createImage, $imageToUpload);
break;
case "gif":
imagegif($createImage, $imageToUpload);
break;
default:
throw new ImageUploaderException("A watermark can only be applied to: jpeg, jpg, gif, png images ");
break;
}
}
/*--------------------------------------------------------------------------
| SHRINK | Image shrink/resize methods |
---------------------------------------------------------------------------*/
/**
* Get the Width and Height of the image image to shrink (in pixels)
*
* @param array $setImageDimensions
* @return $this
*/
public function shrink(array $setImageDimensions)
{
$this->imageShrinkDimensions = $setImageDimensions;
return $this;
}
/**
* Shrink the image.
*
* @param $fileName - the file name
* @param $fileToUpload - the file to upload
* @throws ImageUploaderException
*/
private function applyImageShrink($fileName, $fileToUpload)
{
if (!$this->imageShrinkDimensions) {
return;
}
list($width, $height) = $this->getImagePixels($fileToUpload);
$newWidth = $this->imageShrinkDimensions['width'];
$newHeight = $this->imageShrinkDimensions['height'];
$imgString = file_get_contents($fileToUpload);
$image = imagecreatefromstring($imgString);
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled(
$tmp,
$image,
0,
0,
0,
0,
$newWidth,
$newHeight,
$width,
$height
);
$mimeType = $this->getMimeType($fileName);
switch ($mimeType) {
case "jpeg":
case "jpg":
imagejpeg($tmp, $fileToUpload, 100);
break;
case "png":
imagepng($tmp, $fileToUpload, 0);
break;
case "gif":
imagegif($tmp, $fileToUpload);
break;
default:
throw new ImageUploaderException(" Only jpg, jpeg, png and gif files can be resized ");
break;
}
}
/*--------------------------------------------------------------------------
| CROPPING | Image cropping methods |
---------------------------------------------------------------------------*/
/**
* Get size dimensions to use for new image cropping
*
* @param array $imageCropValues
* @return $this
*/
public function crop(array $imageCropValues)
{
$this->imageCropDimension = $imageCropValues;
return $this;
}
/**
* Apply crop image, from the given size
*
* @param $imageName
* @param $tmp_name
* @return resource
* @throws ImageUploaderException
*/
private function applyImageCropping($imageName, $tmp_name)
{
if (!$this->imageCropDimension) {
return;
}
$mimeType = $this->getMimeType($imageName);
switch ($mimeType) {
case "jpg":
case "jpeg":
$image = imagecreatefromjpeg($tmp_name);
break;
case "png":
$image = imagecreatefrompng($tmp_name);
break;
case "gif":
$image = imagecreatefromgif($tmp_name);
break;
default:
throw new ImageUploaderException(" Only gif, jpg, jpeg and png files can be cropped ");
break;
}
// Uploaded image pixels.
list($imgWidth, $imgHeight) = $this->getImagePixels($tmp_name);
// Size given for cropping image.
$heightToCrop = $this->imageCropDimension["height"];
$widthToCrop = $this->imageCropDimension["width"];
// The image offsets/coordination to crop the image.
$widthTrim = ceil(($imgWidth - $widthToCrop) / 2);
$heightTrim = ceil(($imgHeight - $heightToCrop) / 2);
// Can't crop a 100X100 image, to 200X200. Image can only be cropped to smaller size.
if ($widthTrim < 0 && $heightTrim < 0) {
return ;
}
$temp = imagecreatetruecolor($widthToCrop, $heightToCrop);
imagecopyresampled(
$temp,
$image,
0,
0,
$widthTrim,
$heightTrim,
$widthToCrop,
$heightToCrop,
$widthToCrop,
$heightToCrop
);
if (!$temp) {
throw new ImageUploaderException("Failed to crop image. Please pass the right parameters");
} else {
imagejpeg($temp, $tmp_name);
}
}
/*--------------------------------------------------------------------------
| Change file | crop/watermark/shrink methods. without uploading |
---------------------------------------------------------------------------*/
/**
* Without uploading, just crop/watermark/shrink all images in your folders
*
* @param $directive - the task.. ex: 'crop', 'watermark', 'shrink'...
* @param $imageName - the image you want to change. Provide full path pls.
* @throws ImageUploaderException
*/
public function change($directive, $imageName){
if(empty($directive) || !file_exists($imageName)){
throw new ImageUploaderException(__FUNCTION__." Requires image name and array directive ");
}
$tasks = array("watermark", "shrink", "crop");
switch ($directive) {
case "watermark":
if(!$this->getWatermarkPosition || !$this->getImageToWatermark){
throw new ImageUploaderException("Please provide 'watermark' and 'position' by using the 'watermark()' method");
}
// Apply watermark to image
$this->applyImageWatermark($imageName);
break;
case "shrink":
if(!$this->imageShrinkDimensions){
throw new ImageUploaderException("Please provide 'width * height' dimensions by using 'shrink()' method ");
}
// Resize or crop the image
$this->applyImageShrink($imageName, $imageName);
break;
case "crop":
if(!$this->imageCropDimension){
throw new ImageUploaderException("Please provide 'width * height' dimensions by using 'shrink()' method ");
}
// Crop the image
$this->applyImageCropping($imageName, $imageName);
break;
default:
throw new ImageUploaderException(__FUNCTION__." Expects either ". implode(", ", $tasks)." as second argument");
break;
}
}
/**
* Simple file check and delete wrapper.
*
* @param $fileToDelete
* @return bool
* @throws ImageUploaderException
*/
public function deleteFile($fileToDelete){
if (file_exists($fileToDelete) && !unlink($fileToDelete)) {
throw new ImageUploaderException("File may have been deleted or does not exist");
}
return true;
}
/**
* Final image uploader method, to check for errors and upload
*
* @param $fileToUpload
* @param null $isNameProvided
* @return string
* @throws ImageUploaderException
*/
public function upload($fileToUpload, $isNameProvided = null)
{
// First get the real file extension
$this->getMimeType = $this->getMimeType($fileToUpload["name"]);
// Check if this file type is allowed for upload
if (!in_array($this->getMimeType, $this->fileTypesToUpload)) {
throw new ImageUploaderException(" This is not allowed file type!
Please only upload ( " . implode(", ", $this->fileTypesToUpload) . " ) file types");
}
// Check if any errors are thrown by the FILES[] array
if ($fileToUpload['error']) {
$temp = $this->commonFileUploadErrors();
throw new ImageUploaderException("ERROR " . $temp[$fileToUpload['error']]);
}
// Check if size (in bytes) of the image is above or below of defined in 'sizeLimit()'
if ($fileToUpload["size"] <= $this->allowedUploadSize["min"] ||
$fileToUpload["size"] >= $this->allowedUploadSize["max"]
) {
throw new ImageUploaderException("File sizes must be between " .
implode(" to ", $this->allowedUploadSize) . " bytes");
}
// check if image is valid pixel-wise.
list($imgWidth, $imgHeight) = $this->getImagePixels($fileToUpload["name"]);
if($imgWidth < 1 || $imgHeight < 1){
throw new ImageUploaderException("This file is either too small or corrupted to be an image file");
}
if($imgWidth > $this->imageDimension["width"] || $imgHeight > $this->imageDimension["height"]){
throw new ImageUploaderException("The allowed file dimensions are ". implode(", ", $this->imageDimension). " pixels");
}
// Assign given name or generate a new one.
$newFileName = $this->createFileName($isNameProvided);
$this->applyImageWatermark($fileToUpload["tmp_name"]);
$this->applyImageShrink($fileToUpload["name"], $fileToUpload["tmp_name"]);
$this->applyImageCropping($fileToUpload["name"], $fileToUpload["tmp_name"]);
// Security check, to see if file was uploaded with HTTP_POST
$checkSafeUpload = is_uploaded_file($fileToUpload["tmp_name"]);
// Upload the file
$filePath = $this->fileUploadDirectory . "/" . $newFileName;
$moveUploadedFile = move_uploaded_file( $fileToUpload["tmp_name"], $filePath);
if ($checkSafeUpload && $moveUploadedFile) {
return $filePath;
}else{
throw new ImageUploaderException(" File could not be uploaded. Unknown error occurred. ");
}
}
}