@@ -1,65 +1,65 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Image Croping Function. |
|
| 4 | - * |
|
| 5 | - * @author Daniel, Simon <[email protected]> |
|
| 6 | - * @link https://github.com/samayo/bulletproof |
|
| 7 | - * @copyright Copyright (c) 2015 Simon Daniel |
|
| 8 | - * @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
| 9 | - */ |
|
| 3 | + * Image Croping Function. |
|
| 4 | + * |
|
| 5 | + * @author Daniel, Simon <[email protected]> |
|
| 6 | + * @link https://github.com/samayo/bulletproof |
|
| 7 | + * @copyright Copyright (c) 2015 Simon Daniel |
|
| 8 | + * @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
| 9 | + */ |
|
| 10 | 10 | namespace Bulletproof; |
| 11 | 11 | |
| 12 | 12 | function crop($image, $mimeType, $imgWidth, $imgHeight, $newWidth, $newHeight){
|
| 13 | 13 | |
| 14 | - switch ($mimeType) {
|
|
| 15 | - case "jpg": |
|
| 16 | - case "jpeg": |
|
| 17 | - $imageCreate = imagecreatefromjpeg($image); |
|
| 18 | - break; |
|
| 14 | + switch ($mimeType) {
|
|
| 15 | + case "jpg": |
|
| 16 | + case "jpeg": |
|
| 17 | + $imageCreate = imagecreatefromjpeg($image); |
|
| 18 | + break; |
|
| 19 | 19 | |
| 20 | - case "png": |
|
| 21 | - $imageCreate = imagecreatefrompng($image); |
|
| 22 | - break; |
|
| 20 | + case "png": |
|
| 21 | + $imageCreate = imagecreatefrompng($image); |
|
| 22 | + break; |
|
| 23 | 23 | |
| 24 | - case "gif": |
|
| 25 | - $imageCreate = imagecreatefromgif($image); |
|
| 26 | - break; |
|
| 24 | + case "gif": |
|
| 25 | + $imageCreate = imagecreatefromgif($image); |
|
| 26 | + break; |
|
| 27 | 27 | |
| 28 | - default: |
|
| 29 | - throw new \Exception(" Only gif, jpg, jpeg and png files can be cropped ");
|
|
| 30 | - break; |
|
| 31 | - } |
|
| 28 | + default: |
|
| 29 | + throw new \Exception(" Only gif, jpg, jpeg and png files can be cropped ");
|
|
| 30 | + break; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - // The image offsets/coordination to crop the image. |
|
| 34 | - $widthTrim = ceil(($imgWidth - $newWidth) / 2); |
|
| 35 | - $heightTrim = ceil(($imgHeight - $newHeight) / 2); |
|
| 33 | + // The image offsets/coordination to crop the image. |
|
| 34 | + $widthTrim = ceil(($imgWidth - $newWidth) / 2); |
|
| 35 | + $heightTrim = ceil(($imgHeight - $newHeight) / 2); |
|
| 36 | 36 | |
| 37 | - // Can't crop to a bigger size, ex: |
|
| 38 | - // an image with 100X100 can not be cropped to 200X200. Image can only be cropped to smaller size. |
|
| 39 | - if ($widthTrim < 0 && $heightTrim < 0) {
|
|
| 40 | - return ; |
|
| 41 | - } |
|
| 37 | + // Can't crop to a bigger size, ex: |
|
| 38 | + // an image with 100X100 can not be cropped to 200X200. Image can only be cropped to smaller size. |
|
| 39 | + if ($widthTrim < 0 && $heightTrim < 0) {
|
|
| 40 | + return ; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - $temp = imagecreatetruecolor($newWidth, $newHeight); |
|
| 44 | - imagecopyresampled( |
|
| 45 | - $temp, |
|
| 46 | - $imageCreate, |
|
| 47 | - 0, |
|
| 48 | - 0, |
|
| 49 | - $widthTrim, |
|
| 50 | - $heightTrim, |
|
| 51 | - $newWidth, |
|
| 52 | - $newHeight, |
|
| 53 | - $newWidth, |
|
| 54 | - $newHeight |
|
| 55 | - ); |
|
| 43 | + $temp = imagecreatetruecolor($newWidth, $newHeight); |
|
| 44 | + imagecopyresampled( |
|
| 45 | + $temp, |
|
| 46 | + $imageCreate, |
|
| 47 | + 0, |
|
| 48 | + 0, |
|
| 49 | + $widthTrim, |
|
| 50 | + $heightTrim, |
|
| 51 | + $newWidth, |
|
| 52 | + $newHeight, |
|
| 53 | + $newWidth, |
|
| 54 | + $newHeight |
|
| 55 | + ); |
|
| 56 | 56 | |
| 57 | 57 | |
| 58 | - if (!$temp) {
|
|
| 59 | - throw new \Exception("Failed to crop image. Please pass the right parameters");
|
|
| 60 | - } else {
|
|
| 61 | - imagejpeg($temp, $image); |
|
| 62 | - } |
|
| 58 | + if (!$temp) {
|
|
| 59 | + throw new \Exception("Failed to crop image. Please pass the right parameters");
|
|
| 60 | + } else {
|
|
| 61 | + imagejpeg($temp, $image); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | 64 | } |
| 65 | 65 | |
@@ -11,86 +11,86 @@ |
||
| 11 | 11 | |
| 12 | 12 | function watermark($image, $mimeType, $imgWidth, $imgHeight, $watermark, $watermarkHeight, $watermarkWidth, $position = "center"){
|
| 13 | 13 | |
| 14 | - // Calculate the watermark position |
|
| 15 | - switch ($position) {
|
|
| 16 | - case "center": |
|
| 17 | - $marginBottom = round($imgHeight / 2); |
|
| 18 | - $marginRight = round($imgWidth / 2) - round($watermarkWidth / 2); |
|
| 19 | - break; |
|
| 20 | - |
|
| 21 | - case "top-left": |
|
| 22 | - $marginBottom = round($imgHeight - $watermarkHeight); |
|
| 23 | - $marginRight = round($imgWidth - $watermarkWidth); |
|
| 24 | - break; |
|
| 25 | - |
|
| 26 | - case "bottom-left": |
|
| 27 | - $marginBottom = 5; |
|
| 28 | - $marginRight = round($imgWidth - $watermarkWidth); |
|
| 29 | - break; |
|
| 30 | - |
|
| 31 | - case "top-right": |
|
| 32 | - $marginBottom = round($imgHeight - $watermarkHeight); |
|
| 33 | - $marginRight = 5; |
|
| 34 | - break; |
|
| 35 | - |
|
| 36 | - default: |
|
| 37 | - $marginBottom = 2; |
|
| 38 | - $marginRight = 2; |
|
| 39 | - break; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - |
|
| 43 | - $watermark = imagecreatefrompng($watermark); |
|
| 44 | - |
|
| 45 | - switch ($mimeType) {
|
|
| 46 | - case "jpeg": |
|
| 47 | - case "jpg": |
|
| 48 | - $createImage = imagecreatefromjpeg($image); |
|
| 49 | - break; |
|
| 50 | - |
|
| 51 | - case "png": |
|
| 52 | - $createImage = imagecreatefrompng($image); |
|
| 53 | - break; |
|
| 54 | - |
|
| 55 | - case "gif": |
|
| 56 | - $createImage = imagecreatefromgif($image); |
|
| 57 | - break; |
|
| 58 | - |
|
| 59 | - default: |
|
| 60 | - $createImage = imagecreatefromjpeg($image); |
|
| 61 | - break; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - $sx = imagesx($watermark); |
|
| 65 | - $sy = imagesy($watermark); |
|
| 66 | - imagecopy( |
|
| 67 | - $createImage, |
|
| 68 | - $watermark, |
|
| 69 | - imagesx($createImage) - $sx - $marginRight, |
|
| 70 | - imagesy($createImage) - $sy - $marginBottom, |
|
| 71 | - 0, |
|
| 72 | - 0, |
|
| 73 | - imagesx($watermark), |
|
| 74 | - imagesy($watermark) |
|
| 75 | - ); |
|
| 76 | - |
|
| 77 | - |
|
| 78 | - switch ($mimeType) {
|
|
| 79 | - case "jpeg": |
|
| 80 | - case "jpg": |
|
| 81 | - imagejpeg($createImage, $image); |
|
| 82 | - break; |
|
| 83 | - |
|
| 84 | - case "png": |
|
| 85 | - imagepng($createImage, $image); |
|
| 86 | - break; |
|
| 87 | - |
|
| 88 | - case "gif": |
|
| 89 | - imagegif($createImage, $image); |
|
| 90 | - break; |
|
| 91 | - |
|
| 92 | - default: |
|
| 93 | - throw new \Exception("A watermark can only be applied to: jpeg, jpg, gif, png images ");
|
|
| 94 | - break; |
|
| 95 | - } |
|
| 14 | + // Calculate the watermark position |
|
| 15 | + switch ($position) {
|
|
| 16 | + case "center": |
|
| 17 | + $marginBottom = round($imgHeight / 2); |
|
| 18 | + $marginRight = round($imgWidth / 2) - round($watermarkWidth / 2); |
|
| 19 | + break; |
|
| 20 | + |
|
| 21 | + case "top-left": |
|
| 22 | + $marginBottom = round($imgHeight - $watermarkHeight); |
|
| 23 | + $marginRight = round($imgWidth - $watermarkWidth); |
|
| 24 | + break; |
|
| 25 | + |
|
| 26 | + case "bottom-left": |
|
| 27 | + $marginBottom = 5; |
|
| 28 | + $marginRight = round($imgWidth - $watermarkWidth); |
|
| 29 | + break; |
|
| 30 | + |
|
| 31 | + case "top-right": |
|
| 32 | + $marginBottom = round($imgHeight - $watermarkHeight); |
|
| 33 | + $marginRight = 5; |
|
| 34 | + break; |
|
| 35 | + |
|
| 36 | + default: |
|
| 37 | + $marginBottom = 2; |
|
| 38 | + $marginRight = 2; |
|
| 39 | + break; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + |
|
| 43 | + $watermark = imagecreatefrompng($watermark); |
|
| 44 | + |
|
| 45 | + switch ($mimeType) {
|
|
| 46 | + case "jpeg": |
|
| 47 | + case "jpg": |
|
| 48 | + $createImage = imagecreatefromjpeg($image); |
|
| 49 | + break; |
|
| 50 | + |
|
| 51 | + case "png": |
|
| 52 | + $createImage = imagecreatefrompng($image); |
|
| 53 | + break; |
|
| 54 | + |
|
| 55 | + case "gif": |
|
| 56 | + $createImage = imagecreatefromgif($image); |
|
| 57 | + break; |
|
| 58 | + |
|
| 59 | + default: |
|
| 60 | + $createImage = imagecreatefromjpeg($image); |
|
| 61 | + break; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + $sx = imagesx($watermark); |
|
| 65 | + $sy = imagesy($watermark); |
|
| 66 | + imagecopy( |
|
| 67 | + $createImage, |
|
| 68 | + $watermark, |
|
| 69 | + imagesx($createImage) - $sx - $marginRight, |
|
| 70 | + imagesy($createImage) - $sy - $marginBottom, |
|
| 71 | + 0, |
|
| 72 | + 0, |
|
| 73 | + imagesx($watermark), |
|
| 74 | + imagesy($watermark) |
|
| 75 | + ); |
|
| 76 | + |
|
| 77 | + |
|
| 78 | + switch ($mimeType) {
|
|
| 79 | + case "jpeg": |
|
| 80 | + case "jpg": |
|
| 81 | + imagejpeg($createImage, $image); |
|
| 82 | + break; |
|
| 83 | + |
|
| 84 | + case "png": |
|
| 85 | + imagepng($createImage, $image); |
|
| 86 | + break; |
|
| 87 | + |
|
| 88 | + case "gif": |
|
| 89 | + imagegif($createImage, $image); |
|
| 90 | + break; |
|
| 91 | + |
|
| 92 | + default: |
|
| 93 | + throw new \Exception("A watermark can only be applied to: jpeg, jpg, gif, png images ");
|
|
| 94 | + break; |
|
| 95 | + } |
|
| 96 | 96 | } |
| 97 | 97 | \ No newline at end of file |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | * |
| 122 | 122 | * @param mixed $offset |
| 123 | 123 | * |
| 124 | - * @return bool|mixed |
|
| 124 | + * @return string|boolean |
|
| 125 | 125 | */ |
| 126 | 126 | public function offsetGet($offset) |
| 127 | 127 | {
|
@@ -140,8 +140,6 @@ discard block |
||
| 140 | 140 | /** |
| 141 | 141 | * Renames image |
| 142 | 142 | * |
| 143 | - * @param null $isNameGiven if null, image will be auto-generated |
|
| 144 | - * |
|
| 145 | 143 | * @return $this |
| 146 | 144 | */ |
| 147 | 145 | public function setName($isNameProvided = null) |
@@ -318,7 +316,7 @@ discard block |
||
| 318 | 316 | /** |
| 319 | 317 | * Returns error string or false if no errors occurred |
| 320 | 318 | * |
| 321 | - * @return string|bool |
|
| 319 | + * @return string|false |
|
| 322 | 320 | */ |
| 323 | 321 | public function getError(){
|
| 324 | 322 | return $this->error != "" ? $this->error : false; |
@@ -430,7 +428,7 @@ discard block |
||
| 430 | 428 | * Final upload method to be called, isolated for testing purposes |
| 431 | 429 | * |
| 432 | 430 | * @param $tmp_name int the temporary location of the image file |
| 433 | - * @param $destination int upload destination |
|
| 431 | + * @param string $destination int upload destination |
|
| 434 | 432 | * |
| 435 | 433 | * @return bool |
| 436 | 434 | */ |
@@ -16,426 +16,426 @@ |
||
| 16 | 16 | |
| 17 | 17 | class Image implements \ArrayAccess |
| 18 | 18 | {
|
| 19 | - /** |
|
| 20 | - * @var string The new image name, to be provided or will be generated. |
|
| 21 | - */ |
|
| 22 | - protected $name; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @var int The image width in pixels |
|
| 26 | - */ |
|
| 27 | - protected $width; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var int The image height in pixels |
|
| 31 | - */ |
|
| 32 | - protected $height; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var string The image mime type (extension) |
|
| 36 | - */ |
|
| 37 | - protected $mime; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @var string The full image path (dir + image + mime) |
|
| 41 | - */ |
|
| 42 | - protected $fullPath; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @var string The folder or image storage location |
|
| 46 | - */ |
|
| 47 | - protected $location; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @var array A json format of all information about an image |
|
| 51 | - */ |
|
| 52 | - protected $serialize = array(); |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @var array The min and max image size allowed for upload (in bytes) |
|
| 56 | - */ |
|
| 57 | - protected $size = array(100, 50000); |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @var array The max height and width image allowed |
|
| 61 | - */ |
|
| 62 | - protected $dimensions = array(500, 5000); |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @var array The mime types allowed for upload |
|
| 66 | - */ |
|
| 67 | - protected $mimeTypes = array("jpeg", "png", "gif");
|
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @var array list of known image types |
|
| 71 | - */ |
|
| 72 | - protected $imageMimes = array( |
|
| 73 | - 1 => "gif", "jpeg", "png", "swf", "psd", |
|
| 74 | - "bmp", "tiff", "tiff", "jpc", "jp2", "jpx", |
|
| 75 | - "jb2", "swc", "iff", "wbmp", "xbm", "ico" |
|
| 76 | - ); |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @var array storage for the $_FILES global array |
|
| 80 | - */ |
|
| 81 | - private $_files = array(); |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @var string storage for any errors |
|
| 85 | - */ |
|
| 86 | - private $error = ""; |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param array $_files represents the $_FILES array passed as dependancy |
|
| 90 | - */ |
|
| 91 | - public function __construct(array $_files = []) |
|
| 92 | - {
|
|
| 93 | - $this->_files = $_files; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Gets the real image mime type |
|
| 98 | - * |
|
| 99 | - * @param $tmp_name string The upload tmp directory |
|
| 100 | - * |
|
| 101 | - * @return bool|string |
|
| 102 | - */ |
|
| 103 | - protected function getImageMime($tmp_name) |
|
| 104 | - {
|
|
| 105 | - if (isset($this->imageMimes[exif_imagetype($tmp_name)])) {
|
|
| 106 | - return $this->imageMimes [exif_imagetype($tmp_name)]; |
|
| 107 | - } |
|
| 108 | - return false; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * array offset \ArrayAccess |
|
| 113 | - * unused |
|
| 114 | - */ |
|
| 115 | - public function offsetSet($offset, $value){}
|
|
| 116 | - public function offsetExists($offset){}
|
|
| 117 | - public function offsetUnset($offset){}
|
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Gets array value \ArrayAccess |
|
| 121 | - * |
|
| 122 | - * @param mixed $offset |
|
| 123 | - * |
|
| 124 | - * @return bool|mixed |
|
| 125 | - */ |
|
| 126 | - public function offsetGet($offset) |
|
| 127 | - {
|
|
| 128 | - if ($offset == "error") {
|
|
| 129 | - return $this->error; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - if (isset($this->_files[$offset]) && file_exists($this->_files[$offset]["tmp_name"])) {
|
|
| 133 | - $this->_files = $this->_files[$offset]; |
|
| 134 | - return true; |
|
| 135 | - } |
|
| 19 | + /** |
|
| 20 | + * @var string The new image name, to be provided or will be generated. |
|
| 21 | + */ |
|
| 22 | + protected $name; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @var int The image width in pixels |
|
| 26 | + */ |
|
| 27 | + protected $width; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var int The image height in pixels |
|
| 31 | + */ |
|
| 32 | + protected $height; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var string The image mime type (extension) |
|
| 36 | + */ |
|
| 37 | + protected $mime; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @var string The full image path (dir + image + mime) |
|
| 41 | + */ |
|
| 42 | + protected $fullPath; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @var string The folder or image storage location |
|
| 46 | + */ |
|
| 47 | + protected $location; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @var array A json format of all information about an image |
|
| 51 | + */ |
|
| 52 | + protected $serialize = array(); |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @var array The min and max image size allowed for upload (in bytes) |
|
| 56 | + */ |
|
| 57 | + protected $size = array(100, 50000); |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @var array The max height and width image allowed |
|
| 61 | + */ |
|
| 62 | + protected $dimensions = array(500, 5000); |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @var array The mime types allowed for upload |
|
| 66 | + */ |
|
| 67 | + protected $mimeTypes = array("jpeg", "png", "gif");
|
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @var array list of known image types |
|
| 71 | + */ |
|
| 72 | + protected $imageMimes = array( |
|
| 73 | + 1 => "gif", "jpeg", "png", "swf", "psd", |
|
| 74 | + "bmp", "tiff", "tiff", "jpc", "jp2", "jpx", |
|
| 75 | + "jb2", "swc", "iff", "wbmp", "xbm", "ico" |
|
| 76 | + ); |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @var array storage for the $_FILES global array |
|
| 80 | + */ |
|
| 81 | + private $_files = array(); |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @var string storage for any errors |
|
| 85 | + */ |
|
| 86 | + private $error = ""; |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param array $_files represents the $_FILES array passed as dependancy |
|
| 90 | + */ |
|
| 91 | + public function __construct(array $_files = []) |
|
| 92 | + {
|
|
| 93 | + $this->_files = $_files; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Gets the real image mime type |
|
| 98 | + * |
|
| 99 | + * @param $tmp_name string The upload tmp directory |
|
| 100 | + * |
|
| 101 | + * @return bool|string |
|
| 102 | + */ |
|
| 103 | + protected function getImageMime($tmp_name) |
|
| 104 | + {
|
|
| 105 | + if (isset($this->imageMimes[exif_imagetype($tmp_name)])) {
|
|
| 106 | + return $this->imageMimes [exif_imagetype($tmp_name)]; |
|
| 107 | + } |
|
| 108 | + return false; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * array offset \ArrayAccess |
|
| 113 | + * unused |
|
| 114 | + */ |
|
| 115 | + public function offsetSet($offset, $value){}
|
|
| 116 | + public function offsetExists($offset){}
|
|
| 117 | + public function offsetUnset($offset){}
|
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Gets array value \ArrayAccess |
|
| 121 | + * |
|
| 122 | + * @param mixed $offset |
|
| 123 | + * |
|
| 124 | + * @return bool|mixed |
|
| 125 | + */ |
|
| 126 | + public function offsetGet($offset) |
|
| 127 | + {
|
|
| 128 | + if ($offset == "error") {
|
|
| 129 | + return $this->error; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + if (isset($this->_files[$offset]) && file_exists($this->_files[$offset]["tmp_name"])) {
|
|
| 133 | + $this->_files = $this->_files[$offset]; |
|
| 134 | + return true; |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - return false; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Renames image |
|
| 142 | - * |
|
| 143 | - * @param null $isNameGiven if null, image will be auto-generated |
|
| 144 | - * |
|
| 145 | - * @return $this |
|
| 146 | - */ |
|
| 147 | - public function setName($isNameProvided = null) |
|
| 148 | - {
|
|
| 149 | - if ($isNameProvided) {
|
|
| 150 | - $this->name = filter_var($isNameProvided, FILTER_SANITIZE_STRING); |
|
| 151 | - } |
|
| 137 | + return false; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Renames image |
|
| 142 | + * |
|
| 143 | + * @param null $isNameGiven if null, image will be auto-generated |
|
| 144 | + * |
|
| 145 | + * @return $this |
|
| 146 | + */ |
|
| 147 | + public function setName($isNameProvided = null) |
|
| 148 | + {
|
|
| 149 | + if ($isNameProvided) {
|
|
| 150 | + $this->name = filter_var($isNameProvided, FILTER_SANITIZE_STRING); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - return $this; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Define a mime type for uploading |
|
| 158 | - * |
|
| 159 | - * @param array $fileTypes |
|
| 160 | - * |
|
| 161 | - * @return $this |
|
| 162 | - */ |
|
| 163 | - public function setMime(array $fileTypes) |
|
| 164 | - {
|
|
| 165 | - $this->mimeTypes = $fileTypes; |
|
| 166 | - return $this; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Define a min and max image size for uploading |
|
| 171 | - * |
|
| 172 | - * @param $min int minimum value in bytes |
|
| 173 | - * @param $max int maximum value in bytes |
|
| 174 | - * |
|
| 175 | - * @return $this |
|
| 176 | - */ |
|
| 177 | - public function setSize($min, $max) |
|
| 178 | - {
|
|
| 179 | - $this->size = array($min, $max); |
|
| 180 | - return $this; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Creates a location for upload storage |
|
| 185 | - * |
|
| 186 | - * @param $dir string the folder name to create |
|
| 187 | - * @param int $permission chmod permission |
|
| 188 | - * |
|
| 189 | - * @return $this |
|
| 190 | - */ |
|
| 191 | - public function setLocation($dir = "bulletproof", $permission = 0666) |
|
| 192 | - {
|
|
| 193 | - if (!file_exists($dir) && !is_dir($dir) && !$this->location) {
|
|
| 194 | - $createFolder = @mkdir("" . $dir, (int) $permission, true);
|
|
| 195 | - if (!$createFolder) {
|
|
| 196 | - $this->error = "Folder " . $dir . " could not be created"; |
|
| 197 | - return; |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $this->location = $dir; |
|
| 202 | - return $this; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Sets acceptable max image height and width |
|
| 207 | - * |
|
| 208 | - * @param $maxWidth int max width value |
|
| 209 | - * @param $maxHeight int max height value |
|
| 210 | - * |
|
| 211 | - * @return $this |
|
| 212 | - */ |
|
| 213 | - public function setDimension($maxWidth, $maxHeight) |
|
| 214 | - {
|
|
| 215 | - $this->dimensions = array($maxWidth, $maxHeight); |
|
| 216 | - return $this; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Returns the image name |
|
| 221 | - * |
|
| 222 | - * @return string |
|
| 223 | - */ |
|
| 224 | - public function getName() |
|
| 225 | - {
|
|
| 226 | - if (!$this->name) {
|
|
| 227 | - return uniqid(true) . "_" . str_shuffle(implode(range("e", "q")));
|
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - return $this->name; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Returns the full path of the image ex "location/image.mime" |
|
| 235 | - * |
|
| 236 | - * @return string |
|
| 237 | - */ |
|
| 238 | - public function getFullPath() |
|
| 239 | - {
|
|
| 240 | - $this->fullPath = $this->location . "/" . $this->name . "." . $this->mime; |
|
| 241 | - return $this->fullPath; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Returns the image size in bytes |
|
| 246 | - * |
|
| 247 | - * @return int |
|
| 248 | - */ |
|
| 249 | - public function getSize() |
|
| 250 | - {
|
|
| 251 | - return (int) $this->_files["size"]; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Returns the image height in pixels |
|
| 256 | - * |
|
| 257 | - * @return int |
|
| 258 | - */ |
|
| 259 | - public function getHeight() |
|
| 260 | - {
|
|
| 261 | - if ($this->height != null) {
|
|
| 262 | - return $this->height; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - list(, $height) = getImageSize($this->_files["tmp_name"]); |
|
| 266 | - return $height; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * Returns the image width |
|
| 271 | - * |
|
| 272 | - * @return int |
|
| 273 | - */ |
|
| 274 | - public function getWidth() |
|
| 275 | - {
|
|
| 276 | - if ($this->width != null) {
|
|
| 277 | - return $this->width; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - list($width) = getImageSize($this->_files["tmp_name"]); |
|
| 281 | - return $width; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Returns the storage / folder name |
|
| 286 | - * |
|
| 287 | - * @return string |
|
| 288 | - */ |
|
| 289 | - public function getLocation() |
|
| 290 | - {
|
|
| 291 | - if(!$this->location){
|
|
| 292 | - $this->setLocation(); |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - return $this->location; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Returns a JSON format of the image width, height, name, mime ... |
|
| 300 | - * |
|
| 301 | - * @return string |
|
| 302 | - */ |
|
| 303 | - public function getJson() |
|
| 304 | - {
|
|
| 305 | - return json_encode($this->serialize); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Returns the image mime type |
|
| 310 | - * |
|
| 311 | - * @return string |
|
| 312 | - */ |
|
| 313 | - public function getMime() |
|
| 314 | - {
|
|
| 315 | - return $this->mime; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Returns error string or false if no errors occurred |
|
| 320 | - * |
|
| 321 | - * @return string|bool |
|
| 322 | - */ |
|
| 323 | - public function getError(){
|
|
| 324 | - return $this->error != "" ? $this->error : false; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * Checks for the common upload errors |
|
| 329 | - * |
|
| 330 | - * @param $e int error constant |
|
| 331 | - */ |
|
| 332 | - protected function uploadErrors($e) |
|
| 333 | - {
|
|
| 334 | - $errors = array( |
|
| 335 | - UPLOAD_ERR_OK => "", |
|
| 336 | - UPLOAD_ERR_INI_SIZE => "Image is larger than the specified amount set by the server", |
|
| 337 | - UPLOAD_ERR_FORM_SIZE => "Image is larger than the specified amount specified by browser", |
|
| 338 | - UPLOAD_ERR_PARTIAL => "Image could not be fully uploaded. Please try again later", |
|
| 339 | - UPLOAD_ERR_NO_FILE => "Image is not found", |
|
| 340 | - UPLOAD_ERR_NO_TMP_DIR => "Can't write to disk, due to server configuration ( No tmp dir found )", |
|
| 341 | - UPLOAD_ERR_CANT_WRITE => "Failed to write file to disk. Please check you file permissions", |
|
| 342 | - UPLOAD_ERR_EXTENSION => "A PHP extension has halted this file upload process" |
|
| 343 | - ); |
|
| 344 | - return $errors[$e]; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * Main upload method. |
|
| 349 | - * This is where all the monkey business happens |
|
| 350 | - * |
|
| 351 | - * @return $this|bool |
|
| 352 | - */ |
|
| 353 | - public function upload() |
|
| 354 | - {
|
|
| 355 | - /* modify variable names for convenience */ |
|
| 356 | - $image = $this; |
|
| 357 | - $files = $this->_files; |
|
| 358 | - |
|
| 359 | - /* initialize image properties */ |
|
| 360 | - $image->name = $image->getName(); |
|
| 361 | - $image->width = $image->getWidth(); |
|
| 362 | - $image->height = $image->getHeight(); |
|
| 363 | - $image->location = $image->getLocation(); |
|
| 364 | - |
|
| 365 | - /* get image sizes */ |
|
| 366 | - list($minSize, $maxSize) = $image->size; |
|
| 367 | - |
|
| 368 | - /* check for common upload errors */ |
|
| 369 | - if($image->error = $image->uploadErrors($files["error"])){
|
|
| 370 | - return ; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /* check image for valid mime types and return mime */ |
|
| 374 | - $image->mime = $image->getImageMime($files["tmp_name"]); |
|
| 375 | - |
|
| 376 | - /* validate image mime type */ |
|
| 377 | - if (!in_array($image->mime, $image->mimeTypes)) {
|
|
| 378 | - $ext = implode(", ", $image->mimeTypes);
|
|
| 379 | - $image->error = "Invalid File! Only ($ext) image types are allowed"; |
|
| 380 | - return ; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - /* check image size based on the settings */ |
|
| 384 | - if ($files["size"] < $minSize || $files["size"] > $maxSize) {
|
|
| 385 | - $min = intval($minSize / 1000) ?: 1; $max = intval($maxSize / 1000); |
|
| 153 | + return $this; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Define a mime type for uploading |
|
| 158 | + * |
|
| 159 | + * @param array $fileTypes |
|
| 160 | + * |
|
| 161 | + * @return $this |
|
| 162 | + */ |
|
| 163 | + public function setMime(array $fileTypes) |
|
| 164 | + {
|
|
| 165 | + $this->mimeTypes = $fileTypes; |
|
| 166 | + return $this; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Define a min and max image size for uploading |
|
| 171 | + * |
|
| 172 | + * @param $min int minimum value in bytes |
|
| 173 | + * @param $max int maximum value in bytes |
|
| 174 | + * |
|
| 175 | + * @return $this |
|
| 176 | + */ |
|
| 177 | + public function setSize($min, $max) |
|
| 178 | + {
|
|
| 179 | + $this->size = array($min, $max); |
|
| 180 | + return $this; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Creates a location for upload storage |
|
| 185 | + * |
|
| 186 | + * @param $dir string the folder name to create |
|
| 187 | + * @param int $permission chmod permission |
|
| 188 | + * |
|
| 189 | + * @return $this |
|
| 190 | + */ |
|
| 191 | + public function setLocation($dir = "bulletproof", $permission = 0666) |
|
| 192 | + {
|
|
| 193 | + if (!file_exists($dir) && !is_dir($dir) && !$this->location) {
|
|
| 194 | + $createFolder = @mkdir("" . $dir, (int) $permission, true);
|
|
| 195 | + if (!$createFolder) {
|
|
| 196 | + $this->error = "Folder " . $dir . " could not be created"; |
|
| 197 | + return; |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $this->location = $dir; |
|
| 202 | + return $this; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Sets acceptable max image height and width |
|
| 207 | + * |
|
| 208 | + * @param $maxWidth int max width value |
|
| 209 | + * @param $maxHeight int max height value |
|
| 210 | + * |
|
| 211 | + * @return $this |
|
| 212 | + */ |
|
| 213 | + public function setDimension($maxWidth, $maxHeight) |
|
| 214 | + {
|
|
| 215 | + $this->dimensions = array($maxWidth, $maxHeight); |
|
| 216 | + return $this; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Returns the image name |
|
| 221 | + * |
|
| 222 | + * @return string |
|
| 223 | + */ |
|
| 224 | + public function getName() |
|
| 225 | + {
|
|
| 226 | + if (!$this->name) {
|
|
| 227 | + return uniqid(true) . "_" . str_shuffle(implode(range("e", "q")));
|
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + return $this->name; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Returns the full path of the image ex "location/image.mime" |
|
| 235 | + * |
|
| 236 | + * @return string |
|
| 237 | + */ |
|
| 238 | + public function getFullPath() |
|
| 239 | + {
|
|
| 240 | + $this->fullPath = $this->location . "/" . $this->name . "." . $this->mime; |
|
| 241 | + return $this->fullPath; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Returns the image size in bytes |
|
| 246 | + * |
|
| 247 | + * @return int |
|
| 248 | + */ |
|
| 249 | + public function getSize() |
|
| 250 | + {
|
|
| 251 | + return (int) $this->_files["size"]; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Returns the image height in pixels |
|
| 256 | + * |
|
| 257 | + * @return int |
|
| 258 | + */ |
|
| 259 | + public function getHeight() |
|
| 260 | + {
|
|
| 261 | + if ($this->height != null) {
|
|
| 262 | + return $this->height; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + list(, $height) = getImageSize($this->_files["tmp_name"]); |
|
| 266 | + return $height; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * Returns the image width |
|
| 271 | + * |
|
| 272 | + * @return int |
|
| 273 | + */ |
|
| 274 | + public function getWidth() |
|
| 275 | + {
|
|
| 276 | + if ($this->width != null) {
|
|
| 277 | + return $this->width; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + list($width) = getImageSize($this->_files["tmp_name"]); |
|
| 281 | + return $width; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Returns the storage / folder name |
|
| 286 | + * |
|
| 287 | + * @return string |
|
| 288 | + */ |
|
| 289 | + public function getLocation() |
|
| 290 | + {
|
|
| 291 | + if(!$this->location){
|
|
| 292 | + $this->setLocation(); |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + return $this->location; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Returns a JSON format of the image width, height, name, mime ... |
|
| 300 | + * |
|
| 301 | + * @return string |
|
| 302 | + */ |
|
| 303 | + public function getJson() |
|
| 304 | + {
|
|
| 305 | + return json_encode($this->serialize); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Returns the image mime type |
|
| 310 | + * |
|
| 311 | + * @return string |
|
| 312 | + */ |
|
| 313 | + public function getMime() |
|
| 314 | + {
|
|
| 315 | + return $this->mime; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Returns error string or false if no errors occurred |
|
| 320 | + * |
|
| 321 | + * @return string|bool |
|
| 322 | + */ |
|
| 323 | + public function getError(){
|
|
| 324 | + return $this->error != "" ? $this->error : false; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * Checks for the common upload errors |
|
| 329 | + * |
|
| 330 | + * @param $e int error constant |
|
| 331 | + */ |
|
| 332 | + protected function uploadErrors($e) |
|
| 333 | + {
|
|
| 334 | + $errors = array( |
|
| 335 | + UPLOAD_ERR_OK => "", |
|
| 336 | + UPLOAD_ERR_INI_SIZE => "Image is larger than the specified amount set by the server", |
|
| 337 | + UPLOAD_ERR_FORM_SIZE => "Image is larger than the specified amount specified by browser", |
|
| 338 | + UPLOAD_ERR_PARTIAL => "Image could not be fully uploaded. Please try again later", |
|
| 339 | + UPLOAD_ERR_NO_FILE => "Image is not found", |
|
| 340 | + UPLOAD_ERR_NO_TMP_DIR => "Can't write to disk, due to server configuration ( No tmp dir found )", |
|
| 341 | + UPLOAD_ERR_CANT_WRITE => "Failed to write file to disk. Please check you file permissions", |
|
| 342 | + UPLOAD_ERR_EXTENSION => "A PHP extension has halted this file upload process" |
|
| 343 | + ); |
|
| 344 | + return $errors[$e]; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * Main upload method. |
|
| 349 | + * This is where all the monkey business happens |
|
| 350 | + * |
|
| 351 | + * @return $this|bool |
|
| 352 | + */ |
|
| 353 | + public function upload() |
|
| 354 | + {
|
|
| 355 | + /* modify variable names for convenience */ |
|
| 356 | + $image = $this; |
|
| 357 | + $files = $this->_files; |
|
| 358 | + |
|
| 359 | + /* initialize image properties */ |
|
| 360 | + $image->name = $image->getName(); |
|
| 361 | + $image->width = $image->getWidth(); |
|
| 362 | + $image->height = $image->getHeight(); |
|
| 363 | + $image->location = $image->getLocation(); |
|
| 364 | + |
|
| 365 | + /* get image sizes */ |
|
| 366 | + list($minSize, $maxSize) = $image->size; |
|
| 367 | + |
|
| 368 | + /* check for common upload errors */ |
|
| 369 | + if($image->error = $image->uploadErrors($files["error"])){
|
|
| 370 | + return ; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /* check image for valid mime types and return mime */ |
|
| 374 | + $image->mime = $image->getImageMime($files["tmp_name"]); |
|
| 375 | + |
|
| 376 | + /* validate image mime type */ |
|
| 377 | + if (!in_array($image->mime, $image->mimeTypes)) {
|
|
| 378 | + $ext = implode(", ", $image->mimeTypes);
|
|
| 379 | + $image->error = "Invalid File! Only ($ext) image types are allowed"; |
|
| 380 | + return ; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + /* check image size based on the settings */ |
|
| 384 | + if ($files["size"] < $minSize || $files["size"] > $maxSize) {
|
|
| 385 | + $min = intval($minSize / 1000) ?: 1; $max = intval($maxSize / 1000); |
|
| 386 | 386 | |
| 387 | - $image->error = "Image size should be atleast more than min: $min and less than max: $max kb"; |
|
| 388 | - return ; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /* check image dimension */ |
|
| 392 | - list($allowedHeight, $allowedWidth) = $image->dimensions; |
|
| 393 | - |
|
| 394 | - if ($image->height > $allowedHeight || $image->width > $allowedWidth) {
|
|
| 395 | - $image->error = "Image height/width should be less than ' $allowedHeight \ $allowedWidth ' pixels"; |
|
| 396 | - return ; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - if($image->height < 4 || $image->width < 4){
|
|
| 400 | - $image->error = "Invalid! Image height/width is too small or maybe corrupted"; |
|
| 401 | - return ; |
|
| 402 | - } |
|
| 387 | + $image->error = "Image size should be atleast more than min: $min and less than max: $max kb"; |
|
| 388 | + return ; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /* check image dimension */ |
|
| 392 | + list($allowedHeight, $allowedWidth) = $image->dimensions; |
|
| 393 | + |
|
| 394 | + if ($image->height > $allowedHeight || $image->width > $allowedWidth) {
|
|
| 395 | + $image->error = "Image height/width should be less than ' $allowedHeight \ $allowedWidth ' pixels"; |
|
| 396 | + return ; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + if($image->height < 4 || $image->width < 4){
|
|
| 400 | + $image->error = "Invalid! Image height/width is too small or maybe corrupted"; |
|
| 401 | + return ; |
|
| 402 | + } |
|
| 403 | 403 | |
| 404 | - /* set and get folder name */ |
|
| 405 | - $image->fullPath = $image->location. "/" . $image->name . "." . $image->mime; |
|
| 406 | - |
|
| 407 | - /* gather image info for json storage */ |
|
| 408 | - $image->serialize = array( |
|
| 409 | - "name" => $image->name, |
|
| 410 | - "mime" => $image->mime, |
|
| 411 | - "height" => $image->height, |
|
| 412 | - "width" => $image->width, |
|
| 413 | - "size" => $files["size"], |
|
| 414 | - "location" => $image->location, |
|
| 415 | - "fullpath" => $image->fullPath |
|
| 416 | - ); |
|
| 417 | - |
|
| 418 | - if ($image->error === "") {
|
|
| 419 | - $moveUpload = $image->moveUploadedFile($files["tmp_name"], $image->fullPath); |
|
| 420 | - if (false !== $moveUpload) {
|
|
| 421 | - return $image; |
|
| 422 | - } |
|
| 423 | - } |
|
| 404 | + /* set and get folder name */ |
|
| 405 | + $image->fullPath = $image->location. "/" . $image->name . "." . $image->mime; |
|
| 406 | + |
|
| 407 | + /* gather image info for json storage */ |
|
| 408 | + $image->serialize = array( |
|
| 409 | + "name" => $image->name, |
|
| 410 | + "mime" => $image->mime, |
|
| 411 | + "height" => $image->height, |
|
| 412 | + "width" => $image->width, |
|
| 413 | + "size" => $files["size"], |
|
| 414 | + "location" => $image->location, |
|
| 415 | + "fullpath" => $image->fullPath |
|
| 416 | + ); |
|
| 417 | + |
|
| 418 | + if ($image->error === "") {
|
|
| 419 | + $moveUpload = $image->moveUploadedFile($files["tmp_name"], $image->fullPath); |
|
| 420 | + if (false !== $moveUpload) {
|
|
| 421 | + return $image; |
|
| 422 | + } |
|
| 423 | + } |
|
| 424 | 424 | |
| 425 | - $image->error = "Upload failed, Unknown error occured"; |
|
| 426 | - return false; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * Final upload method to be called, isolated for testing purposes |
|
| 431 | - * |
|
| 432 | - * @param $tmp_name int the temporary location of the image file |
|
| 433 | - * @param $destination int upload destination |
|
| 434 | - * |
|
| 435 | - * @return bool |
|
| 436 | - */ |
|
| 437 | - public function moveUploadedFile($tmp_name, $destination) |
|
| 438 | - {
|
|
| 439 | - return move_uploaded_file($tmp_name, $destination); |
|
| 440 | - } |
|
| 425 | + $image->error = "Upload failed, Unknown error occured"; |
|
| 426 | + return false; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * Final upload method to be called, isolated for testing purposes |
|
| 431 | + * |
|
| 432 | + * @param $tmp_name int the temporary location of the image file |
|
| 433 | + * @param $destination int upload destination |
|
| 434 | + * |
|
| 435 | + * @return bool |
|
| 436 | + */ |
|
| 437 | + public function moveUploadedFile($tmp_name, $destination) |
|
| 438 | + {
|
|
| 439 | + return move_uploaded_file($tmp_name, $destination); |
|
| 440 | + } |
|
| 441 | 441 | } |
@@ -11,79 +11,79 @@ |
||
| 11 | 11 | |
| 12 | 12 | function resize($image, $mimeType, $imgWidth, $imgHeight, $newWidth, $newHeight, $ratio = FALSE, $upsize = TRUE){
|
| 13 | 13 | |
| 14 | - // First, calculate the height. |
|
| 15 | - $height = intval($newWidth / $imgWidth * $imgHeight); |
|
| 14 | + // First, calculate the height. |
|
| 15 | + $height = intval($newWidth / $imgWidth * $imgHeight); |
|
| 16 | 16 | |
| 17 | - // If the height is too large, set it to the maximum height and calculate the width. |
|
| 18 | - if ($height > $newHeight) {
|
|
| 17 | + // If the height is too large, set it to the maximum height and calculate the width. |
|
| 18 | + if ($height > $newHeight) {
|
|
| 19 | 19 | |
| 20 | - $height = $newHeight; |
|
| 21 | - $newWidth = intval($height / $imgHeight * $imgWidth); |
|
| 22 | - } |
|
| 20 | + $height = $newHeight; |
|
| 21 | + $newWidth = intval($height / $imgHeight * $imgWidth); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - // If we don't allow upsizing check if the new width or height are too big. |
|
| 25 | - if (!$upsize) {
|
|
| 26 | - // If the given width is larger then the image height, then resize it. |
|
| 27 | - if ($newWidth > $imgWidth) {
|
|
| 28 | - $newWidth = $imgWidth; |
|
| 29 | - $height = intval($newWidth / $imgWidth * $imgHeight); |
|
| 30 | - } |
|
| 24 | + // If we don't allow upsizing check if the new width or height are too big. |
|
| 25 | + if (!$upsize) {
|
|
| 26 | + // If the given width is larger then the image height, then resize it. |
|
| 27 | + if ($newWidth > $imgWidth) {
|
|
| 28 | + $newWidth = $imgWidth; |
|
| 29 | + $height = intval($newWidth / $imgWidth * $imgHeight); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - // If the given height is larger then the image height, then resize it. |
|
| 33 | - if ($height > $imgHeight) {
|
|
| 34 | - $height = $imgHeight; |
|
| 35 | - $newWidth = intval($height / $imgHeight * $imgWidth); |
|
| 36 | - } |
|
| 37 | - } |
|
| 32 | + // If the given height is larger then the image height, then resize it. |
|
| 33 | + if ($height > $imgHeight) {
|
|
| 34 | + $height = $imgHeight; |
|
| 35 | + $newWidth = intval($height / $imgHeight * $imgWidth); |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - if ($ratio == true) |
|
| 40 | - {
|
|
| 41 | - $source_aspect_ratio = $imgWidth / $imgHeight; |
|
| 42 | - $thumbnail_aspect_ratio = $newWidth / $newHeight; |
|
| 43 | - if ($imgWidth <= $newWidth && $imgHeight <= $newHeight) {
|
|
| 44 | - $newWidth = $imgWidth; |
|
| 45 | - $newHeight = $imgHeight; |
|
| 46 | - } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
|
|
| 47 | - $newWidth = (int) ($newHeight * $source_aspect_ratio); |
|
| 48 | - $newHeight = $newHeight; |
|
| 49 | - } else {
|
|
| 50 | - $newWidth = $newWidth; |
|
| 51 | - $newHeight = (int) ($newWidth / $source_aspect_ratio); |
|
| 52 | - } |
|
| 53 | - } |
|
| 39 | + if ($ratio == true) |
|
| 40 | + {
|
|
| 41 | + $source_aspect_ratio = $imgWidth / $imgHeight; |
|
| 42 | + $thumbnail_aspect_ratio = $newWidth / $newHeight; |
|
| 43 | + if ($imgWidth <= $newWidth && $imgHeight <= $newHeight) {
|
|
| 44 | + $newWidth = $imgWidth; |
|
| 45 | + $newHeight = $imgHeight; |
|
| 46 | + } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
|
|
| 47 | + $newWidth = (int) ($newHeight * $source_aspect_ratio); |
|
| 48 | + $newHeight = $newHeight; |
|
| 49 | + } else {
|
|
| 50 | + $newWidth = $newWidth; |
|
| 51 | + $newHeight = (int) ($newWidth / $source_aspect_ratio); |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - $imgString = file_get_contents($image); |
|
| 55 | + $imgString = file_get_contents($image); |
|
| 56 | 56 | |
| 57 | - $imageFromString = imagecreatefromstring($imgString); |
|
| 58 | - $tmp = imagecreatetruecolor($newWidth, $newHeight); |
|
| 59 | - imagecopyresampled( |
|
| 60 | - $tmp, |
|
| 61 | - $imageFromString, |
|
| 62 | - 0, |
|
| 63 | - 0, |
|
| 64 | - 0, |
|
| 65 | - 0, |
|
| 66 | - $newWidth, |
|
| 67 | - $newHeight, |
|
| 68 | - $imgWidth, |
|
| 69 | - $imgHeight |
|
| 70 | - ); |
|
| 57 | + $imageFromString = imagecreatefromstring($imgString); |
|
| 58 | + $tmp = imagecreatetruecolor($newWidth, $newHeight); |
|
| 59 | + imagecopyresampled( |
|
| 60 | + $tmp, |
|
| 61 | + $imageFromString, |
|
| 62 | + 0, |
|
| 63 | + 0, |
|
| 64 | + 0, |
|
| 65 | + 0, |
|
| 66 | + $newWidth, |
|
| 67 | + $newHeight, |
|
| 68 | + $imgWidth, |
|
| 69 | + $imgHeight |
|
| 70 | + ); |
|
| 71 | 71 | |
| 72 | - switch ($mimeType) {
|
|
| 73 | - case "jpeg": |
|
| 74 | - case "jpg": |
|
| 75 | - imagejpeg($tmp, $image, 90); |
|
| 76 | - break; |
|
| 77 | - case "png": |
|
| 78 | - imagepng($tmp, $image, 0); |
|
| 79 | - break; |
|
| 80 | - case "gif": |
|
| 81 | - imagegif($tmp, $image); |
|
| 82 | - break; |
|
| 83 | - default: |
|
| 84 | - throw new \Exception(" Only jpg, jpeg, png and gif files can be resized ");
|
|
| 85 | - break; |
|
| 86 | - } |
|
| 72 | + switch ($mimeType) {
|
|
| 73 | + case "jpeg": |
|
| 74 | + case "jpg": |
|
| 75 | + imagejpeg($tmp, $image, 90); |
|
| 76 | + break; |
|
| 77 | + case "png": |
|
| 78 | + imagepng($tmp, $image, 0); |
|
| 79 | + break; |
|
| 80 | + case "gif": |
|
| 81 | + imagegif($tmp, $image); |
|
| 82 | + break; |
|
| 83 | + default: |
|
| 84 | + throw new \Exception(" Only jpg, jpeg, png and gif files can be resized ");
|
|
| 85 | + break; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | 88 | } |
| 89 | 89 | |