Jump to content

image rotation


phppup
 Share

Recommended Posts

I have some photos that were taken using a cellphone in an upright/vertical position.

They were downloaded and placed in a folder to be viewed on a webpage but they displayed horizontally/sideways (with the persons head on the left).

I am assuming that the EXIF information in the image tells it which side is up and what the intended 'top' of the picture should be..

I want to have a PHP function that will rotate the image to its intended display position so that photos of mountain ranges [intentionally holding the smartphone sideways] are horizontal and portraits [where the smartphone in upright] are vertical.

I placed this code in a PHP file but got no change in the image:


function correctImageOrientation($filename) {
  if (function_exists('exif_read_data')) {
    $exif = exif_read_data($filename);
    if($exif && isset($exif['Orientation'])) {
      $orientation = $exif['Orientation'];
      if($orientation != 1){
        $img = imagecreatefromjpeg($filename);
        $deg = 0;
        switch ($orientation) {
          case 3:
            $deg = 180;
            break;
          case 6:
            $deg = 270;
            break;
          case 8:
            $deg = 90;
            break;
        }
        if ($deg) {
          $img = imagerotate($img, $deg, 0);        
        }
        // then rewrite the rotated image back to the disk as $filename 
        imagejpeg($img, $filename, 95);
      } 
    } 
  } 
}

$filename = 'upload/myTest.jpg';

correctImageOrientation($filename);

Am I using the correct approach to get the desired result?

Is the code correct?

 

Link to comment
Share on other sites

Yes, I am going to try that.

If I'm understanding correctly, the script checks the orientation inside the EXIF and then changes the degrees of rotation accordingly.

Does the new image then contain EXIF info that head been adjusted to reflect the new orientation?

 

Link to comment
Share on other sites

29 minutes ago, phppup said:

Does the new image then contain EXIF info that head been adjusted to reflect the new orientation?

I tried your function then pulled the exif data from the rotated image. Virtually all of the mass of data from the original has gone, including the "Orientation". Width and height were updated OK.''

Exif from new image

Array
(
    [FileName] => RDSCN0035.JPG
    [FileDateTime] => 1575484231
    [FileSize] => 3509919
    [FileType] => 2
    [MimeType] => image/jpeg
    [SectionsFound] => COMMENT
    [COMPUTED] => Array
        (
            [html] => width="3672" height="4896"
            [Height] => 4896
            [Width] => 3672
            [IsColor] => 1
        )

    [COMMENT] => Array
        (
            [0] => CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), quality = 95

        )

)

 

Link to comment
Share on other sites

So how come I am getting no result and you have success? What an I missing?

How do I transfer the unaltered pieces of EXIF data?

I've read multiple webpages. Most accept that it's lost in the rotation process, but that seems somewhat of a lazy inevitability.

Link to comment
Share on other sites

9 minutes ago, phppup said:

So how come I am getting no result and you have success? What an I missing?

I'll have to pass on that. Did the output of your original's exif data contain the "Orientation" value?

In my test, the original had an orientation = 8. Here's the before and after

<img src="images/DSCN0035.JPG" width="500"  >
<img src="images/RDSCN0035.JPG" height="500" >      <!-- rotated version -->

Results

image.png.fd41e0475494df4aadd669b5fe003b4e.png

Link to comment
Share on other sites

The test image is on the server in an upload folder which is in the same folder as this script (above).

I confirmed connectivity to the image by printing EXIF data, but the image did not change.

If it were overwritten, then the image file's 'last modified' info would change.

For some reason, nothing is being altered or created from the original file.

Do I need to move or create a line in the script?

Link to comment
Share on other sites

BEFORE

Array
(
    [FileName] => myTest.jpg
    [FileDateTime] => 1574967654
    [FileSize] => 12566
    [FileType] => 2
    [MimeType] => image/jpeg
    [SectionsFound] => COMMENT
    [COMPUTED] => Array
        (
            [html] => width="500" height="244"
            [Height] => 244
            [Width] => 500
            [IsColor] => 1
        )

    [COMMENT] => Array
        (
            [0] => CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 80

        )

)

AFTER

Array
(
    [FileName] => myTest.jpg
    [FileDateTime] => 1574967654
    [FileSize] => 12566
    [FileType] => 2
    [MimeType] => image/jpeg
    [SectionsFound] => COMMENT
    [COMPUTED] => Array
        (
            [html] => width="500" height="244"
            [Height] => 244
            [Width] => 500
            [IsColor] => 1
        )

    [COMMENT] => Array
        (
            [0] => CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 80

        )

)

 

Link to comment
Share on other sites

12 minutes ago, phppup said:

BEFORE

Array
(
    [FileName] => myTest.jpg
    [FileDateTime] => 1574967654
    [FileSize] => 12566
    [FileType] => 2
    [MimeType] => image/jpeg
    [SectionsFound] => COMMENT
    [COMPUTED] => Array
        (
            [html] => width="500" height="244"
            [Height] => 244
            [Width] => 500
            [IsColor] => 1
        )

    [COMMENT] => Array
        (
            [0] => CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 80

        )

)

AFTER

