phppup 0 Posted Tuesday at 10:30 PM 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 Share this post Link to post Share on other sites
chhorn 7 Posted Wednesday at 09:24 AM Have you checked every variable if it contains what you expect? Post results, and maybe the image you tested. Quote Share this post Link to post Share on other sites
phppup 0 Posted Wednesday at 04:43 PM 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 Share this post Link to post Share on other sites
Barand 1,389 Posted Wednesday at 05:37 PM A good start would be echo '<pre>', print_r($exif, 1), '</pre>'; after you get the exif data. Quote Share this post Link to post Share on other sites
phppup 0 Posted Wednesday at 06:09 PM 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 Share this post Link to post Share on other sites
Barand 1,389 Posted Wednesday at 06:42 PM 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 Share this post Link to post Share on other sites
phppup 0 Posted Wednesday at 06:54 PM 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 Share this post Link to post Share on other sites
Barand 1,389 Posted Wednesday at 07:09 PM 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 Share this post Link to post Share on other sites
phppup 0 Posted Wednesday at 08:11 PM (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 Wednesday at 08:12 PM by phppup Quote Share this post Link to post Share on other sites
Barand 1,389 Posted Wednesday at 08:24 PM (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 Wednesday at 08:25 PM by Barand Quote Share this post Link to post Share on other sites
phppup 0 Posted Wednesday at 08:50 PM 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 Share this post Link to post Share on other sites
Barand 1,389 Posted Wednesday at 08:51 PM Post the exif data Quote Share this post Link to post Share on other sites
phppup 0 Posted Wednesday at 09:58 PM 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 Share this post Link to post Share on other sites
phppup 0 Posted Wednesday at 10:04 PM What is the difference in result between echo '<pre>', print_r($exif, 1), '</pre>'; and echo '<pre>', print_r($exif), '</pre>'; Quote Share this post Link to post Share on other sites
Barand 1,389 Posted Wednesday at 10:22 PM 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 Share this post Link to post Share on other sites
phppup 0 Posted Wednesday at 10:33 PM 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 Share this post Link to post Share on other sites
Barand 1,389 Posted Wednesday at 10:41 PM 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 Share this post Link to post Share on other sites
phppup 0 Posted Wednesday at 10:45 PM Yes, it was resized. Quote Share this post Link to post Share on other sites
Barand 1,389 Posted Wednesday at 10:47 PM Then you destroyed the exif info. Quote Share this post Link to post Share on other sites
phppup 0 Posted Wednesday at 10:49 PM 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 Share this post Link to post Share on other sites
Barand 1,389 Posted Wednesday at 10:53 PM 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 Share this post Link to post Share on other sites
phppup 0 Posted Wednesday at 11:05 PM 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 Share this post Link to post Share on other sites
Barand 1,389 Posted Wednesday at 11:13 PM 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 Share this post Link to post Share on other sites
phppup 0 Posted Wednesday at 11:21 PM 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 Share this post Link to post Share on other sites