mauglee/yii2-easyimage

为 Yii2 的图像组件

安装: 20

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 6

类型:yii2-extension

1.0.2 2016-07-13 06:16 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:35:56 UTC


README

为 Yii2 的图像组件

此扩展基于 yii-easyImage

安装

安装此扩展的最佳方式是通过 composer

运行以下命令之一

php composer.phar require mauglee/yii2-easyimage

"mauglee/yii2-easyimage": "@dev"

将以下内容添加到您的 composer.json 文件的 require-dev 部分中。

使用方法

安装扩展后,只需按以下方式修改您的应用程序配置

return [
    'components' => [
        'easyImage' => [
            'class'         => 'cliff363825\image\EasyImage',
            'driver'        => 'GD',
            'quality'       => 100,
            'cachePath'     => '/easyimage/',
            'cacheTime'     => 2592000,
            'retinaSupport' => true,
            'pixel_ratio'   => [ 2, 3 ], // Device pixels per CSS pixel (retina stuff)
            'basePath'      => '@webroot',
            'baseUrl'       => '@web',
        ]
    ],
];

参数

  • string $file 必需 - 图像文件路径
  • string $driver - 驱动程序: GD, Imagick

ThumbOf

您可以直接在 View 中创建缩略图

// 创建并自动缓存

Yii::$app->easyImage->thumbOf('/path/to/image.jpg', ['rotate' => 90]);

// 或者

Yii::$app->easyImage->thumbOf('image.jpg', ['rotate' => 90],  ['class' => 'image']);

// 或者

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

注意。此方法返回 Html::img()

示例使用不同的文件类型,不同的图像源路径

// in real situation those filenames are retrieved from DB 
$images = [
    '01.jpg',
    '02.jpg',
    '03.png',
];

foreach ( $images as $image ) {
    $file_path = Yii::getAlias( '@app/data/image/logo/' ) . $image;
    if ( is_file( $file_path ) ) {
        echo Yii::$app->easyImage->thumbOf( $file_path, [
            'resize'  => [ 'width' => 300, 'height' => 300 ],
            'type'    => 'jpg',
            'quality' => '86',
        ] );
    }
}

参数

  • string $file 必需 - 图像文件路径
  • array $params - 图像处理方法
  • array $htmlOptions - Html::img() 的选项

有关使用详情的完整信息,请参阅 作者页面上的文档注意:由于原始版本是为 Yii v1 编写的,因此可能会出现一些差异。