Merge pull request #4152 from humhub/fix/4113

Fix #4036: Profile images are not rotated correctly
This commit is contained in:
Lucas Bartholemy 2020-07-15 10:55:24 +02:00 committed by GitHub
commit 13d274a6e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 21 deletions

View File

@ -3,6 +3,7 @@ HumHub Change Log
1.5.3 (Unreleased)
--------------------
- Fix #4036: Profile images are not rotated correctly
- Fix #4168: Erroneous pagination in notification overview
- Fix #4060: Profile description and text regex error message not translatable
- Fix #4153: Administration: Email transport configuration 'Save & Test' Gives No Result

View File

@ -10,6 +10,7 @@ namespace humhub\libs;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\models\ContentContainer;
use humhub\modules\file\libs\ImageHelper;
use humhub\modules\space\models\Space;
use humhub\modules\space\widgets\Image as SpaceImage;
use humhub\modules\user\widgets\Image as UserImage;
@ -173,11 +174,16 @@ class ProfileImage
if ($file instanceof UploadedFile) {
$file = $file->tempName;
}
$this->delete();
// Make sure original file is max. 800 width
// Convert image to uploaded JPEG, fix orientation and remove additional meta information
$image = Image::getImagine()->open($file);
ImageHelper::fixJpegOrientation($image, $file);
$image->thumbnail($image->getSize())
->save($this->getPath('_org'), ['format' => 'jpg']);
// Make sure original file is max. 800 width
$image = Image::getImagine()->open($this->getPath('_org'));
if ($image->getSize()->getWidth() > 800) {
$image->resize($image->getSize()->widen(800));
}

View File

@ -8,6 +8,7 @@
namespace humhub\modules\file\converter;
use humhub\modules\file\libs\ImageHelper;
use Imagine\Image\ImageInterface;
use Yii;
use humhub\modules\file\models\File;
@ -80,25 +81,7 @@ class PreviewImage extends BaseConverter
if (!is_file($this->file->store->get($fileName))) {
$image = Image::getImagine()->open($this->file->store->get());
// Also handle orientation of resized images
// https://github.com/yiisoft/yii2-imagine/issues/44
if ($this->file->mime_type === 'image/jpeg' && function_exists('exif_read_data')) {
$exif = exif_read_data($this->file->store->get());
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 3:
$image->rotate(180);
break;
case 6:
$image->rotate(90);
break;
case 8:
$image->rotate(-90);
break;
}
}
}
ImageHelper::fixJpegOrientation($image, $this->file);
if ($image->getSize()->getHeight() > $this->options['height']) {
$image->resize($image->getSize()->heighten($this->options['height']));

View File

@ -0,0 +1,59 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2021 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\file\libs;
use humhub\modules\file\models\File;
use Imagine\Image\ImageInterface;
/**
* Class ImageHelper
*
* @since 1.5.2
* @package humhub\modules\file\libs
*/
class ImageHelper
{
/**
* Fix orientation of JPEG images based on EXIF information
*
* @see https://github.com/yiisoft/yii2-imagine/issues/44
* @param $image ImageInterface
* @param $file File|string
* @throws \yii\base\InvalidConfigException
*/
public static function fixJpegOrientation($image, $file)
{
$mimeType = '';
if ($file instanceof File) {
$mimeType = $file->mime_type;
$file = $file->store->get();
} elseif (is_string($file) && file_exists($file)) {
$mimeType = FileHelper::getMimeType($file);
}
if ($mimeType === 'image/jpeg' && function_exists('exif_read_data')) {
$exif = @exif_read_data($file);
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 3:
$image->rotate(180);
break;
case 6:
$image->rotate(90);
break;
case 8:
$image->rotate(-90);
break;
}
}
}
}
}