我不确定你如何在脚本的服务器端裁剪图像,但你可以做的是添加一个名为“使用完整图像”的额外按钮,其中发布数据 (x,y,width,height)将是图像的完整尺寸。然后,如果比率高于预期,您只需创建与您一直想要的相同大小的图像,按宽度调整图像大小并将其放在新图像的中间。所以下面的代码只是“伪代码”来显示我在说什么。假设用户想上传一张 1000px * 200px 的图片,而你想要的比例是 200px * 150px。用户选择“完整图像大小”,因此您发送的数据非常类似于:0,0,1000,200(位置:0,0 和宽度/高度:1000、200)。在服务器端,您可以进行所有必要的计算:
$width = $_POST['width'];
$height= $_POST['height'];
$fullsize = isset($_POST['fullsize']) && $_POST['fullsize']; // If the full size mode is on.
if ($fullsize) {
$image = new Image(0,0,200,150); // Create the image canvas with whatever plugin your using.
if (($width/$height) > (200/150)) { // If WIDTH ratio is higher...
$x = 0; // We already know the image will be align on the left because of the previous calculations (ratio)
$y = (150/2) - ($height/2); // 50% top - half the image size = center.
}
if (($height/$width) > (150/200)) { // If HEIGHT ratio is higher...
// Repeat previous code with adjusted values for height instead of width.
}
// The next part is pure fiction, i dont know what php extension you use, but lets pretend this is working.
$newImage = new Image($x,$y,$width,$height,$file); // $file is probably the $_FILEs...
$image->placeInsideCanvas($newImage); // Imagine this adds the image into your previously created canvas (200/150);
$image->output();
}
我希望这有帮助。