folkloreinc/laravel-image-legacy

Images uploaded with an iphone can be upside down.

Closed this issue · 2 comments

I have found that images uploaded from an iphone or just taken from an iphone would be in an incorrect rotation. This is because of the iphone rotating its photos by modifying the exif information and not actually the photo.

So the question would be how to turn or rotate the image to the correct rotation by reading the exif information?

Thanks by the way

I will add it to built-in filters but for now, this library is based on Imagine and they do have an Autorotate filter that you can use this way:

    use Imagine\Filter\Basic\Autorotate;

    $image = Image::open('/path/to/uploaded/mobile/image.png');
    $autorotate = new Autorotate();
    $autorotate->apply($image)->save('/path/to/rotated/image.png');

or you could define a custom filter for it:

    use Imagine\Filter\Basic\Autorotate;

    Image::filter('autorotate', function($image)
    {
        $autorotate = new Autorotate();
        return $autorotate->apply($image);
    });

and then use it this way:

<img src="<?=Image::url('/path/to/image.jpg', array('autorotate'))?>" />

Thanks for the quick reply,

I couldnt make it work, metadata always returned null and autorate filter always returned the same image. I ended up using this code

$exif = exif_read_data($path, 'IFD0');
        $rotation   = 0;
        if(!empty($exif['Orientation'])) {
            switch($exif['Orientation']) {
                case 8:
                    $rotation = 90;
                    break;
                case 3:
                    $rotation = 180;
                    break;
                case 6:
                    $rotation = -90;
                    break;
            }
        }

// Crop image and watermark it
$img = Image::make($path, ['width' => 800, 'height' => 540, 'crop' => true, 'rotate' => $rotation]);