artkost/yii2-image-style-behavior

Yii 2 图片样式生成行为

安装: 45

依赖: 0

建议: 0

安全: 0

星标: 3

关注者: 2

分支: 1

公开问题: 0

类型:yii2-extension

0.1 2015-03-08 03:29 UTC

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;