上传后图像方向问题

Problem with image orientation after upload

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

    <?php
    if(isset($_FILES['image'])){
    $errors= array();
    foreach($_FILES['image']['tmp_name'] as $key => $tmp_name ){
        $file_name =$_FILES['image']['name'][$key];
        $file_size =$_FILES['image']['size'][$key];
        $file_tmp =$_FILES['image']['tmp_name'][$key];
        $file_type=$_FILES['image']['type'][$key]; 

            // Remove encoding problem
            $file_name = Normalizer::normalize($file_name); 
            setlocale(LC_ALL,'bs_BA.UTF-8'); 


            // get file extension
            $fileType = strtolower(pathinfo($file_name, PATHINFO_EXTENSION)); 

            $temp = pathinfo($file_name, PATHINFO_EXTENSION);
            $name = str_replace($temp, '', $file_name);


            // get filename without extension
            $fileNewName = pathinfo($name, PATHINFO_FILENAME);
            $watermarkImagePath = 'watermark.png'; 
            $folderPath = "upload/";
            $sourceProperties = getimagesize($file_tmp);
            $imageType = $sourceProperties[2];

            // Resize code
            switch ($imageType) {
            case IMAGETYPE_PNG:
                $imageResourceId = imagecreatefrompng($file_tmp); 
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagepng($targetLayer,$folderPath. $fileNewName. ".jpg");
                break;
            case IMAGETYPE_GIF:
                $imageResourceId = imagecreatefromgif($file_tmp); 
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagegif($targetLayer,$folderPath. $fileNewName. ".jpg");
                break;
            case IMAGETYPE_JPEG:
                $imageResourceId = imagecreatefromjpeg($file_tmp); 
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagejpeg($targetLayer,$folderPath. $fileNewName. ".jpg");
                break;
            default:
                echo "Invalid Image type.";
                exit;
                break;
            }   

                // watermark code
                $NewFile=$fileNewName.'.jpg';
                $targetFilePath = $folderPath . $NewFile; 
                $watermarkImg = imagecreatefrompng($watermarkImagePath); 

                if(preg_match('/[.](jpg)$/i', $NewFile)) {  
                $im = imagecreatefromjpeg($targetFilePath);  
              } else if (preg_match('/[.](jpeg)$/i', $NewFile)) {  
                $im = imagecreatefromjpeg($targetFilePath);  
              } else if (preg_match('/[.](png)$/i', $NewFile)) {  
                $im = imagecreatefrompng($targetFilePath);  
              }  else if (preg_match('/[.](gif)$/i', $NewFile)) {  
                $im = imagecreatefromgif($targetFilePath);  
              }  

                $marge_right = 1; 
                $marge_bottom = 1; 

                $sx = imagesx($watermarkImg); 
                $sy = imagesy($watermarkImg); 

                imagecopy($im, $watermarkImg, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($watermarkImg), imagesy($watermarkImg)); 


                imagejpeg($im, $targetFilePath,70); 
                imagedestroy($im); 


    }
     echo ' Successful upload';
}
function imageResize($imageResourceId,$width,$height) {
if($width > $height){
    $targetWidth=1000;
    $targetHeight=($height/$width)*$targetWidth;
} else {
    $targetHeight=1000;
$targetWidth=($width/$height)*$targetHeight;}
    $targetLayer=imagecreatetruecolor($targetWidth,$targetHeight);
    imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);
    return $targetLayer;
}

?>
<div class="sender">
<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="image[]" multiple/>
    <input type="submit" value="Send"/>
</form></div>

这是由于照片中的可交换信息。 大多数从 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
?>

以上是修复旋转的代码

请参阅此以获取更多信息 https://www.php.net/manual/en/function.exif-read-data.php

这是现在适合我的解决方案。图像方向校正代码在第 50-70 行。可能有更好的解决方案,但这是我唯一能够正常工作的方法:

