要修改图片尺寸可以使用PHP的GD库,以下是一个简单的示例代码:
// 原始图片路径
$src = "path/to/image.jpg";
// 目标图片路径
$dst = "path/to/newimage.jpg";
// 目标图片宽度
$newWidth = 800;
// 目标图片高度
$newHeight = 600;
// 获取原始图片信息
list($width, $height) = getimagesize($src);
// 计算缩放比例
$scale = min($newWidth / $width, $newHeight / $height);
// 计算缩放后的宽度和高度
$newWidth = round($scale * $width);
$newHeight = round($scale * $height);
// 创建原始图片资源
$srcImg = imagecreatefromjpeg($src);
// 创建目标图片资源
$dstImg = imagecreatetruecolor($newWidth, $newHeight);
// 将原始图片缩放并复制到目标图片中
imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
// 保存目标图片
imagejpeg($dstImg, $dst);
以上代码将会将一个 JPEG 格式的图片缩放到指定尺寸,并保存为另一个 JPEG 格式的图片。你可以根据需要进行适当的修改,例如修改图片格式、调整缩放比例等。