October CMS 内置一个图片尺寸调整器,可让您更改受支持图片的形状和尺寸。
要调整图片大小,使用 Resizer 外观上的 open 方法来指定文件路径。
$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');以下元素在选项数组中受支持受支持:
| 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标记过滤器 可用于在您的主题中调整图片大小。
:::