PHP 在不裁剪的情况下将上传的图片调整为特定尺寸
PHP Resize Uploaded Image To Specific Dimensions Without Cropping
我目前有一个脚本,用户可以在其中上传任何尺寸的图像文件。
上传的图像通过 ajax 发送到 PHP 脚本,在那里它应该调整大小并保存到服务器。
调整大小的过程不应裁剪或扭曲图像,而是通过在边上添加白色来将其调整为特定尺寸,或者 top/bottom 如果它与尺寸不完全匹配。我有这个过程非常适合方形图像 - 但是当尝试修改过程以适用于矩形尺寸时它不再正常运行。
$sourceImage = imagecreatefromjpeg("../img/whiteBG.jpg");
$dimensions = getimagesize($files["tmp_name"][$i]);
$ratio = $dimensions[0] / $dimensions[1]; // width/height
$dst_y = 0;
$dst_x = 0;
//final image should be 600x360
if ($ratio > 1) {
$width = 600;
$height = 360 / $ratio;
$dst_y = (360 - $height) / 2;
} else {
$width = 600 * $ratio;
$height = 360;
$dst_x = (600 - $width) / 2;
}
$src = imagecreatefromstring(file_get_contents($files["tmp_name"][$i]));
$dst = imagecreatetruecolor($width, $height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $dimensions[0], $dimensions[1]);
imagecopymerge($sourceImage, $dst, $dst_x, $dst_y, 0, 0, imagesx($dst), imagesy($dst), 100);
$moved = imagepng($sourceImage, $dir . $filename);
输出图像 ($moved) 的最终尺寸应为 600 x 360。相反,最终图像总是失真的。如果上传高图片比例,则最终产品会在宽度方向拉伸。如果上传更宽的图像比例,则它会被压缩并以额外的顶部和底部间距混合。 whiteBG.jpg 只是一张尺寸为 600x360
的普通白色 jpeg
最终更正了这一点。问题是我如何计算和补偿比率。这是纠正问题的最终代码,使用变量使代码在其他情况下更加通用
$targetWidth = 600;
$targetHeight = 360;
$dstY = 0;
$dstX = 0;
$ratioFactor = 0;
$bgImage = imagecreatefromjpeg("../img/bgWhite.jpg");
$dimensions = getimagesize($files["tmp_name"][$i]);
$sourceWidth = $dimensions[0];
$sourceHeight = $dimensions[1];
$ratio = $sourceWidth / $sourceHeight; // width/height
//final image should be 600x360
if ($ratio > 1) {
$ratioFactor = $sourceWidth / $targetWidth;
$targetHeight = $sourceHeight / $ratioFactor;
$dstY = (360 - $targetHeight) / 2;
} else {
$ratioFactor = $sourceHeight / $targetHeight;
$targetWidth = $sourceWidth / $ratioFactor;
$dstX = (600 - $targetWidth) / 2;
}
$source = imagecreatefromstring(file_get_contents($files["tmp_name"][$i]));
$target = imagecreatetruecolor($targetWidth, $targetHeight);
imagecopyresampled($target, $source, 0, 0, 0, 0, $targetWidth, $targetHeight, $sourceWidth, $sourceHeight);
imagecopymerge($bgImage, $target, $dstX, $dstY, 0, 0, imagesx($target), imagesy($target), 100);
imagepng($bgImage, $dir . $filename);
我目前有一个脚本,用户可以在其中上传任何尺寸的图像文件。 上传的图像通过 ajax 发送到 PHP 脚本,在那里它应该调整大小并保存到服务器。 调整大小的过程不应裁剪或扭曲图像,而是通过在边上添加白色来将其调整为特定尺寸,或者 top/bottom 如果它与尺寸不完全匹配。我有这个过程非常适合方形图像 - 但是当尝试修改过程以适用于矩形尺寸时它不再正常运行。
$sourceImage = imagecreatefromjpeg("../img/whiteBG.jpg");
$dimensions = getimagesize($files["tmp_name"][$i]);
$ratio = $dimensions[0] / $dimensions[1]; // width/height
$dst_y = 0;
$dst_x = 0;
//final image should be 600x360
if ($ratio > 1) {
$width = 600;
$height = 360 / $ratio;
$dst_y = (360 - $height) / 2;
} else {
$width = 600 * $ratio;
$height = 360;
$dst_x = (600 - $width) / 2;
}
$src = imagecreatefromstring(file_get_contents($files["tmp_name"][$i]));
$dst = imagecreatetruecolor($width, $height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $dimensions[0], $dimensions[1]);
imagecopymerge($sourceImage, $dst, $dst_x, $dst_y, 0, 0, imagesx($dst), imagesy($dst), 100);
$moved = imagepng($sourceImage, $dir . $filename);
输出图像 ($moved) 的最终尺寸应为 600 x 360。相反,最终图像总是失真的。如果上传高图片比例,则最终产品会在宽度方向拉伸。如果上传更宽的图像比例,则它会被压缩并以额外的顶部和底部间距混合。 whiteBG.jpg 只是一张尺寸为 600x360
的普通白色 jpeg最终更正了这一点。问题是我如何计算和补偿比率。这是纠正问题的最终代码,使用变量使代码在其他情况下更加通用
$targetWidth = 600;
$targetHeight = 360;
$dstY = 0;
$dstX = 0;
$ratioFactor = 0;
$bgImage = imagecreatefromjpeg("../img/bgWhite.jpg");
$dimensions = getimagesize($files["tmp_name"][$i]);
$sourceWidth = $dimensions[0];
$sourceHeight = $dimensions[1];
$ratio = $sourceWidth / $sourceHeight; // width/height
//final image should be 600x360
if ($ratio > 1) {
$ratioFactor = $sourceWidth / $targetWidth;
$targetHeight = $sourceHeight / $ratioFactor;
$dstY = (360 - $targetHeight) / 2;
} else {
$ratioFactor = $sourceHeight / $targetHeight;
$targetWidth = $sourceWidth / $ratioFactor;
$dstX = (600 - $targetWidth) / 2;
}
$source = imagecreatefromstring(file_get_contents($files["tmp_name"][$i]));
$target = imagecreatetruecolor($targetWidth, $targetHeight);
imagecopyresampled($target, $source, 0, 0, 0, 0, $targetWidth, $targetHeight, $sourceWidth, $sourceHeight);
imagecopymerge($bgImage, $target, $dstX, $dstY, 0, 0, imagesx($target), imagesy($target), 100);
imagepng($bgImage, $dir . $filename);