Support the ongoing development of Laravel.io →
Views Forms
Last updated 2 years ago.
0

This is a real pain, and is caused by the EXIF data that is (sometimes) stored with the jpeg-format in which the iPhone takes pictures.

I wrote a function to take care of it:

/**
 * Orientate an image, based on its exif rotation state
 * 
 * @param  Intervention\Image\Image $image
 * @param  integer $orientation Image exif orientation
 * @return Intervention\Image\Image
 */
function orientate($image, $orientation)
{
    switch ($orientation) {

        // 888888
        // 88    
        // 8888  
        // 88    
        // 88    
        case 1:
            return $image;

        // 888888
        //     88
        //   8888
        //     88
        //     88
        case 2:
            return $image->flip('h');


        //     88
        //     88
        //   8888
        //     88
        // 888888
        case 3:
            return $image->rotate(180);
        
        // 88    
        // 88    
        // 8888  
        // 88
        // 888888
        case 4:
            return $image->rotate(180)->flip('h');

        // 8888888888
        // 88  88    
        // 88        
        case 5:
            return $image->rotate(-90)->flip('h');

        // 88        
        // 88  88    
        // 8888888888
        case 6:
            return $image->rotate(-90);

        //         88
        //     88  88
        // 8888888888
        case 7:
            return $image->rotate(-90)->flip('v');

        // 8888888888
        //     88  88
        //         88
        case 8:
            return $image->rotate(90);

        default:
            return $image;
    }
}

The usage of this would then be something like this, supposing you have an Intervention image named $image:

// Check if the image is a jpg
$isJpg = $image->mime === 'image/jpg' || $image->mime === 'image/jpeg';

// If the image is jpg and has orientation data, make sure we orientate correctly before uploading
if($isJpg && $image->exif('Orientation'))
    $image = orientate($image, $image->exif('Orientation'));

// Horray! Your image should now be correctly rotated!

I hope this helps!

Last updated 2 years ago.
0

@lieberkind : Thanks that worked perfectly for me.

Last updated 2 years ago.
0

Where did you place your function orientate at?

Last updated 2 years ago.
0

Well, you could place it everywhere really. The easiest way would probably be to place it an a new file called app/helpers.php, and then, in your app/start/global.php do:


require app_path().'/helpers.php';

Last updated 2 years ago.
0

Got it to work! Thank you so much for sharing this code... and for helping me understand helper functions. Learning new things all the time!

Last updated 2 years ago.
0

Intervention has a native method for this.

\Intervention\Image\Image::make($path)->orientate();
0

Thanks it's work like a charm.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Failcookie failcookie Joined 20 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.