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!
Where did you place your function orientate at?
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:
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!
Intervention has a native method for this.
\Intervention\Image\Image::make($path)->orientate();
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community