Home » Php » image rotate not working PHP

image rotate not working PHP

Posted by: admin October 26, 2017 Leave a comment

Questions:

This is giving me headaches :/ I can’t seem to figure out why the image is not rotating. the code inside the function is run, so I don’t understand why the image just does not rotate. Here is the function

function image_fix_orientation($image) {
    $exif = exif_read_data($image);
    $orientation = $exif['Orientation'];
    print_r($exif);
    if (!empty($orientation)) {
        echo 'has orientation';
        //$original_photo = imagecreatefromjpeg($image);
        switch ($orientation) {
            case 3:
                echo 'run 3 ';
                $original_photo = imagecreatefromjpeg($image);
                $new_photo = imagerotate ($original_photo , 180 , 0 );
                break;

            case 6:
                echo ' run 6';
                $original_photo = imagecreatefromjpeg($image);
                $new_photo = imagerotate ($original_photo , -90 , 0 );
                break;

            case 8:
                echo ' run 8';
                $original_photo = imagecreatefromjpeg($image);
                $new_photo = imagerotate ($original_photo , 90 , 0 );
                break;
        }

        return $new_photo;

    }
}

$image = 'test/IMG_4386.JPG';
$new = 'test/IMG_4386_NEW.JPG';
$new_photo = image_fix_orientation($image); 
imagejpeg($new_photo, $new);

echo '<br>';
echo '<img src="'.$new.'" width="800" height="800" />';

Thank you

EDIT:Replace the code that works. This one will create a new image that is rotated correctly. Tho I don’t want to create an image. I actually need to edit the image before it is uploaded to the server and I have no clue how that can be done. Basically I was trying to make this function works because I need to grab the image from $_FILES rotate it if needed and then pass it to the class that will handle the image processing. I do not have access to the class. Is this even possible? rotate the image before it is passed to the class that will handle the image?

Answers:

May be try this.

<?php
    function image_fix_orientation($image) {
        $exif = exif_read_data($image);
        $orientation = $exif['Orientation'];
        print_r($exif);
        $original_photo = null;
        if (!empty($orientation)) {


            switch ($orientation) {
                case 3:
                    $original_photo = imagecreatefromjpeg($image);
                    return imagerotate ($original_photo , 180 , 0 );

                case 1:
                    $original_photo = imagecreatefromjpeg($image);
                    return imagerotate ($original_photo , -90 , 0 );

                case 8:
                    $original_photo = imagecreatefromjpeg($image);
                    return imagerotate ($original_photo , 90 , 0 );
            }
        }
    }

    $rotatedImage = image_fix_orientation("/path/to/image.jpeg");
    // Set the content type header - in this case image/jpeg
    header('Content-Type: image/jpeg');

    // Output the image
    imagejpeg($rotatedImage);
   ?>

Referred from http://exceptionshub.com/uploads/i11aA.D.e43/201710/ntek01h2ipm.php