代码之家  ›  专栏  ›  技术社区  ›  halocursed

上传时压缩图像文件大小?

  •  11
  • halocursed  · 技术社区  · 17 年前

    我正在将产品截图上载到我的网站…我想按原样上载原始图像,并为其创建一个缩略图,但在上载两个文件后,创建的缩略图的文件大小比预期的要大一点。是否有任何方法可以在不影响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."'>"; 
    
          }
        } ?>
    

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

    7 回复  |  直到 8 年前
        1
  •  16
  •   Paul Dixon    17 年前

    质量不超过100分 imagejpeg() -任何超过90的都是过度杀伤力,只会给你一个更大的jpeg。对于缩略图,请尝试75并向下操作,直到质量/尺寸折衷可以接受为止。

    //try this
    imagejpeg($tn, $save, 75) ; 
    
        2
  •  4
  •   Kamlesh Kumar    9 年前

    您好@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
  •  2
  •   Alix Axel    17 年前

    75是默认的质量设置,但是如果使用它,您会发现质量会大大降低。90给你一个伟大的图像质量和减少一半的文件大小,如果你想减少文件的大小,甚至更多地使用85或80,但没有下文。

        4
  •  2
  •   Marijan    14 年前

    它应该是60,代表60%。

    例子: 如果您在Photoshop中打开一个图像,并尝试将其保存到Web并选择JPG,您可以通过使用60看到它仍然处于高质量,但文件大小较低的状态。如果你想要更低,更退化,意味着颜色更扭曲。

    超过60不会给你任何更好的,只有更大的文件大小。

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

        5
  •  1
  •   Rufinus    17 年前

    100%的质量设置相当大。尝试85%或90%你不会看到在大多数图像上的差异。

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

        6
  •  1
  •   Pascal MARTIN    17 年前

    如果使用jpeg,使用介于75和85之间的质量通常是不可接受的(100占用了太多的空间,对于一个不那么重要的增益,btw),至少对于照片而言。

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

        7
  •  0
  •   Roshan Kr Soni    8 年前

    它用原始图像移动压缩图像。实际上,我只想移动以“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;
                    }
    
    
                    ?>
    
    推荐文章