PHP压缩映像并重命名

时间:2018-12-13 02:18:47

标签: php gd image-compression

我发现了that代码here

    <?php
if(isset($_POST['upload'])){

  // Getting file name
  $filename = $_FILES['imagefile']['name'];

  // Valid extension
  $valid_ext = array('png','jpeg','jpg');

  // Location
  $location = "images/".$filename;

  // file extension
  $file_extension = pathinfo($location, PATHINFO_EXTENSION);
  $file_extension = strtolower($file_extension);

  // Check extension
  if(in_array($file_extension,$valid_ext)){

    // Compress Image
    compressImage($_FILES['imagefile']['tmp_name'],$location,60);

  }else{
    echo "Invalid file type.";
  }
}

// Compress image
function compressImage($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);

}

?>

那很好,但是我无法弄清楚保存时如何重命名图像文件。 我知道如何使用move_uploaded_file()重命名,但是如何使用compress_image函数重命名doesn't work

我尝试添加/更改变量,但没有成功,因此我保存了原始名称的图像。 有什么想法吗?

2 个答案:

答案 0 :(得分:0)

如果使用imagecreatefromstring()函数,则不需要检查MIME类型。

实际上imagecreatefromstring()如果无法创建资源,则将返回false,这意味着该字符串不是有效的图像。

因此,您可以将其用作附加且更可靠的测试,以确保您拥有有效的图像。

我遍历了您的代码,更改了一些内容,并在此过程中进行了注释。

if(isset($_POST['upload'])){

  //Getting file name
  $filename = $_FILES['imagefile']['name'];

  //Valid extension
  $valid_ext = array('png','jpeg','jpg');

  //Location
  $path = 'images/'; //<--Just the path to the directory.
  $imageName = 'myNewImageName.jpg'; //<---Rename your image. 

  //File extension
  $file_extension = strtolower(pathinfo($location, PATHINFO_EXTENSION));

  //Check extension
  if(in_array($file_extension, $valid_ext)){

    //Compress Image    
    compressImage($filename, $path, $imageName, 60);

  }else{
    echo "Invalid file type.";
  }
}

//Compress image
function compressImage($file, $path, $imageName, $quality) {

  //Check to see if the directory exist. If not create it with write permissions.
  if(!file_exists($path)){

    mkdir($path, 0777);

  }

  //Check to see if the directory is writable. If not change permissions.
  if(!is_writable($path)){

  chmod($path, 0777);  

  }

  $resource = imagecreatefromstring(file_get_contents($file)); //Creates an image resource and it does not matter what MIME type.
  imagejpeg($resource, $path . $imageName, $quality); //Will save image to new path and filename.
  imagedestroy($resource); //<--Don't forget to free your resources.

  //Change your permissions back to what they need to be.
  chmod($path, 0755);
  chmod($destination, 0644);

}

答案 1 :(得分:0)

如果有人带着同样的问题来到这里,我就找到了解决问题的方法:

index.php

    <!doctype html>
<html>
    <head>
        <title>How to Compress Image and Rename while Uploading with PHP</title>
    </head>
    <body>
        <?php
        if(isset($_POST['upload'])){

            // Getting file name
            $filename = $_FILES['imagefile']['name'];

            // Valid extension
            $valid_ext = array('png','jpeg','jpg');

            // Location
            $location = "images/".$filename;

            $imageName = "myNewImageName"; // New image name. 

            // file extension
            $file_extension = pathinfo($location, PATHINFO_EXTENSION);
            $file_extension = strtolower($file_extension);

            $path = "../withdrawals_receipt_uploads/".$imageName.'.'.$file_extension;

            // Check extension
            if(in_array($file_extension,$valid_ext)){  

                // Compress Image
                compressImage($_FILES['imagefile']['tmp_name'], $path, 60);

            }else{
                echo "Invalid file type.";
            }
        }

        // Compress image
        function compressImage($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);

        }

        ?>

        <!-- Upload form -->
        <form method='post' action='' enctype='multipart/form-data'>
            <input type='file' name='imagefile' >
            <input type='submit' value='Upload' name='upload'>    
        </form>
    </body>
</html>

感谢@Joseph_J