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