使用 CakePHP 2 上传插件后创建缩略图

Create thumbnail images after using the CakePHP 2 upload plugin

我使用 CakePHP Upload plugin 已经有一段时间了,我真的很满意它:

public $actsAs = array(
    'Upload.Upload' => array(
        'image' => array(
            'fields' => array(
                'dir' => 'dir'
            ),
            'thumbnailSizes' => array(
                'xvga' => '1024x768',
                'vga' => '640x480',
                'thumb' => '300x300'
            ),
            'thumbnailMethod'  => 'php',
        )
    )
);

在新用例中,我必须制作一些比将分辨率静态降级到 640x640 或 300x300 更好的缩略图。我想要每个图像方向的分辨率或者说图像不应超过 30kB 的能力。

插件作者 Jose Gonzales 明确表示上传插件不会创建缩略图:

此插件不创建缩略图。您可以使用自定义转换器来创建文件上传的修改版本。

我想知道这如何与现有的上传插件一起实现?

In a new usecase I have to make some better thumbnails than some static downgrade of the resolution to 640x640 or 300x300

是什么阻止您在插件处理完上传后进行自定义图像处理?您也可以扩展行为并添加您想要的内容。

你可以使用我的 Imagine plugin that comes with a behavior for easy image processing. Or combine it with FileStorage,它使用 Imagine 在上传后自动处理图像。

或直接使用the underlying library Imagine

I want to have a resolution per image orientation or the ability to say that an image should not exceed 30kB.

为此使用验证规则。

最后我使用了来自 thephpleagueglidehttps://github.com/thephpleague/glide

如文档页面所述,它允许 "on-the-fly" 图像处理,例如:

  • 使用基于简单的 HTTP API.
  • 对图像进行调整、调整大小和添加效果
  • 经过处理的图像会自动缓存并与 far-future 到期 headers。
  • 改变方向
  • 添加水印
  • 添加过滤器
  • 添加边框
  • 定义图像的最大尺寸

在 CakePHP 中实现就这么简单(不需要插件)。将库作为依赖项添加到 composer.json:

"require": {
    "league/glide" : "1.0.x"
},

运行 ./composer.phar update

在控制器动作中可以这样调用:

public function imageResize() {
    $server = League\Glide\ServerFactory::create([
        'source' => 'files',
        'cache' => 'files/cache',
        'watermarks' => 'files/watermarks'
    ]);

    $server->outputImage($path, $_GET);
}

所以我可以像这样简单地从应用程序调用图像:

https://www.url.com/images/imageResize?w=480&mark=mark.png&markpos=right&markh=60&markpad=4

另一个有用的库是 Adaptive Imageshttp://adaptive-images.com

它会检测您的访问者的屏幕尺寸,并自动创建、缓存和提供适合您网页的 re-scaled 版本的嵌入 HTML 图像的设备。无需 mark-up 更改。它旨在与响应式设计一起使用,并与流体图像技术结合使用。