加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – 上传时压缩图片文件大小?

发布时间:2021-07-06 05:23:15 所属栏目:大数据 来源: https://www.jb51.cc
导读:我正在将产品截图上传到我的网站…我想原样上传原始图像并为其创建缩略图,但在上传两个文件后,创建的缩略图的文件大小比预期的要大.有没有什么方法可以减少缩略图的文件大小而不影响PHP的质量或使用Imagemagick(我不知道如何使用但我愿意学习,如果需要)… 下

我正在将产品截图上传到我的网站…我想原样上传原始图像并为其创建缩略图,但在上传两个文件后,创建的缩略图的文件大小比预期的要大.有没有什么方法可以减少缩略图的文件大小而不影响PHP的质量或使用Imagemagick(我不知道如何使用但我愿意学习,如果需要)…
下面是我用来上传文件的代码..

<form action="<?PHP echo $_server['PHP-self'];  ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm">
    <input name="new_image" id="new_image" size="30" type="file" class="fileUpload" />
    <button name="submit" type="submit" class="submitButton">Upload/Resize Image</button>
<?PHP
    if(isset($_POST['submit'])){
      if (isset ($_FILES['new_image'])){
          $imagename = $_FILES['new_image']['name'];
          $source = $_FILES['new_image']['tmp_name'];
          $target = "images/".$imagename;
          move_uploaded_file($source, $target);

          $imagepath = $imagename;
          $save = "images/" . $imagepath; //This is the new file you saving
          $file = "images/" . $imagepath; //This is the original file

          list($width, $height) = getimagesize($file) ; 


          $tn = imagecreatetruecolor($width, $height) ; 
          $image = imagecreatefromjpeg($file) ; 
          imagecopyresampled($tn, $image, 0, 0, 0, 0, $width, $height, $width, $height) ; 

          imagejpeg($tn, $save, 100) ; 

          $save = "images/sml_" . $imagepath; //This is the new file you saving
          $file = "images/" . $imagepath; //This is the original file

          list($width, $height) = getimagesize($file) ; 

          $modwidth = 130; 

          $diff = $width / $modwidth;

          $modheight = 185; 
          $tn = imagecreatetruecolor($modwidth, $modheight) ; 
          $image = imagecreatefromjpeg($file) ; 
          imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

          imagejpeg($tn, $save, 100) ; 
        echo "Large image: <img src='images/".$imagepath."'><br>"; 
        echo "Thumbnail: <img src='images/sml_".$imagepath."'>"; 

      }
    } ?>

请指出我正确的方向……谢谢

解决方法:

不要传递100作为imagejpeg()的质量 – 超过90的任何东西通常都是矫枉过正的,只会让你获得更大的JPEG.对于缩略图,请尝试75并向下工作,直到质量/尺寸权衡可以接受.

//try this
imagejpeg($tn, $save, 75) ; 

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读