From a56961430b1fb103115cd82c846a73a81e157a45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Villaf=C3=A1=C3=B1ez?= Date: Thu, 19 Jan 2023 11:44:50 +0100 Subject: [PATCH] Workaround to fix the orientation of some jpg images The orientation of the image is read from the exif data of the image. The data was being read correctly in some cases, but not in all of them. The reason of the failure is still unclear because reading the same data from the same file using the same exif_read_data function works differently depending on the stream being used. --- lib/private/legacy/image.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/private/legacy/image.php b/lib/private/legacy/image.php index 9dcda0391ff2..e461cf972265 100644 --- a/lib/private/legacy/image.php +++ b/lib/private/legacy/image.php @@ -494,7 +494,19 @@ public function load($imageRef) { public function loadFromFileHandle($handle) { $contents = \stream_get_contents($handle); if ($this->loadFromData($contents)) { - $this->loadExifData($handle); + // The loadExifData function has problems using the $handle resource + // so the exif data isn't loaded with some files (checked with jpg). + // Note that this error doesn't happen with all the (jpg) files, but + // some of them. + // As workaround, we write the contents in a temporary stream + // and load the exif data from there + $ff = \fopen('php://temp', 'w+'); + \fwrite($ff, $contents); + // no need to seek to the initial position because the underlying + // exif_read_data is expected to handle it on its own. It works + // without seeking + $this->loadExifData($ff); + \fclose($ff); return $this->resource; } return false;