gpt4 book ai didi

php - 使用 Laravel 5 intervention image 为图像添加空白以制作正方形图像

转载 作者:可可西里 更新时间:2023-10-31 23:01:30 26 4
gpt4 key购买 nike

假设我有一个最喜欢的正方形尺寸,在本例中它的宽度和高度为 2236 px

我需要使用 php intervention package 在我的服务器上保存我的图像。

用户的图像大小是多少并不重要,关键是图像必须以新的大小保存,但用户图像必须位于正方形的中心和中间并且如果图片小于我喜欢的尺寸,则必须拉伸(stretch)它,如果图片更大,则必须将其压缩到我喜欢的尺寸

请看这张图: my plan

下面是一些真实的例子: example 1 example 2

有没有人遇到过这种情况,你知道我该怎么做吗?

提前致谢

最佳答案

<?php
$width = 2236;
$height = 2236;

$img = Image::make('image.jpg');

// we need to resize image, otherwise it will be cropped
if ($img->width() > $width) {
$img->resize($width, null, function ($constraint) {
$constraint->aspectRatio();
});
}

if ($img->height() > $height) {
$img->resize(null, $height, function ($constraint) {
$constraint->aspectRatio();
});
}

$img->resizeCanvas($width, $height, 'center', false, '#ffffff');
$img->save('out.jpg');

关于php - 使用 Laravel 5 intervention image 为图像添加空白以制作正方形图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44350072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com