PHP上传时的图像方向

时间:2015-10-02 07:59:35

标签: php image-uploading image-rotation

我试图在调整大小时让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

非常感谢您提前的时间!

祝你好运! :-)

1 个答案:

答案 0 :(得分:1)

imagerotate()期待图片资源,您可以为其提供文件路径字符串。

image_fix_orientation()功能

之后将您的来电移至imagecreatefromjpeg()
if($extension=="jpg" || $extension=="jpeg" ) {
  $uploadedfile = $_FILES[$inputImage]['tmp_name'];
  $src = imagecreatefromjpeg($uploadedfile);
  $src = image_fix_orientation($src , $filename);
}