0

Im trying to make iphone images rotate when resizing, but i just cant seem to get it working.

The upload and resize part is working fine. Its the rotation part that is giving me trouble.

I hope someone can spot my rookie error! :-)

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;

}

EDIT: I tried the solution posted in answers, but now i get this error: https://gyazo.com/591feaa396e194a69a012715d02a32e7

Thank you so much for your time in advance!

Best regards! :-)

2
  • So which part of the rotation gives you trouble? Does it work at all? Whats wrong with it? :-) Commented Oct 2, 2015 at 8:00
  • Thanks for your answer. I get this error: gyazo.com/591feaa396e194a69a012715d02a32e7 After i updated the code. Commented Oct 2, 2015 at 8:53

1 Answer 1

1

imagerotate() is expecting a image resource, you're providing it a file path string.

move your call to image_fix_orientation() after the imagecreatefromjpeg() function

if($extension=="jpg" || $extension=="jpeg" ) {
  $uploadedfile = $_FILES[$inputImage]['tmp_name'];
  $src = imagecreatefromjpeg($uploadedfile);
  $src = image_fix_orientation($src , $filename);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much, i now get this error: gyazo.com/591feaa396e194a69a012715d02a32e7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.