yii-ext/yii-easyimage

Yii 框架扩展,用于实时创建和缓存缩略图。

安装: 36

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 23

类型:yii-extension

0.2.2 2014-06-17 07:39 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:11:19 UTC


README

你不需要为项目中的图片创建许多类型的缩略图。你可以在视图(View)中直接创建缩略图。缩略图将自动缓存。很简单!

特性

  • 易于使用
  • 支持 GDImagick
  • 自动缓存缩略图
  • 按子目录排序缓存
  • 支持 Retina 显示
  • 基于 Kohana Image Library。

##安装和配置 将 EasyImage 文件夹提取到 protected/extensions 之下

在你的配置文件 components 部分,添加以下内容

'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,
  ),

并在 import 部分,添加以下内容

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

##用法 ###实例化

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

####参数

  • string $file 必需 - 图片文件路径
  • string $driver - 驱动:GDImagick

ThumbOf

你可以在视图中直接创建缩略图

// 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,
  ));

注意。 此方法返回 CHtml::image()

####参数

  • string $file 必需 - 图片文件路径
  • array $params - 图片处理方法。参见 方法
  • array $htmlOptions - 对于 CHtml::image() 的选项

ThumbSrcOf

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

注意。 此方法返回缓存中图片的路径。

  • string $file 必需 - 图片文件路径
  • array $params - 图片处理方法。参见 方法

####参数

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

####参数

  • ##方法 ###Resize
  • integer $width - 新宽度
  • integer $height - 新高度

integer $master - 主维度:EasyImage::RESIZE_NONEEasyImage::RESIZE_WIDTHEasyImage::RESIZE_HEIGHTEasyImage::RESIZE_AUTOEasyImage::RESIZE_INVERSEEasyImage::RESIZE_PRECISE

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

####参数

  • ###Crop
  • integer $width 必需 - 新宽度
  • integer $height 必需 - 新高度
  • mixed $offset_x = NULL - 从左边偏移

mixed $offset_y = NULL - 从顶部偏移

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

####参数

  • ###Rotate

integer $degrees 必需 - 旋转度数:-360-360

$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));

####参数

  • ###Flip

integer $direction 必需 - 方向:EasyImage::RESIZE_NONEEasyImage::RESIZE_WIDTH

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

####参数

  • ###Sharpen

integer $amount 必需 - 加锐量(百分比):1-100

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

###Reflection

  • 注意。 默认情况下,反射将从顶部透明到底部不透明。
  • integer $height = NULL - 反射高度
  • integer $opacity = 100 - 反射不透明度:0-100

boolean $fade_in = FALSE - TRUE 用于渐显,FALSE 用于渐隐

$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)));

###Watermark

  • 注意。 如果没有指定偏移量,则使用轴心中心。如果指定了 TRUE 的偏移量,则使用轴心底部。
  • mixed $watermark 必需 - 水印 EasyImage 实例或图片路径
  • integer $offset_x = NULL - 从左边偏移
  • integer $offset_y = NULL - 从顶部偏移

integer $opacity = 100 - 水印不透明度:1-100

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

###Background

  • 注意 这仅适用于具有 alpha 透明度的图片。
  • string $color 必需 - 十六进制颜色值

integer $opacity = 100 - 背景不透明度:0-100

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

###Quality

  • 注意 这仅适用于 JPG 图片。

integer required - 图片质量:1-100

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

####参数

  • ###Type

string required - 返回的图片类型:pngjpggif,等

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

####参数

  • 字符串 $file = NULL - 新图像路径
  • 整数 $quality = 100 - 图像质量:1-100

### 渲染

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

####参数

  • 字符串 $type = NULL - 返回的图像类型:pngjpggif
  • 整数 $quality = 100 - 图像质量:1-100