我希望为用户上传的非正方形图片添加背景。因此,如果他们上传一张又高又瘦的照片,我想在图像的两侧添加白色背景,使生成的图像具有 1:1 的纵横比。这可能使用 PHP 或 javascript 吗?
最佳答案
您可以使用 GD 库做什么,有一个名为 Wideimage 的库轻而易举:
$image = WideImage::load('img_form_field_name');
$size = max($image->getHeight(), $image->getWidth());
$white = $image->allocateColor(255, 255, 255);
$image->resizeCanvas($size, $size, 'center', 'center', $white);
参见 documentation和 examples , 许多功能甚至可以是 tested interactively .
关于php - 使用 PHP 处理图像 : force 1:1 aspect ratio (sqaure images),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7747721/