Adjust Orientation (rotate) of image while upload image from mobile devices like Android/ iPhone using PHP “exif_read_data” function

Php
Sep 13 2017
2396 Views

Adjust Orientation (rotate) of image while upload image from mobile devices like Android/ iPhone using PHP “exif_read_data” function

Introduction

While upload images from mobile devices like Android/ iPhone with mobile camera, images will be shown in wrong orientation.  To avoid this, you need to know image original orientation. “exif_read_data” function return image Orientation information.

 

Definitions

exif_read_data() reads the EXIF headers from an image file. This way you can read meta data generated by digital cameras.

EXIF headers tend to be present in JPEG/TIFF images generated by digital cameras, but unfortunately each digital camera maker has a different idea of how to actually tag their images, so you can't always rely on a specific Exif header being present.

 

Step 1

To know Orientation information use below code.

<?php

$exif = exif_read_data('test.jpg'); //parameter should be image path

if(isset($exif['Orientation'])) {

    if($exif['Orientation'] === 1) print 'rotated clockwise by 0 deg (nothing)';

    if($exif['Orientation'] === 8) print 'rotated clockwise by 90 deg';

    if($exif['Orientation'] === 3) print 'rotated clockwise by 180 deg';

    if($exif['Orientation'] === 6) print 'rotated clockwise by 270 deg';

    if($exif['Orientation'] === 2) print 'vertical flip, rotated clockwise by 0 deg';

    if($exif['Orientation'] === 7) print 'vertical flip, rotated clockwise by 90 deg';

    if($exif['Orientation'] === 4) print 'vertical flip, rotated clockwise by 180 deg';

    if($exif['Orientation'] === 5) print 'vertical flip, rotated clockwise by 270 deg';

}

Print_r($exif);

?>

 

Output

Array

(

    [FileName] => test.jpg

    [FileDateTime] => 1505297066

    [FileSize] => 1379931

    [FileType] => 2

    [MimeType] => image/jpeg

    [SectionsFound] => ANY_TAG, IFD0, THUMBNAIL, EXIF, GPS

    [COMPUTED] => Array

        (

            [html] => width="3264" height="1836"

            [Height] => 1836

            [Width] => 3264

            [IsColor] => 1

            [ByteOrderMotorola] => 0

            [ApertureFNumber] => f/2.0

            [UserComment] =>

            [UserCommentEncoding] => ASCII

            [Thumbnail.FileType] => 2

            [Thumbnail.MimeType] => image/jpeg

            [Thumbnail.Height] => 240

            [Thumbnail.Width] => 320

        )

 

    [ImageWidth] => 3264

    [ImageLength] => 1836

    [ImageDescription] => Jpeg

    [Make] => ASUS

    [Model] => T00J

    [Orientation] => 6

    [XResolution] => 72/1

    [YResolution] => 72/1

    [ResolutionUnit] => 2

    [Software] => Android

    [DateTime] => 2017:09:08 18:50:53

    [YCbCrPositioning] => 1

    [Exif_IFD_Pointer] => 241

    [GPS_IFD_Pointer] => 13932

    [THUMBNAIL] => Array

        (

            [ImageWidth] => 320

            [ImageLength] => 240

            [Compression] => 6

            [Orientation] => 6

            [XResolution] => 72/1

            [YResolution] => 72/1

            [ResolutionUnit] => 2

            [JPEGInterchangeFormat] => 14139

            [JPEGInterchangeFormatLength] => 9447

        )

 

    [ExposureTime] => 703/10000

    [FNumber] => 20/10

    [ExposureProgram] => 2

    [ISOSpeedRatings] => 310

    [ExifVersion] => 0220

    [DateTimeOriginal] => 2017:09:08 18:50:53

    [DateTimeDigitized] => 2017:09:08 18:50:53

    [ComponentsConfiguration] => [1]

 

    [ShutterSpeedValue] => 250926/65536

    [ApertureValue] => 131072/65536

    [BrightnessValue] => 0/0

    [ExposureBiasValue] => 0/100

    [MaxApertureValue] => 20/10

    [SubjectDistance] => 0/1

    [MeteringMode] => 1

    [LightSource] => 4

    [Flash] => 1

    [FocalLength] => 269/100

    [UserComment] => ASCII

    [FlashPixVersion] => 0100

    [ColorSpace] => 1

    [ExifImageWidth] => 3264

    [ExifImageLength] => 1836

    [ExposureMode] => 0

    [WhiteBalance] => 0

    [DigitalZoomRatio] => 100/100

    [SceneCaptureType] => 0

    [GainControl] => 0

    [Contrast] => 0

    [Saturation] => 0

    [Sharpness] => 0

    [MakerNote] => MKNTh&

    [GPSVersion] => [1][1]

    [GPSTimeStamp] => Array

        (

            [0] => 13/1

            [1] => 20/1

            [2] => 51/1

        )

 

    [GPSDateStamp] => 2017:09:08

)


 

 

Step 2

Here I am using Imagick library to rotate image. Follow the below code

<?php

$pic = new Imagick($source);

$exif = exif_read_data($source);

if (isset($exif['Orientation'])) {

                        switch ($exif['Orientation']) {

                            case 3:

                                $pic->rotateImage("#cccccc", 180);

                                break;

                            case 6:

                                $pic->rotateImage("#cccccc", -90);

                                break;

                            case 8:

                                $pic->rotateImage("#cccccc", 90);

                                break;

                        }

 }

$pic->writeImage($destination);                             

$pic->destroy();

?>

 

Leave a Reply