yii-ext/yii-easyimage

Yii Framework extension for easy creating and caching thumbnails on real time.

Installs: 36

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 24

Type:yii-extension

0.2.2 2014-06-17 07:39 UTC

This package is not auto-updated.

Last update: 2024-04-13 13:20:23 UTC


README

You don't need to create many types of thumbnails for images in your project. You can create a thumbnail directly in the View. Thumbnail will automatically cached. It's easy!

Features:

  • Easy to use
  • Support GD and Imagick
  • Automaticly thumbnails caching
  • Cache sorting to subdirectories
  • Support Retina displays
  • Based on Kohana Image Library.

##Installing and configuring Extract the EasyImage folder under protected/extensions

Add the following to your config file components section:

'components'=>array(
//...
  'easyImage' => array(
    'class' => 'application.extensions.easyimage.EasyImage',
    'password' => 'password' // used for image action
    //'driver' => 'GD',
    //'quality' => 100,
    //'cachePath' => '/assets/easyimage/',
    //'cacheTime' => 2592000,
    //'retinaSupport' => false,
  ),

and the following to import section:

'import' => array(
  //...
  'ext.easyimage.EasyImage'
),

##Usage ###InstanceOf

$image = new EasyImage('/path/to/image.jpg');
$image->resize(100, 100);
$image->save('/full/path/to/thumb.jpg');

####Parameters

  • string $file required - Image file path
  • string $driver - Driver: GD, Imagick

ThumbOf

You can create a thumbnail directly in the View:

// Create and autocache
Yii::app()->easyImage->thumbOf('/path/to/image.jpg', array('rotate' => 90));

// or 
Yii::app()->easyImage->thumbOf('image.jpg', array('rotate' => 90),  array('class' => 'image'));

// or 
Yii::app()->easyImage->thumbOf('image.png', 
  array(
    'resize' => array('width' => 100, 'height' => 100),
    'rotate' => array('degrees' => 90),
    'sharpen' => 50,
    'background' => '#ffffff',
    'type' => 'jpg',
    'quality' => 60,
  ));

Note. This method return CHtml::image()

####Parameters

  • string $file required - Image file path
  • array $params - Image manipulation methods. See Methods
  • array $htmlOptions - options for CHtml::image()

ThumbSrcOf

Yii::app()->easyImage->thumbSrcOf('image.jpg', array('crop' => array('width' => 100, 'height' => 100)));

Note. This method return path to image cached. ####Parameters

  • string $file required - Image file path
  • array $params - Image manipulation methods. See Methods

##Methods ###Resize

$image->resize(100, 100, EasyImage::RESIZE_AUTO);
Yii::app()->easyImage->thumbOf('image.jpg', array('resize' => array('width' => 100, 'height' => 100)));

####Parameters

  • integer $width - New width
  • integer $height - New height
  • integer $master - Master dimension: EasyImage::RESIZE_NONE, EasyImage::RESIZE_WIDTH, EasyImage::RESIZE_HEIGHT, EasyImage::RESIZE_AUTO, EasyImage::RESIZE_INVERSE, EasyImage::RESIZE_PRECISE

###Crop

$image->crop(100, 100);
Yii::app()->easyImage->thumbOf('image.jpg', array('crop' => array('width' => 100, 'height' => 100)));

####Parameters

  • integer $width required - New width
  • integer $height required - New height
  • mixed $offset_x = NULL - Offset from the left
  • mixed $offset_y = NULL - Offset from the top

###Rotate

$image->rotate(45);
Yii::app()->easyImage->thumbOf('image.jpg', array('rotate' => array('degrees' => 45)));
// or
Yii::app()->easyImage->thumbOf('image.jpg', array('rotate' => 45));

####Parameters

  • integer $degrees required - Degrees to rotate: -360-360

###Flip

$image->flip(EasyImage::FLIP_HORIZONTAL);
Yii::app()->easyImage->thumbOf('image.jpg', array('flip' => array('direction' => EasyImage::FLIP_HORIZONTAL)));
// or
Yii::app()->easyImage->thumbOf('image.jpg', array('flip' => EasyImage::FLIP_VERTICAL));

####Parameters

  • integer $direction required - Direction: EasyImage::RESIZE_NONE, EasyImage::RESIZE_WIDTH.

###Sharpen

$image->sharpen(20);
Yii::app()->easyImage->thumbOf('image.jpg', array('sharpen' => array('amount' => 20)));
// or
Yii::app()->easyImage->thumbOf('image.jpg', array('sharpen' => 20));

####Parameters

  • integer $amount required - Amount to sharpen (percent): 1-100

###Reflection

// Create a 50 pixel reflection that fades from 0-100% opacity
$image->reflection(50);
 
// Create a 50 pixel reflection that fades from 100-0% opacity
$image->reflection(50, 100, TRUE);
 
// Create a 50 pixel reflection that fades from 0-60% opacity
$image->reflection(50, 60, TRUE);
Yii::app()->easyImage->thumbOf('image.jpg', array('reflection' => array('height' => 50)));
// or
Yii::app()->easyImage->thumbOf('image.jpg', array('reflection'));

Note. By default, the reflection will be go from transparent at the top to opaque at the bottom. ####Parameters

  • integer $height = NULL - Reflection height
  • integer $opacity = 100 - Reflection opacity: 0-100
  • boolean $fade_in = FALSE - TRUE to fade in, FALSE to fade out

###Watermark

$mark = new EasyImage('watermark.png');
$image->watermark($mark, TRUE, TRUE);
// or 
$image->watermark('watermark.png', 20, 20);
Yii::app()->easyImage->thumbOf('image.jpg', array('watermark' => array('watermark' => 'mark.png', 'opacity' => 50)));

Note. If no offset is specified, the center of the axis will be used. If an offset of TRUE is specified, the bottom of the axis will be used. ####Parameters

  • mixed $watermark required - Watermark EasyImage instance or path to Image
  • integer $offset_x = NULL - Offset from the left
  • integer $offset_y = NULL - Offset from the top
  • integer $opacity = 100 - Opacity of watermark: 1-100

###Background

$image->background('#000', 50);
Yii::app()->easyImage->thumbOf('image.jpg', array('background' => array('color' => '#ffffff', 'opacity' => 50)));
// or
Yii::app()->easyImage->thumbOf('image.jpg', array('background' => '#ffffff'));

Note This is only useful for images with alpha transparency. ####Parameters

  • string $color required - Hexadecimal color value
  • integer $opacity = 100 - Background opacity: 0-100

###Quality

Yii::app()->easyImage->thumbOf('image.jpg', array('quality' => 60)));
//not support: $image->quality(60);
// see $image->render(NULL, 60);

Note This is only useful for JPG images. ####Parameters

  • integer required - Quality of image: 1-100

###Type

Yii::app()->easyImage->thumbOf('image.png', array('type' => 'jpg')));
//not support: $image->type('jpg');
// see $image->render('jpg');

####Parameters

  • string required - Image type to return: png, jpg, gif, etc

###Save

// Save the image as a PNG
$image->save('image.png');
 
// Overwrite the original image
$image->save();

####Parameters

  • string $file = NULL - New image path
  • integer $quality = 100 - Quality of image: 1-100

###Render

// Render the image at 50% quality
$data = $image->render(NULL, 50);
 
// Render the image as a PNG
$data = $image->render('png');

####Parameters

  • string $type = NULL - Image type to return: png, jpg, gif, etc
  • integer $quality = 100 - Quality of image: 1-100