<?php
if(isset($_FILES['image'])){
    $errors= array();
    foreach($_FILES['image']['tmp_name'] as $key => $tmp_name ){
        $file_name =$_FILES['image']['name'][$key];
        $file_size =$_FILES['image']['size'][$key];
        $file_tmp =$_FILES['image']['tmp_name'][$key];
        $file_type=$_FILES['image']['type'][$key];      

            // Remove encoding problem
            $file_name = Normalizer::normalize($file_name); 
            setlocale(LC_ALL,'bs_BA.UTF-8');        

            // get file extension
            $fileType = strtolower(pathinfo($file_name, PATHINFO_EXTENSION)); 

            $temp = pathinfo($file_name, PATHINFO_EXTENSION);
            $name = str_replace($temp, '', $file_name);

            // get filename without extension
            $fileNewName = pathinfo($name, PATHINFO_FILENAME);
            $watermarkImagePath = 'watermark.png'; 
            $folderPath = "a/";
            $sourceProperties = getimagesize($file_tmp);
            $imageType = $sourceProperties[2];

            // Resize code
            switch ($imageType) {
            case IMAGETYPE_PNG:
                $imageResourceId = imagecreatefrompng($file_tmp); 
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagepng($targetLayer,$folderPath. $fileNewName. ".jpg");
                break;
            case IMAGETYPE_GIF:
                $imageResourceId = imagecreatefromgif($file_tmp); 
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagegif($targetLayer,$folderPath. $fileNewName. ".jpg");
                break;
            case IMAGETYPE_JPEG:
                $imageResourceId = imagecreatefromjpeg($file_tmp); 
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagejpeg($targetLayer,$folderPath. $fileNewName. ".jpg");

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

        // Image Orientation correction                         

        $targetFilePath = $folderPath . $file_name;                 
        $exif = exif_read_data($file_tmp);
        if ($exif['Orientation']==3 OR $exif['Orientation']==6 OR $exif['Orientation']==8) {
            $imageResource = imagecreatefromjpeg($targetFilePath); 
            switch ($exif['Orientation']) { 
            case 3:
            $image = imagerotate($imageResource, 180, 0);
            break;
            case 6:
            $image = imagerotate($imageResource, -90, 0);
            break;
            case 8:
            $image = imagerotate($imageResource, 90, 0);
            break;
        } 
        imagejpeg($image, $targetFilePath);
        imagedestroy($imageResource);
        imagedestroy($image);
        }

        // watermark code

                $watermarkImg = imagecreatefrompng($watermarkImagePath); 

                if(preg_match('/[.](jpg)$/i', $file_name)) {  
                $im = imagecreatefromjpeg($targetFilePath);  
              } else if (preg_match('/[.](jpeg)$/i', $file_name)) {  
                $im = imagecreatefromjpeg($targetFilePath);  
              } else if (preg_match('/[.](png)$/i', $file_name)) {  
                $im = imagecreatefrompng($targetFilePath);  
              }  else if (preg_match('/[.](gif)$/i', $file_name)) {  
                $im = imagecreatefromgif($targetFilePath);  
              }  

                $marge_right = 1; 
                $marge_bottom = 1; 

                $sx = imagesx($watermarkImg); 
                $sy = imagesy($watermarkImg); 

                imagecopy($im, $watermarkImg, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($watermarkImg), imagesy($watermarkImg)); 


                imagejpeg($im, $targetFilePath,70); 
                imagedestroy($im);     

    }
     echo ' Successful upload';
}
function imageResize($imageResourceId,$width,$height) {
if($width > $height){
    $targetWidth=1000;
    $targetHeight=($height/$width)*$targetWidth;
} else {
    $targetHeight=1000;
$targetWidth=($width/$height)*$targetHeight;}
    $targetLayer=imagecreatetruecolor($targetWidth,$targetHeight);
    imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);
    return $targetLayer;
}

?>
<div class="sender">
<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="image[]" multiple/>
    <input type="submit" value="Send"/>
</form></div>