OctoberCMS:在上传时裁剪原始图像

OctoberCMS: Cropping the original image on upload

给定以下代码:

$car= new Car();
$car->name = Input::get('name');
$car->photo = Input::file('photo');
$car->save();

我需要在保存照片之前裁剪照片(使用偏移)。我尝试使用 ImageResizer 插件,但无法弄清楚如何将其 API 与上述代码集成。

是的,您可以 resize image 使用 plugin,但您甚至不需要它,因为它在内部也使用 OctoberCMS built-in Resize function

首先您需要将其保存在磁盘上,然后就地调整其大小。

for this you can use October Cms's in-built Resizer https://octobercms.com/docs/api/october/rain/database/attach/resizer

如果您只需要阅读 https://octobercms.com/docs/api/october/rain/database/attach/resizer#crop 文档,您也可以裁剪图像,一切顺利。 如果您需要,还有更多选择。

<?php namespace hardiksatasiya/...somethig;

use October\Rain\Database\Attach\Resizer;

// ...

$car= new Car();
$car->name = Input::get('name');
$car->photo = Input::file('photo');
$car->save();

// code to resize image
$width = 100;
$height = 100;
$options = []; // or ['mode' => 'crop']

Resizer::open($car->photo->getLocalPath()) // create from real path
          ->resize($width, $height, $options)
          ->save($car->photo->getLocalPath());

此代码将打开已保存的图像,调整其大小并将其保存在同一位置。

如果您有任何问题,请发表评论。