artkost / yii2-image-style-behavior
Yii 2 图片样式生成行为
0.1
2015-03-08 03:29 UTC
Requires
- yiisoft/yii2: ~2.0.0
- yiisoft/yii2-imagine: ~2.0.0
This package is auto-updated.
Last update: 2024-08-29 03:27:14 UTC
README
为您的图片创建样式集
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
$ composer require artkost/yii2-image-style-behavior
或者将以下内容添加到您的 composer.json
文件的 require
部分:
"artkost/yii2-image-style-behavior": "*"
配置
按照以下方式配置模型:
use artkost\imagestyle\ImageStyleBehavior; class ImageFile extends \yii\db\ActiveRecord { public function behaviors() { return [ 'styles' => [ 'class' => ImageStyleBehavior::className(), 'path' => '@webroot/uploads/styles', 'url' => '@web/uploads/styles', 'attribute' => 'uri', 'styles' => [ 'big' => [$this, 'styleBig'], //can be any valid callable 'small' => [$this, 'styleSmall'] ] ] ]; } /** * @return \Imagine\Image\ManipulatorInterface */ public function styleBig() { return Image::thumbnail($this->filePath, 814, 458)->save($this->style('big')->path); } }
用法
$file = ImageFile::findOne($id); echo $file->style('big')->url;