phppup Posted December 3, 2019 Share Posted December 3, 2019 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? Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/ Share on other sites More sharing options...
chhorn Posted December 4, 2019 Share Posted December 4, 2019 Have you checked every variable if it contains what you expect? Post results, and maybe the image you tested. Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572161 Share on other sites More sharing options...
phppup Posted December 4, 2019 Author Share Posted December 4, 2019 I'm not sure what you mean. I think I need to compare the code with the test image's EXIF info, but not sure how to get that either. Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572169 Share on other sites More sharing options...
Barand Posted December 4, 2019 Share Posted December 4, 2019 A good start would be echo '<pre>', print_r($exif, 1), '</pre>'; after you get the exif data. Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572173 Share on other sites More sharing options...
phppup Posted December 4, 2019 Author Share Posted December 4, 2019 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? Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572174 Share on other sites More sharing options...
Barand Posted December 4, 2019 Share Posted December 4, 2019 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 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572176 Share on other sites More sharing options...
phppup Posted December 4, 2019 Author Share Posted December 4, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572177 Share on other sites More sharing options...
Barand Posted December 4, 2019 Share Posted December 4, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572179 Share on other sites More sharing options...
phppup Posted December 4, 2019 Author Share Posted December 4, 2019 (edited) I'm going to have to double check it. My image was not being rotated and overwritten. Nor was a new file being created. Edited December 4, 2019 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572185 Share on other sites More sharing options...
Barand Posted December 4, 2019 Share Posted December 4, 2019 (edited) For the function to create a new file, the original image must have an orientation value and that value must be something other than 1. If it is 3, 6, or 8 it is rotated. Edited December 4, 2019 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572187 Share on other sites More sharing options...
phppup Posted December 4, 2019 Author Share Posted December 4, 2019 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? Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572189 Share on other sites More sharing options...
Barand Posted December 4, 2019 Share Posted December 4, 2019 Post the exif data Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572190 Share on other sites More sharing options...
phppup Posted December 4, 2019 Author Share Posted December 4, 2019 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 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572196 Share on other sites More sharing options...
phppup Posted December 4, 2019 Author Share Posted December 4, 2019 What is the difference in result between echo '<pre>', print_r($exif, 1), '</pre>'; and echo '<pre>', print_r($exif), '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572197 Share on other sites More sharing options...
Barand Posted December 4, 2019 Share Posted December 4, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572199 Share on other sites More sharing options...
phppup Posted December 4, 2019 Author Share Posted December 4, 2019 I suppose it is VERY DIFFICULT to alter the orientation if none is detected. Do smartphones use a different method of orientation? Could uploading an image remove the data? Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572200 Share on other sites More sharing options...
Barand Posted December 4, 2019 Share Posted December 4, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572201 Share on other sites More sharing options...
phppup Posted December 4, 2019 Author Share Posted December 4, 2019 Yes, it was resized. Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572202 Share on other sites More sharing options...
Barand Posted December 4, 2019 Share Posted December 4, 2019 Then you destroyed the exif info. Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572203 Share on other sites More sharing options...
phppup Posted December 4, 2019 Author Share Posted December 4, 2019 Does resizing eradicate other data (by default). Is there a way to retain this data? Is the solution to simply ROTATE before resizing (during the upload process)? Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572204 Share on other sites More sharing options...
Barand Posted December 4, 2019 Share Posted December 4, 2019 When you resize, or rotate, you are creating a new copied image using the GD library. This copy loses the exif data that the original contains. Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572205 Share on other sites More sharing options...
phppup Posted December 4, 2019 Author Share Posted December 4, 2019 So during the upload process, rotation needs to be handled before resizing? Or will the resizing be adversely effected then? No way to retain the EXIF data? Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572206 Share on other sites More sharing options...
Barand Posted December 4, 2019 Share Posted December 4, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572208 Share on other sites More sharing options...
phppup Posted December 4, 2019 Author Share Posted December 4, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572209 Share on other sites More sharing options...
phppup Posted December 6, 2019 Author Share Posted December 6, 2019 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? Quote Link to comment https://forums.phpfreaks.com/topic/309622-image-rotation/#findComment-1572319 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.