【问题标题】:compress image file size on upload?上传时压缩图像文件大小?
【发布时间】:2023-11-05 01:39:01
【问题描述】:

我正在将产品截图上传到我的网站...我想按原样上传原始图像并为其创建一个缩略图,但是在上传这两个文件后,创建的缩略图的文件大小比预期的要大一些.有没有什么办法可以减小缩略图的文件大小而不会影响 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."'>"; 

      }
    } ?>

请指点我正确的方向...谢谢

【问题讨论】:

    标签: php image-processing


    【解决方案1】:

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

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

    【讨论】:

    • 这会查找图像的服务器位置。但我想从本地图像调整大小并上传
    【解决方案2】:

    你好@halocursed 我只是尝试使用你的代码压缩不同的图像类型,比如 png 和 gif,而不是图像变黑。所以,我修改了代码块并为 jpg、png 工作良好。

    <?php
        if(isset($_POST['submit'])){
          if (isset ($_FILES['new_image'])){
              // print_r($_FILES); die;
              $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);
              $info = getimagesize($target);
              if ($info['mime'] == 'image/jpeg'){
                $image = imagecreatefromjpeg($file);
              }elseif ($info['mime'] == 'image/gif'){
                $image = imagecreatefromgif($file);
              }elseif ($info['mime'] == 'image/png'){
                $image = imagecreatefrompng($file);
              }
    
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $width, $height, $width, $height);
              imagejpeg($tn, $save, 60);
    
              echo "Large image: ".$imagepath;
    
          }
        } 
    ?>
    

    【讨论】:

      【解决方案3】:

      75 是默认的质量设置,但是如果您使用它,您会发现质量显着下降。 90 可为您提供出色的图像质量并将文件大小减少一半,如果您想进一步减小文件大小,请使用 85 或 80,但没有低于此值。

      【讨论】:

        【解决方案4】:

        应该是 60,它代表 60%。

        示例: 如果您在 Photoshop 中打开图像并尝试将其保存为 web 并选择 jpg,您可以看到使用 60 时它仍然处于高质量状态,但文件大小较小。如果你想要更低,更多退化,这意味着颜色失真更多。

        超过 60 并没有给你任何更好的东西,只有更大的文件大小。

        这是网络的标准图像优化。保持高质量,但尽可能减小文件大小。

        【讨论】:

          【解决方案5】:

          100% 的质量设置非常大。尝试 85% 或 90%,您不会在大多数图像上看到差异。

          见: http://www.ampsoft.net/webdesign-l/jpeg-compression.html

          【讨论】:

            【解决方案6】:

            如果使用 jpeg,使用 75 到 85 之间的质量通常是可以接受的(而且 100 占用太多空间,顺便说一句,增益并不那么重要),至少对于照片而言。

            作为旁注,如果您正在处理屏幕截图,使用 PNG 可能会获得更好的质量:jpeg 会降低图像质量(对于照片来说这很好,但对于屏幕截图,字体是按像素绘制的,会带来一些不好看的效果)

            【讨论】:

              【解决方案7】:

              它将压缩图像与原始图像一起移动。实际上我只想移动以“rxn-”开头的压缩图像。

                              include("do.php");
                              session_start();
                              if(is_array($_FILES)) {
              
                              for($i=0; $i<count($_FILES['userImage']['tmp_name']); $i++){
              
              
                                      if(is_uploaded_file($_FILES['userImage']['tmp_name'][$i])) {
                                      $sourcePath = $_FILES['userImage']['tmp_name'][$i];
                                      $upload_dir = "images/";
                                      $targetPath = "images/".basename($_FILES['userImage']['name'][$i]);
                                      $source_image = "images/".basename($_FILES['userImage']['name'][$i]);
                                      $imageName =$_FILES['userImage']['name'][$i];
              
              
                                      $imageFileType = strtolower(pathinfo($targetPath,PATHINFO_EXTENSION)); 
              
                                      $check = getimagesize($_FILES["userImage"]["tmp_name"][$i]);
              
              
              
                                      if (file_exists($targetPath)) {
                                         // echo "<span style='color:red;'> file already exists</span> ";
              
                                      }
              
                                            if($check !== false) {
                                             // echo "File is an image - " . $check["mime"] . ".";
                                              $uploadOk = 1;
                                          } else {
                                              echo "<span style='color:red;'>File is not an image.</span><br>";
                                              $uploadOk = 0;
                                          }
              
              
                                      if($imageFileType == "jpg" || $imageFileType == "png" || $imageFileType == "jpeg"
                                      ||  $imageFileType =="gif" ) {
              
              
              
                                      if(move_uploaded_file($sourcePath,$targetPath)) 
                                      //if(1) 
                                      {
              
                                          $image_destination = $upload_dir."rxn-".$imageName;
                                          $compress_images = compressImage($source_image, $image_destination);
              
                                          $pId=$_SESSION['pId'];
              
                                          $sql="insert into image(p_id,img_path) values('$pId','$compress_images')";
                                          $result = mysql_query($sql);
                                          echo "
                                          <div class='col-sm-3' id='randomdiv' style='padding-bottom: 15px;'>  
                                          <div class='bg_prcs uk-height-small uk-flex uk-flex-center uk-flex-middle   uk-background-cover uk-light   uk-card-default uk-card-hover imgt'   data-src='$image_destination' uk-img>
                                           </div>
                                          </div>
              
              
                                          ";  
              
              
                                      }
              
                                      }
                                      else{ echo "<span style='color:red;'> only JPG, JPEG, PNG & GIF files are allowed.</span>";
                                           }
              
              
              
              
              
              
                                      }
              
                              }
              
                              }
              
                              // created compressed JPEG file from source file
                              function compressImage($source_image, $compress_image) {
                              $image_info = getimagesize($source_image);
                              if ($image_info['mime'] == 'image/jpeg') {
                              $source_image = imagecreatefromjpeg($source_image);
                              imagejpeg($source_image, $compress_image, 75);
                              } elseif ($image_info['mime'] == 'image/gif') {
                              $source_image = imagecreatefromgif($source_image);
                              imagegif($source_image, $compress_image, 75);
                              } elseif ($image_info['mime'] == 'image/png') {
                              $source_image = imagecreatefrompng($source_image);
                              imagepng($source_image, $compress_image, 6);
                              }
                              return $compress_image;
                              }
              
              
                              ?>
              

              【讨论】: