亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

上傳后圖像方向問題

上傳后圖像方向問題

PHP
慕雪6442864 2023-03-26 16:00:31
我有用于上傳、調整大小和水印照片的 PHP 代碼。一切正常,除非照片是從某些移動設備上傳的,然后照片的方向有問題(照片旋轉了 90°)我找到了一些解決方案(How to check/fix image rotation before使用 PHP 上傳圖片)但我無法在我的代碼中實現它,因為我對 php 了解不夠。我相信我需要在設置水印之前進行方向校正。這是沒有方向校正的代碼:
查看完整描述

2 回答

?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

這是由于照片中的可交換信息。大多數從 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

?>

上面是修復旋轉的代碼


查看完整回答
反對 回復 2023-03-26
?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

這是現在對我有用的解決方案。圖像方向校正代碼在第 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>




查看完整回答
反對 回復 2023-03-26
  • 2 回答
  • 0 關注
  • 225 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號