October CMS 内置了一个图像调整器,可让您更改支持图像的形状和大小。
To resize an image, use the open method on the Resizer facade to target a file path.
$image = Resizer::open('path/to/image.jpg');您也可以从 页面请求输入 传递一个上传的文件。
$image = Resizer::open(Input::file('field_name'));要调整图像大小,调用对象的 resize 方法来执行调整大小操作。第一个参数是图像宽度,第二个参数是图像高度,第三个参数是一个调整大小参数数组。
$image->resize(800, 600, ['mode' => 'crop']);宽度和高度参数也是可选的,例如,要使用现有宽度并仅调整高度,将第二个参数设为 null。此值会根据 mode 选项,使用原始图像比例(自动等比例缩放)进行计算。
$image->resize(800, null, [...]);最后,使用 save 方法将调整大小后的图像保存到新位置。
$image->save('path/to/new/file.jpg');以下元素在 options 数组中受支持 受支持:
| Key | Description | Default | Options |
|---|---|---|---|
mode | How the image should be fitted to dimensions | auto | exact, portrait, landscape, auto, fit, or crop |
offset | Offset the crop of the resized image | [0,0] | [left, top] |
quality | Quality of the resized image | 90 | 0-100 |
sharpen | Amount to sharpen the image | 0 | 0-100 |
mode 选项允许您指定图像应如何调整大小。可用模式如下:
| Mode | Description |
|---|---|
auto | Automatically choose between portrait and landscape based on the image's orientation |
exact | Resize to the exact dimensions given, without preserving aspect ratio |
portrait | Resize to the given height and adapt the width to preserve aspect ratio |
landscape | Resize to the given width and adapt the height to preserve aspect ratio |
crop | Crop to the given dimensions after fitting as much of the image as possible inside those |
fit | Fit the image inside the given maximal dimensions, keeping the aspect ratio |
这 System\Classes\ResizeImages 类可用于调整图像大小并通过 URL 提供:
$url = \System\Classes\ResizeImages::resize($image, $width, $height, $options);如果您正在调整媒体库项目的大小,您应该使用 Media\Classes\MediaLibrary 类和 url 方法来解析媒体路径。
$image = \Media\Classes\MediaLibrary::url('relative/path/to/asset.jpg');
$url = \System\Classes\ResizeImages::resize($image, $width, $height, $options);还有一个
|resize标记过滤器 可用于调整主题中的图片大小.