我正在尝试在上传之前调整图像的大小。我在下面的代码中添加了$img = resize_image($_FILES['Image']['tmp_name'], 200, 200);行。上传的代码工作正常。现在我只是试着调整图像的大小。我不确定如何获取返回值$img并将其与move_uploaded_file一起保存。当我尝试用$img替换$_FILES['Image']['tmp_name]时,我得到一个解析错误。我还添加了resize_image函数,这样您就可以看到它到底返回了什么。
{
$image_name= $_FILES['Image']['name'];
$img = resize_image($_FILES['Image']['tmp_name'], 200, 200);
$image_name = $picCounter .$firstName .$image_name;
move_uploaded_file( $_FILES['Image']['tmp_name'], "IMG/$image_name");
correctImageOrientation("IMG/$image_name");
$image_path = "/IMG/" .$image_name;
$picCounter = $picCounter++;
$sql = ("UPDATE LoginInformation SET PicCounter = PicCounter + 1 WHERE Email = '" . $useremail . "'" );
mysqli_query($conn, $sql);
}
else
{
$image_path = "/IMG/square-image.png";
}下面是resize函数
function resize_image($file, $w, $h, $crop=FALSE) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}发布于 2019-05-01 11:56:03
你不能调整图片大小之前上传与PHP,PHP是服务器端语言,将只有一次上传的图像,而不是之前。
您需要通过JavaScript完成此操作。看看这个:Use HTML5 to resize an image before upload
https://stackoverflow.com/questions/55931146
复制相似问题