Image upload, resize and crop PHP Class
eImage it's a simple PHP Class to make Uploading and Editing Images even more easy!, (*2)
A rewrite of a old Class (_image) originally written by Mark Jackson (mjdigital) all credits of main idea goes to him :D, (*3)
Major changes from the original class:
- Used all available PSR (Autoload, CodeStyle, etc...)
- Added Exception to handle errors
- Reduced portions of code by putting them into general class methods
- Removed obsolete|unused methods|conditions|variables, (*4)
use eImage\eImage; use eImage\eImageException; /** Upload your image **/ $File = (isset($_FILES['img'])) ? $_FILES['img'] : null; require_once('eImage/autoload.php'); try { /** * Simple Upload */ $Image = new eImage(); $Image->upload($File); /** -------------------------------------------------- */ /** * The next code will do the following: * Rename the image to my_new_image. * Place the uploaded image into base_dir/Images/ * Create a new unique image if find an existing one. * return an array with the new image properties. */ $Image = new eImage([ 'NewName' => 'my_new_name', 'UploadTo' => 'Images/', 'Duplicates' => 'u', 'ReturnType' => 'array' ]); $Image->upload($File); } catch (eImageException $e){ /** do something **/ }
NOTE: If there is not an extension specified in 'NewName' parameter it will take the extension from the original image. NOTE2: You can specify a new extension with NewExtension property, (*5)
use eImage\eImage; use eImage\eImageException; /** Upload your image **/ $File = (isset($_FILES['img'])) ? $_FILES['img'] : null; require_once('eImage/autoload.php'); try { /** * Crop from upload */ $Image = new eImage(); $Image->upload($File); $Image->crop(250, 250, -50, -75); /** -------------------------------------------------- */ /** * Crop from source file */ $Image->set([ 'Source' => 'path_to_your_file.jpg', 'Prefix' => 'AfterCrop-' ]); $Image->crop(250, 250, -50, -75); } catch (eImageException $e){ /** do something **/ }
NOTE: if you do not specify a NewName or Prefix parameter the original image will be override by the new crop method. You can also set the Duplicates to 'u' unique to create a new image instead of override, (*6)
use eImage\eImage; use eImage\eImageException; /** Upload your image **/ $File = (isset($_FILES['img'])) ? $_FILES['img'] : null; require_once('eImage/autoload.php'); try { /** * Resize from upload */ $Image = new eImage(); $Image->upload($File); $Image->resize(600, 450); /** -------------------------------------------------- */ /** * Resize from source file */ $Image->set([ 'Source' => 'my_source_image.jpg', 'Prefix' => 'AfterResize-', 'AspectRatio' => false, 'ScaleUp' => true ]); $Image->resize(600, 205); } catch (eImageException $e){ /** do something **/ }
NOTE: You may want to specify resize properties such as AspectRatio, Oversize, ScaleUp according to your needs., (*7)
public $NewName; public $UploadTo; public $ReturnType = 'full_path'; public $SafeRename = true; public $Duplicates = 'o'; private $EnableMIMEs = [ '.jpe' => 'image/jpeg', '.jpg' => 'image/jpg', '.jpeg' => 'image/jpeg', '.gif' => 'image/gif', '.png' => 'image/png', '.bmp' => 'image/bmp', '.ico' => 'image/x-icon', ]; private $DisabledMIMEs = []; public $CreateDir = false; public $Source; public $ImageQuality = 90; public $NewExtension; public $Prefix; public $NewPath; public $AspectRatio = true; public $Oversize = false; public $ScaleUp = false; public $Position = 'cc'; public $FitPad = true; public $PadColor = 'transparent';
Specify the new name for your image., (*8)
Specify where the new image is going to be uploaded to., (*9)
$_FILE
array it will return name, path, size, tmp_name and full_path.If a there is an existing file: - 'o': Overwrite the file. - 'u': Create an unique file. - 'e': Throw eImageException., (*10)
An array that contain the MIME Types the eImage Class will be allow to upload., (*11)
['.jpg' => 'image/jpg']
If this property is set with values it will forbid to upload the MIME Types or Extensions specified., (*12)
NOTE: Any other MIME Type or Extension THAT IS NOT SET HERE will be allowed to upload., (*13)
Full path to a file automatically set after image upload for easy access resize and crop methods., (*14)
If set to true create a directory if not exist (UploadTo | NewPath)., (*15)
Integer [1-100]%., (*16)
Apply a new extension to the image (.jpg, .png, .gif)., (*17)
Specify a new prefix for the image., (*18)
Specify path for the new image, it apply only for crop() and resize() methods., (*19)
Set true or false if you want to maintain or not your image aspect ratio., (*20)
If true it will oversize the image when width > height or the otherwise., (*21)
Set true if want allow the image to scale up from a small size to a bigger one., (*22)
Hexadecimal color string for the image background if does not fit the canvas, default is 'transparent'., (*23)
Set true if want to make use of the Position to fit the image in the canvas when the new size does not fit and AspectRatio is true., (*24)
Set the position of source in the canvas: - 'tr': top right - 'tl': top left - 'tc': top center - 'br': bottom right - 'bl': bottom left - 'bc': bottom center - 'cr': center right - 'cl': center left - 'cc': center horizontal and vertically, (*25)