Merge pull request #1930 from nextcloud/fix_avatars_exif
Fix avatar on exif rotated images
This commit is contained in:
commit
7cae758ef9
2 changed files with 28 additions and 0 deletions
|
@ -237,6 +237,7 @@ class AvatarController extends Controller {
|
|||
try {
|
||||
$image = new \OC_Image();
|
||||
$image->loadFromData($content);
|
||||
$image->readExif($content);
|
||||
$image->fixOrientation();
|
||||
|
||||
if ($image->valid()) {
|
||||
|
|
|
@ -54,6 +54,8 @@ class OC_Image implements \OCP\IImage {
|
|||
private $fileInfo;
|
||||
/** @var \OCP\ILogger */
|
||||
private $logger;
|
||||
/** @var array */
|
||||
private $exif;
|
||||
|
||||
/**
|
||||
* Get mime type for an image file.
|
||||
|
@ -347,6 +349,10 @@ class OC_Image implements \OCP\IImage {
|
|||
* @return int The orientation or -1 if no EXIF data is available.
|
||||
*/
|
||||
public function getOrientation() {
|
||||
if ($this->exif !== null) {
|
||||
return $this->exif['Orientation'];
|
||||
}
|
||||
|
||||
if ($this->imageType !== IMAGETYPE_JPEG) {
|
||||
$this->logger->debug('OC_Image->fixOrientation() Image is not a JPEG.', array('app' => 'core'));
|
||||
return -1;
|
||||
|
@ -370,9 +376,30 @@ class OC_Image implements \OCP\IImage {
|
|||
if (!isset($exif['Orientation'])) {
|
||||
return -1;
|
||||
}
|
||||
$this->exif = $exif;
|
||||
return $exif['Orientation'];
|
||||
}
|
||||
|
||||
public function readExif($data) {
|
||||
if (!is_callable('exif_read_data')) {
|
||||
$this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core'));
|
||||
return;
|
||||
}
|
||||
if (!$this->valid()) {
|
||||
$this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core'));
|
||||
return;
|
||||
}
|
||||
|
||||
$exif = @exif_read_data('data://image/jpeg;base64,' . base64_encode($data));
|
||||
if (!$exif) {
|
||||
return;
|
||||
}
|
||||
if (!isset($exif['Orientation'])) {
|
||||
return;
|
||||
}
|
||||
$this->exif = $exif;
|
||||
}
|
||||
|
||||
/**
|
||||
* (I'm open for suggestions on better method name ;)
|
||||
* Fixes orientation based on EXIF data.
|
||||
|
|
Loading…
Reference in a new issue