我正在尋找一種在上傳照片之前調(diào)整照片大小的方法,我找到了下面的代碼,它完全符合我的目標(biāo),只是希望它不會(huì)將原始圖像與裁剪圖像一起存儲(chǔ)在目錄中。我對(duì)它做了一些更改,但沒有成功。我希望我能得到一些幫助。 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上傳前裁剪圖像?
慕無(wú)忌1623718
2022-09-17 15:36:05