上传时的图像方向


PHP Image orientation on upload

我试着让iphone图片在调整大小时旋转,但我似乎不能让它工作。

上传和调整大小部分工作正常。旋转部分给我带来了麻烦。

我希望有人能发现我的新手错误!: -)
function image_fix_orientation($src, $filename) {
    $exif = exif_read_data($filename, 0, true);
    if (!empty($exif['Orientation'])) {
        $ort = $exif['IFD0']['Orientation'];
        switch ($ort) {
            case 3:
                $src = imagerotate($src, 180, 0);
                break;
            case 6:
                $src = imagerotate($src, -90, 0);
                break;
            case 8:
                $src = imagerotate($src, 90, 0);
                break;
        }
    }
    return $src;
}
function uploadImage($inputImage){
    $inputImage = $inputImage; 
    $uniqueId = uniqid();
    $errors=0;
    if ( isset($_POST['submitNewProduct']) || isset($_POST['productImageEdit']) || isset($_POST['productImageSecondEdit']) || isset($_POST['productImageThirdEdit']) ) {
        ini_set('memory_limit', '-1');
        $image =$_FILES[$inputImage]["name"];
        $uploadedfile = $_FILES[$inputImage]['tmp_name'];
        $size=filesize($_FILES[$inputImage]['tmp_name']);
        if(isset($_POST['productImageValue'])) {
            $productInputValue = $_POST['productImageValue'];
        } else {
            $productInputValue = 0;
        }
        if(isset($_POST['productImageSecondValue'])) {
            $productInputValueSecond = $_POST['productImageValueSecond'];
        } else {
            $productInputValueSecond = 0;
        }
        if(isset($_POST['productImageThirdValue'])) {
            $productInputValueThird = $_POST['productImageValueThird'];
        } else {
            $productInputValueThird = 0;
        }

        if ($productInputValue < 18388608 || $productInputValueSecond < 18388608 || $productInputValueThird < 18388608)  {
            if ($size < 18388608) {
                if (!empty($image)) {
                    $filename = stripslashes($_FILES[$inputImage]['name']);
                    $extension = getExtension($filename);
                    $extension = strtolower($extension);
                    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
                        echo "<h3>Forkert filformat, kun JPG, PNG og GIF er tilladt.</h3>";
                        $errors=1;
                    } 
                    else {
                        if($extension=="jpg" || $extension=="jpeg" ) {
                          $uploadedfile = $_FILES[$inputImage]['tmp_name'];
                          $src = imagecreatefromjpeg($uploadedfile);
                          $src = image_fix_orientation($src, $filename);
                        }
                        else if($extension=="png") {
                            $uploadedfile = $_FILES[$inputImage]['tmp_name'];
                            $src = imagecreatefrompng($uploadedfile);
                        } 
                        else {
                            $src = imagecreatefromgif($uploadedfile);
                        }
                        list($width,$height)=getimagesize($uploadedfile);
                        if($width <= 800) {
                            $newwidth= $width;
                        } else {
                            $newwidth= 800;
                        }
                        if(!$errors) {
                            $newheight=($height/$width)*$newwidth;
                            $tmp=imagecreatetruecolor($newwidth,$newheight);
                            imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, imagecolorallocate($tmp, 255, 255, 255));
                            imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
                            $filename = "uploads/". $uniqueId . "-" . $_FILES[$inputImage]['name'];
                            imagejpeg($tmp,$filename,100);
                            imagedestroy($src);
                            imagedestroy($tmp);
                        } 
                        else {
                            echo "<h3>Der skete en fejl, prøv venligst igen</h3>";
                        }
                    }
                }
            } else {
                echo "<h3>Billedet: " . $_FILES[$inputImage]["name"] . " fylder for meget.</h3>";
            }
        } else {
            echo "<h3>Et af billederne fylder for meget.</h3>";
        }
    }
        $productImage['name'] = $uniqueId . "-" . basename($_FILES[$inputImage]["name"]);
        $productImage['ort'] = $convertToInt;
        return $productImage;
}

编辑:我尝试了解决方案张贴在答案,但现在我得到这个错误:https://gyazo.com/591feaa396e194a69a012715d02a32e7

提前感谢您的宝贵时间!

致以最亲切的问候!: -)

imagerotate()正在等待一个图像资源,您正在为它提供一个文件路径字符串。

将呼叫移到imagecreatefromjpeg()函数之后的image_fix_orientation()

if($extension=="jpg" || $extension=="jpeg" ) {
  $uploadedfile = $_FILES[$inputImage]['tmp_name'];
  $src = imagecreatefromjpeg($uploadedfile);
  $src = image_fix_orientation($src , $filename);
}