2017 © Pedro Peláez
 

project eimage

Image upload, resize and crop PHP Class

image

falmar/eimage

Image upload, resize and crop PHP Class

  • Friday, June 3, 2016
  • by falmar
  • Repository
  • 2 Watchers
  • 2 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

eImage - Image upload, resize and crop!

Build Status, (*1)

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)

Requirements

  • PHP >= 5.4

Examples

Upload Image

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)

Crop Image

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)

Resize Image

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)

Parameters and their default values

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';

NewName

Specify the new name for your image., (*8)

UploadTo

Specify where the new image is going to be uploaded to., (*9)

ReturnType

  • 'full_path': string with the full path to the new image.
  • 'bool': true or false if the upload succeeded.
  • 'array':
    • from upload() method: Pretty close to the $_FILE array it will return name, path, size, tmp_name and full_path.
    • from crop() method: Will return name, prefix, path, height, width and full_path.
    • from resize() method: Will return name, prefix, path, height, width, pad_color and full_path.

SafeRename

  • true: will clean the image name and remove strange characters.
  • false: the new image will contain the same name as the uploaded image.

Duplicates

If a there is an existing file: - 'o': Overwrite the file. - 'u': Create an unique file. - 'e': Throw eImageException., (*10)

EnabledMIMEs

An array that contain the MIME Types the eImage Class will be allow to upload., (*11)

['.jpg' => 'image/jpg']

DisabledMIMEs

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)

Source

Full path to a file automatically set after image upload for easy access resize and crop methods., (*14)

CreateDir

If set to true create a directory if not exist (UploadTo | NewPath)., (*15)

ImageQuality

Integer [1-100]%., (*16)

NewExtension

Apply a new extension to the image (.jpg, .png, .gif)., (*17)

Prefix

Specify a new prefix for the image., (*18)

NewPath

Specify path for the new image, it apply only for crop() and resize() methods., (*19)

AspectRatio

Set true or false if you want to maintain or not your image aspect ratio., (*20)

Oversize

If true it will oversize the image when width > height or the otherwise., (*21)

ScaleUp

Set true if want allow the image to scale up from a small size to a bigger one., (*22)

PadColor

Hexadecimal color string for the image background if does not fit the canvas, default is 'transparent'., (*23)

FitPad

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)

Position

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)

The Versions

03/06 2016

dev-dev

dev-dev

Image upload, resize and crop PHP Class

  Sources   Download

MIT

The Requires

  • php >=5.4

 

03/06 2016

dev-master

9999999-dev

Image upload, resize and crop PHP Class

  Sources   Download

MIT

The Requires

  • php >=5.4

 

05/04 2016

v0.0.1-alpha

0.0.1.0-alpha

Image upload, resize and crop PHP Class

  Sources   Download

MIT

The Requires

  • php >=5.4