如何在使用php上传之前裁剪图像?

问题描述 投票:-1回答:1

我一直在寻找一种在上载照片之前调整其大小的方法,我在下面找到了代码,它非常符合我的目标,只希望它不要将原始图像与裁剪后的图像一起存储在目录中。我对其进行了一些更改,但没有解决。希望我能有所帮助。

  if(isset($_POST["submit"])) {
      if(is_array($_FILES)) {


    $uploadedFile = $_FILES['file']['tmp_name']; 
    $sourceProperties = getimagesize($uploadedFile);
    $newFileName = time();
    $dirPath = "uploads/";
    $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
    $imageType = $sourceProperties[2];


    switch ($imageType) {


        case IMAGETYPE_PNG:
            $imageSrc = imagecreatefrompng($uploadedFile); 
            $tmp = imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1]);
            imagepng($tmp,$dirPath. $newFileName. "_thump.". $ext);
            break;           

        case IMAGETYPE_JPEG:
            $imageSrc = imagecreatefromjpeg($uploadedFile); 
            $tmp = imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1]);
            imagejpeg($tmp,$dirPath. $newFileName. "_thump.". $ext);
            break;

        case IMAGETYPE_GIF:
            $imageSrc = imagecreatefromgif($uploadedFile); 
            $tmp = imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1]);
            imagegif($tmp,$dirPath. $newFileName. "_thump.". $ext);
            break;

        default:
            echo "Invalid Image type.";
            exit;
            break;
    }


    move_uploaded_file($uploadedFile, $dirPath. $newFileName. ".". $ext);
    echo "Image Resize Successfully.";
    }
  }


   function imageResize($imageSrc,$imageWidth,$imageHeight) {

    $newImageWidth =900;
    $newImageHeight =534;

     $newImageLayer=imagecreatetruecolor($newImageWidth,$newImageHeight);



   imagecopyresampled($newImageLayer,$imageSrc,0,0,0,0,$newImageWidth,$newImageHeight,$imageWidth,$imageHeight);

    return $newImageLayer;
}
?> 
php function file post image-resizing
1个回答
0
投票

如果从代码中删除行move_uploaded_file,它将不会保存原始文件(该文件将保留在temp文件夹中,并最终被系统删除)。您的其余代码应保持原样。

© www.soinside.com 2019 - 2024. All rights reserved.