iPhone Image Manipulation Intervention Laravel 5.4 Flip it

iPhone Image Manipulation Intervention Laravel 5.4

I set up a form for uploading files and it works great with everything. I am having issues uploading portrait images and having them stay in their original way without being flipped. I use the Intervention package right now, but I don’t see any out-of-the-box functionality with that.

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: write it in your helper or make function

/**
 * 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) {
 
 case 1:
 return $image;

case 2:
 return $image->flip('h');

case 3:
 return $image->rotate(180);

case 4:
 return $image->rotate(180)->flip('h');
 
 case 5:
 return $image->rotate(-90)->flip('h');

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

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:

$img = Image::make($file->getRealPath());
if($img->exif('Orientation')) {  
       $img = orientate($img, $img->exif('Orientation')); 
} 
$file_name = "image.jpg"; 
$path = public_path("storage/profile/$file_name");  
$img->resize(145,145)->save($path2);

 

I hope this helps!

Leave a Reply

Your email address will not be published. Required fields are marked *