上传后图像方向问题

我有用于上传、调整大小和水印照片的 PHP 代码。一切正常,除非照片是从某些移动设备上传的,然后照片的方向有问题(照片旋转了 90°)我找到了一些解决方案(How to check/fix image rotation before使用 PHP 上传图片)但我无法在我的代码中实现它,因为我对 php 了解不够。我相信我需要在设置水印之前进行方向校正。这是没有方向校正的代码:



慕雪6442864
浏览 185回答 2
2回答

明月笑刀无情

这是由于照片中的可交换信息。大多数从 iphone 或 DSLR 拍摄的照片都有 exif 数据。   <?php    $image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name']));    $exif = exif_read_data($_FILES['image_upload']['tmp_name']);    if(!empty($exif['Orientation'])) {    switch($exif['Orientation']) {        case 8:            $image = imagerotate($image,90,0);            break;        case 3:            $image = imagerotate($image,180,0);            break;        case 6:            $image = imagerotate($image,-90,0);            break;    }}// $image now contains a resource with the image oriented correctly?>上面是修复旋转的代码

慕尼黑的夜晚无繁华

这是现在对我有用的解决方案。图像方向校正代码在第 50-70 行。可能有更好的解决方案,但这是我唯一能够正常工作的事情:<?phpif(isset($_FILES['image'])){&nbsp; &nbsp; $errors= array();&nbsp; &nbsp; foreach($_FILES['image']['tmp_name'] as $key => $tmp_name ){&nbsp; &nbsp; &nbsp; &nbsp; $file_name =$_FILES['image']['name'][$key];&nbsp; &nbsp; &nbsp; &nbsp; $file_size =$_FILES['image']['size'][$key];&nbsp; &nbsp; &nbsp; &nbsp; $file_tmp =$_FILES['image']['tmp_name'][$key];&nbsp; &nbsp; &nbsp; &nbsp; $file_type=$_FILES['image']['type'][$key];&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Remove encoding problem&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $file_name = Normalizer::normalize($file_name);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setlocale(LC_ALL,'bs_BA.UTF-8');&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get file extension&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fileType = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $temp = pathinfo($file_name, PATHINFO_EXTENSION);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $name = str_replace($temp, '', $file_name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get filename without extension&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fileNewName = pathinfo($name, PATHINFO_FILENAME);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $watermarkImagePath = 'watermark.png';&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $folderPath = "a/";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sourceProperties = getimagesize($file_tmp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $imageType = $sourceProperties[2];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Resize code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch ($imageType) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case IMAGETYPE_PNG:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $imageResourceId = imagecreatefrompng($file_tmp);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imagepng($targetLayer,$folderPath. $fileNewName. ".jpg");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case IMAGETYPE_GIF:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $imageResourceId = imagecreatefromgif($file_tmp);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imagegif($targetLayer,$folderPath. $fileNewName. ".jpg");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case IMAGETYPE_JPEG:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $imageResourceId = imagecreatefromjpeg($file_tmp);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imagejpeg($targetLayer,$folderPath. $fileNewName. ".jpg");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "Invalid Image type.";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Image Orientation correction&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $targetFilePath = $folderPath . $file_name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $exif = exif_read_data($file_tmp);&nbsp; &nbsp; &nbsp; &nbsp; if ($exif['Orientation']==3 OR $exif['Orientation']==6 OR $exif['Orientation']==8) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $imageResource = imagecreatefromjpeg($targetFilePath);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch ($exif['Orientation']) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image = imagerotate($imageResource, 180, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 6:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image = imagerotate($imageResource, -90, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 8:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image = imagerotate($imageResource, 90, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; imagejpeg($image, $targetFilePath);&nbsp; &nbsp; &nbsp; &nbsp; imagedestroy($imageResource);&nbsp; &nbsp; &nbsp; &nbsp; imagedestroy($image);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // watermark code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $watermarkImg = imagecreatefrompng($watermarkImagePath);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(preg_match('/[.](jpg)$/i', $file_name)) {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $im = imagecreatefromjpeg($targetFilePath);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (preg_match('/[.](jpeg)$/i', $file_name)) {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $im = imagecreatefromjpeg($targetFilePath);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (preg_match('/[.](png)$/i', $file_name)) {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $im = imagecreatefrompng($targetFilePath);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; else if (preg_match('/[.](gif)$/i', $file_name)) {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $im = imagecreatefromgif($targetFilePath);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $marge_right = 1;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $marge_bottom = 1;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sx = imagesx($watermarkImg);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sy = imagesy($watermarkImg);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imagecopy($im, $watermarkImg, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($watermarkImg), imagesy($watermarkImg));&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imagejpeg($im, $targetFilePath,70);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imagedestroy($im);&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;echo ' Successful upload';}function imageResize($imageResourceId,$width,$height) {if($width > $height){&nbsp; &nbsp; $targetWidth=1000;&nbsp; &nbsp; $targetHeight=($height/$width)*$targetWidth;} else {&nbsp; &nbsp; $targetHeight=1000;$targetWidth=($width/$height)*$targetHeight;}&nbsp; &nbsp; $targetLayer=imagecreatetruecolor($targetWidth,$targetHeight);&nbsp; &nbsp; imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);&nbsp; &nbsp; return $targetLayer;}?><div class="sender"><form action="" method="POST" enctype="multipart/form-data">&nbsp; &nbsp; <input type="file" name="image[]" multiple/>&nbsp; &nbsp; <input type="submit" value="Send"/></form></div>
打开App,查看更多内容
随时随地看视频慕课网APP