fix for broken exif reading

Signed-off-by: Andy Miller <rhuk@mac.com>
This commit is contained in:
Andy Miller
2020-07-21 14:57:19 -06:00
parent 046c5f69ac
commit 6879d8b6ce

View File

@@ -141,4 +141,38 @@ class ImageFile extends Image
$this->hash = sha1(serialize($datas));
}
/**
* Read exif rotation from file and apply it.
*/
public function fixOrientation()
{
if (!in_array(exif_imagetype($this->source->getInfos()), array(
IMAGETYPE_JPEG,
IMAGETYPE_TIFF_II,
IMAGETYPE_TIFF_MM,
))) {
return $this;
}
if (!extension_loaded('exif')) {
throw new \RuntimeException('You need to EXIF PHP Extension to use this function');
}
// resolve any streams
$filepath = Grav::instance()['locator']->findResource($this->source->getInfos());
try {
$exif = exif_read_data($filepath);
} catch (\Exception $e) {
Grav::instance()['log']->error($filepath . " - " . $e->getMessage());
return $this;
}
if ($exif === false || !array_key_exists('Orientation', $exif)) {
return $this;
}
return $this->applyExifOrientation($exif['Orientation']);
}
}