【问题标题】:How do you compress image size before uploading to folder using PHP?在使用 PHP 上传到文件夹之前如何压缩图像大小?
【发布时间】:2019-11-19 16:15:55
【问题描述】:

在将图像上传到文件夹之前,您将如何压缩图像大小。到目前为止,我的代码低于我的代码,我将如何压缩图像?谢谢!

$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);

【问题讨论】:

  • PHP 是服务器端,所以文件必须先上传到服务器,然后 PHP 才能操作文件。如果您的意思是在移动到上传文件夹之前进行压缩:stackoverflow.com/questions/11418594/…
  • 您也可以使用php intervention,它易于使用,您可以使用此库进行压缩、裁剪、过滤等操作。
  • @catcon 的问题是我不知道如何通过我的代码设置来实现它。我需要帮助了解如何压缩它然后插入它,反之亦然

标签: php image compression


【解决方案1】:

【讨论】:

  • 请不要只发布链接。您应该在答案本身中添加问题的实际解决方案,并且只添加链接作为参考。此外,如果第一个链接(即 SO 链接)回答了问题,则应将其作为副本关闭,而不是回答。
猜你喜欢
  • 2018-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多