PHP 压缩图片并重命名

PHP Compress Image and rename

我找到了that code 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() but doesn't work 和 compress_image 函数重命名它。

我尝试了 adding/changing 个变量,但没有用 我用原来的名称保存了图像。 有什么想法吗?

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

事实上,如果无法创建资源,imagecreatefromstring() 将 return 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);

}

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

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