在使用PHP上传到文件夹之前,如何压缩图像大小?

在使用PHP上传到文件夹之前,如何压缩图像大小?,php,image,compression,Php,Image,Compression,在将图像上载到文件夹之前,您将如何压缩图像大小。我有我的代码下面是我到目前为止,我将如何压缩图像?谢谢 $temp = explode(".", $_FILES["Image"]["name"]); $newfilename = round(microtime(true)) . '.' . end($temp); echo $newfilename; if (!!$_FILES['Image']['tmp_name']) { $info = explode('.', strtolowe

在将图像上载到文件夹之前,您将如何压缩图像大小。我有我的代码下面是我到目前为止,我将如何压缩图像?谢谢

$temp = explode(".", $_FILES["Image"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
echo $newfilename;

if (!!$_FILES['Image']['tmp_name']) {
    $info = explode('.', strtolower($_FILES['Image']['name'])); 
    if (in_array( end($info), $allow)) 
        if(move_uploaded_file( $_FILES['Image']['tmp_name'], $todir . $newfilename  ) )        {
          echo "success";
        }
    }
    else {
      echo "error";
    }
我找到了这段代码,但我不确定如何用我的代码实现它

function compress($source, $destination, $quality) {

    $info = getimagesize($source);

    if ($info['mime'] == 'image/jpeg') 
        $image = imagecreatefromjpeg($source);

    elseif ($info['mime'] == 'image/gif') 
        $image = imagecreatefromgif($source);

    elseif ($info['mime'] == 'image/png') 
        $image = imagecreatefrompng($source);

    imagejpeg($image, $destination, $quality);

    return $destination;
}

$source_img = 'source.jpg';
$destination_img = 'destination .jpg';

$d = compress($source_img, $destination_img, 90);

在这里你将得到你问题的答案

(一)


2)

PHP是服务器端,因此必须先将文件上载到服务器,然后PHP才能操作文件。如果您的意思是在移动到上载文件夹之前进行压缩:或者您可以使用它的易用性,您可以使用此库进行压缩、裁剪、筛选等。@catcon问题是我不知道如何使用我的代码设置来实现这一点。我需要帮助了解如何压缩它,然后插入它或请不要只是张贴链接。您应该在答案中添加问题的实际解决方案,并且只添加链接作为参考。此外,如果第一个链接(即SO链接)回答了问题,则应将其作为副本关闭,而不是回答。