【问题标题】:Compress image size on upload -PHP上传时压缩图像大小-PHP
【发布时间】:2014-03-01 15:56:54
【问题描述】:

我正在将屏幕截图上传到我的网站。 我想上传具有相同宽度和高度属性的原始图像,只是我想减小它的大小,例如,如果上传前的图像大小是 3 MB,那么上传/保存后它必须是 300 KB 但在我的情况下,图像是生成的没有显示完全空白的图像。

下面是我用来上传文件的代码..

<form action="" 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, 70) ; 

          $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, 70) ; 
        echo "Large image: <img src='images/".$imagepath."'><br>"; 
        echo "Thumbnail: <img src='images/sml_".$imagepath."'>"; 

      }
    } ?>

如果我上传 jpg 图像/但不为其他人工作,它工作正常 我想为每种图像类型使用它

【问题讨论】:

    标签: php image


    【解决方案1】:

    对于类型检测,请使用以下代码和说明。

    <?php
    function imageCreateFromAny($filepath) {
        $type = exif_imagetype($filepath); // [] if you don't have exif you could use getImageSize()
        $allowedTypes = array(
            1,  // [] gif
            2,  // [] jpg
            3,  // [] png
            6   // [] bmp
        );
        if (!in_array($type, $allowedTypes)) {
            return false;
        }
        switch ($type) {
            case 1 :
                $im = imageCreateFromGif($filepath);
            break;
            case 2 :
                $im = imageCreateFromJpeg($filepath);
            break;
            case 3 :
                $im = imageCreateFromPng($filepath);
            break;
            case 6 :
                $im = imageCreateFromBmp($filepath);
            break;
        }   
        return $im; 
    }
    ?>
    

    【讨论】:

      【解决方案2】:
      //This may not be the answer
      
      //in php we have different functions for different image formats so this may helps you
      imagecreatefromjpeg ( string $filename )
      imagecreatefrompng ( string $filename )
      imagecreatefromgif ( string $filename )
      

      imagecreatefromjpeg DOC

      imagecreatefrompng DOC

      imagecreatefromgif DOC

      【讨论】:

      • 谢谢兄弟,我知道他们我已经使用了 1,如果源是 png 或其他,我真的不知道如何用于检测类型,以便我们可以相应地查询