PHP - 如何在上传时重命名图像文件

PHP - How to Rename an Image File on Upload

这是我正在使用的一个完整脚本,它根据我的原始上传创建了 3 个不同的重新调整大小的图像。现在它重命名了 3 个输出文件的大小加上原始图像文件名。我想重命名图像的大小,但将其更改为一个不同的名称,该名称将是名为的字符串中的一个值。

$ListingId=$_GET['ListingId']; (我正在从 url 中获取值)

这是我的索引页

    <?php
    include( 'function.php');
    // settings
    $max_file_size = 1024*200; // 200kb
    $valid_exts = array('jpeg', 'jpg', 'png', 'gif');
    // thumbnail sizes
    $sizes = array(100 => 100, 150 => 150, 250 => 250);

    if ($_SERVER['REQUEST_METHOD'] == 'POST' AND isset($_FILES['image'])) {
      if( $_FILES['image']['size'] < $max_file_size ){
        // get file extension
        $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
        if (in_array($ext, $valid_exts)) {
          /* resize image */
          foreach ($sizes as $w => $h) {
            $files[] = resize($w, $h);
          }

        } else {
          $msg = 'Unsupported file';
        }
      } else{
        $msg = 'Please upload image smaller than 200KB';
      }
    }
    ?>
    <html>
    <head>
      <title>Image resize while uploadin</title>
    <head>
    <body>
      <!-- file uploading form -->
      <form action="" method="post" enctype="multipart/form-data">
        <label>
          <span>Choose image</span>
          <input type="file" name="image" accept="image/*" />
        </label>
        <input type="submit" value="Upload" />
      </form>
    </body>
    </html>

这是function.php代码

    /**
     * Image resize
     * @param int $width
     * @param int $height
     */
    function resize($width, $height){
      /* Get original image x y*/
      list($w, $h) = getimagesize($_FILES['image']['tmp_name']);
      /* calculate new image size with ratio */
      $ratio = max($width/$w, $height/$h);
      $h = ceil($height / $ratio);
      $x = ($w - $width / $ratio) / 2;
      $w = ceil($width / $ratio);
      /* new file name */
      $path = 'uploads/'.$width.'x'.$height.'_'.$_FILES['image']['name'];
      /* read binary data from image file */
      $imgString = file_get_contents($_FILES['image']['tmp_name']);
      /* create image from string */
      $image = imagecreatefromstring($imgString);
      $tmp = imagecreatetruecolor($width, $height);
      imagecopyresampled($tmp, $image,
        0, 0,
        $x, 0,
        $width, $height,
        $w, $h);
      /* Save image */
      switch ($_FILES['image']['type']) {
        case 'image/jpeg':
          imagejpeg($tmp, $path, 100);
          break;
        case 'image/png':
          imagepng($tmp, $path, 0);
          break;
        case 'image/gif':
          imagegif($tmp, $path);
          break;
        default:
          exit;
          break;
      }
      return $path;
      /* cleanup memory */
      imagedestroy($image);
      imagedestroy($tmp);
     }

非常感谢任何帮助!感谢代码 Resalat Haque 的原作者。

新文件名在resize()函数中设置为宽高和上传的文件名

/* new file name */
$path = 'uploads/'.$width.'x'.$height.'_'.$_FILES['image']['name'];

如果要重命名,可以将新名称传递给 resize() 函数并将函数更改为

function resize($width, $height, $newFileName){
  /* Get original image x y*/
  list($w, $h) = getimagesize($_FILES['image']['tmp_name']);
  /* calculate new image size with ratio */
  $ratio = max($width/$w, $height/$h);
  $h = ceil($height / $ratio);
  $x = ($w - $width / $ratio) / 2;
  $w = ceil($width / $ratio);
  /* new file name */
  $path = 'uploads/'.$width.'x'.$height.'_'.$newFileName;
  /* read binary data from image file */
  $imgString = file_get_contents($_FILES['image']['tmp_name']);
  /* create image from string */
  $image = imagecreatefromstring($imgString);
  $tmp = imagecreatetruecolor($width, $height);
  imagecopyresampled($tmp, $image,
  0, 0,
  $x, 0,
  $width, $height,
  $w, $h);
  /* Save image */
  switch ($_FILES['image']['type']) {
    case 'image/jpeg':
      imagejpeg($tmp, $path.'.jpg', 100);
      break;
    case 'image/png':
      imagepng($tmp, $path.'.png', 0);
      break;
    case 'image/gif':
      imagegif($tmp, $path.'.gif');
      break;
    default:
      exit;
      break;
  }
  return $path;
  /* cleanup memory */
  imagedestroy($image);
  imagedestroy($tmp);
}

将函数调用更改为:

$newFileName = 'newName'; // set to your desired value
/* resize image */
foreach ($sizes as $w => $h) {
   $files[] = resize($w, $h, $newFileName);
}

编辑:还需要将文件类型扩展名添加到开关中的 $path。