我正在尝试在 PHP 中将图像批量调整为 250 x 250 的大小
所有源图像都大于 250 x 250,因此很有帮助。
我想保持纵横比,但将它们全部设为 250 x 250。我知道图像的一部分将被裁剪以执行此操作。这对我来说不是问题
问题是我当前的脚本仅适用于宽度并根据方面制作高度,但有时,图像现在最终会变成 250 x 166。我不能使用它。
在那个原因它需要以相反的方式调整大小(高度到宽度)
脚本必须如何才能使最终图像始终为 250 x 250 且不拉伸。再说一次,我不在乎是否有裁剪。我假设某个地方会有其他人,但这现在已经超出我的想象了。我更像是一个前端的人。
任何帮助都会很棒。
以下只是我完整脚本的相关部分:
$width = 250;
$height = true;
// download and create gd image
$image = ImageCreateFromString(file_get_contents($url));
// calculate resized ratio
// Note: if $height is set to TRUE then we automatically calculate the height based on the ratio
$height = $height === true ? (ImageSY($image) * $width / ImageSX($image)) : $height;
// create image
$output = ImageCreateTrueColor($width, $height);
ImageCopyResampled($output, $image, 0, 0, 0, 0, $width, $height, ImageSX($image), ImageSY($image));
// save image
ImageJPEG($output, $destdir, 100);
$newWidth = 250;
$newHeight = 250;
// download and create gd image
$image = ImageCreateFromString(file_get_contents($url));
$width = ImageSX($image);
$height = ImageSY($image);
$coefficient = $newHeight / $height;
if ($newHeight / $width > $coefficient) {
$coefficient = $newHeight / $width;
}
// create image
$output = ImageCreateTrueColor($newWidth, $newHeight);
ImageCopyResampled($output, $image, 0, 0, 0, 0, $width * $coefficient, $height * $coefficient, $width, $height);
// save image
ImageJPEG($output, $destdir, 100);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句