Array
(
    [FileName] => myTest.jpg
    [FileDateTime] => 1574967654
    [FileSize] => 12566
    [FileType] => 2
    [MimeType] => image/jpeg
    [SectionsFound] => COMMENT
    [COMPUTED] => Array
        (
            [html] => width="500" height="244"
            [Height] => 244
            [Width] => 500
            [IsColor] => 1
        )

    [COMMENT] => Array
        (
            [0] => CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 80

        )

)

 

I don't see any "orientation" index in your exif data.

My "BEFORE" exif data looks like this (as you can see, orientation = 8)...

Array
(
    [FileName] => DSCN0035.JPG
    [FileDateTime] => 1528993833
    [FileSize] => 4044525
    [FileType] => 2
    [MimeType] => image/jpeg
    [SectionsFound] => ANY_TAG, IFD0, THUMBNAIL, EXIF, GPS, INTEROP, MAKERNOTE
    [COMPUTED] => Array
        (
            [html] => width="4896" height="3672"
            [Height] => 3672
            [Width] => 4896
            [IsColor] => 1
            [ByteOrderMotorola] => 0
            [ApertureFNumber] => f/5.8
            [UserComment] =>                                                                                                                        
            [UserCommentEncoding] => UNDEFINED
            [Thumbnail.FileType] => 2
            [Thumbnail.MimeType] => image/jpeg
        )

    [ImageDescription] =>           
    [Make] => NIKON
    [Model] => COOLPIX P520
    [Orientation] => 8                                               <<*************** ORIENTATION **************
    [XResolution] => 300/1
    [YResolution] => 300/1
    [ResolutionUnit] => 2
    [Software] => COOLPIX P520   V1.0
    [DateTime] => 2014:06:30 13:33:43
    [YCbCrPositioning] => 2
    [Exif_IFD_Pointer] => 314
    [GPS_IFD_Pointer] => 1002
    [THUMBNAIL] => Array
        (
            [Compression] => 6
            [XResolution] => 300/1
            [YResolution] => 300/1
            [ResolutionUnit] => 2
            [JPEGInterchangeFormat] => 4032
            [JPEGInterchangeFormatLength] => 4658
        )

    [ExposureTime] => 10/6400
    [FNumber] => 58/10
    [ExposureProgram] => 3
    [ISOSpeedRatings] => 80
    [UndefinedTag:0x8830] => 1
    [ExifVersion] => 0230
    [DateTimeOriginal] => 2014:06:30 13:33:43
    [DateTimeDigitized] => 2014:06:30 13:33:43
    [ComponentsConfiguration] => 
    [CompressedBitsPerPixel] => 2/1
    [ExposureBiasValue] => 0/10
    [MaxApertureValue] => 32/10
    [MeteringMode] => 5
    [LightSource] => 0
    [Flash] => 16
    [FocalLength] => 356/10
    [MakerNote] => Nikon
    [UserComment] =>                                                                                                                        
    [FlashPixVersion] => 0100
    [ColorSpace] => 1
    [ExifImageWidth] => 4896
    [ExifImageLength] => 3672
    [InteroperabilityOffset] => 1114
    [FileSource] => 
    [SceneType] => 
    [CustomRendered] => 0
    [ExposureMode] => 0
    [WhiteBalance] => 0
    [DigitalZoomRatio] => 0/100
    [FocalLengthIn35mmFilm] => 200
    [SceneCaptureType] => 0
    [GainControl] => 0
    [Contrast] => 0
    [Saturation] => 0
    [Sharpness] => 0
    [SubjectDistanceRange] => 0
    [GPSVersion] => 
    [InterOperabilityIndex] => R98
    [InterOperabilityVersion] => 0100
)

As for your question about print_r(), see the manual

Link to comment
Share on other sites

38 minutes ago, phppup said:

[COMMENT] => Array ( [0] => CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 80 )

From your "BEFORE" exif data (above) it was created by GD library in PHP. Uploading should just copy the file. Are you doing any PHP processing on the file after uploading, such as resizing?

My image was copied from my camera to my PC.

Link to comment
Share on other sites

1 minute ago, phppup said:

So during the upload process, rotation needs to be handled before resizing?

Yes, or do them both in the same operation.

 

2 minutes ago, phppup said:

No way to retain the EXIF data?

Not in the image. If you store the original image then you can still get it any time you want it.. You would have to amend your rotation function as it currently overwrites the input file.

Link to comment
Share on other sites

Thanks for the lesson.

And it seems that even altering rotation in Photo Viewer (and I'm assuming most 'common' tools) changes the rotation that the user sees, but either erases it or leaves it at 1 (behind the scenes).  Perhaps professional software like Photoshop offers more options.

It is rather interesting that the info is erased, but I digress.  Thanks again for the help.

Link to comment
Share on other sites

I used        print_r($exif, 1)     - -  not sure of the differention of print_r($exif)     - -  to get a handle on things.

Now, for confirmation, I want to print EXIF data at the end of the script to verify that the function was successful and that the EXIF data was changed.  However, even though I have confirmed that the actual image was altered, the EXIF that is printed afterward remains identical to the original.

I have tried to rename it and change the location but the results are the same.  What do I need to do in order to get the revised EXIF data for the same file?

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.