codificar/yii2imagecache

为 Yii2 的图片缓存

0.0.5 2021-08-19 23:59 UTC

This package is auto-updated.

Last update: 2024-09-20 06:57:07 UTC


README

Codificar

Yii2 ImageCache

由 Codificar 开发的库。

React Native is released under the MIT license. Versão Downloads

基于 yii2-imagecache

安装

安装此辅助程序的推荐方式是通过 composer

运行以下命令之一

php composer.phar require "codificar/yii2imagecache" "0.0.5"

"codificar/yii2imagecache" : "0.0.5"

将其添加到您的应用程序的 composer.json 文件的 require 部分。

配置

您应该在

  • ThumbAction 添加到您的控制器之一。
  • 修改您的应用程序配置
    • 添加 imageCache 组件,
    • 添加 URL 规则以处理对缺失缩略图的请求。

添加 ThumbAction

您需要将 ThumbAction 添加到您的控制器之一,以便 imageCache 可以处理对缺失缩略图的请求并在需要时创建它们。您可以使用 site 控制器

class SiteController extends Controller
{
  ...
  public function actions()
  {
      return [
        ...
        'thumb' => 'codificar\yii2imagecache\ThumbAction',
        ...
      ];
  }
  ...
}

imageCache 组件配置

您应该在您的应用程序配置中添加 imageCache 组件

$config = [
    'components' => [
      ...
      'imageCache' => [
        'class' => 'iutbay\yii2imagecache\ImageCache',
        'sourcePath' => '@app/web/images',
        'sourceUrl' => '@web/images',
        //'thumbsPath' => '@app/web/thumbs',
        //'thumbsUrl' => '@web/thumbs',
        //'sizes' => [
        //    'thumb' => [150, 150],
        //    'medium' => [300, 300],
        //    'large' => [600, 600],
        //],
      ],
      ...
    ],
];

urlManager 配置

您应该修改您的 urlManager 配置

$config = [
    'components' => [
      ...
      'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
          ...
          'thumbs/<path:.*>' => 'site/thumb',
          ...
        ],
      ],
      ...
    ],
];

如何使用

<?= Yii::$app->imageCache->thumb('/your-app/images/2014/test.jpg') ?>
// <img src="/your-app/thumbs/2014/test_thumb.jpg" alt="">

<?= Yii::$app->imageCache->thumbSrv('/your-app/images/2014/test.jpg') ?>
// url_string ="/your-app/thumbs/2014/test_medium.jpg"