Skip to content

Commit

Permalink
Merge pull request #2862 from craftcms/pull/transform-gifs-setting
Browse files Browse the repository at this point in the history
Added `transformGifs` config setting, Fixes #2845
  • Loading branch information
andris-sevcenko authored May 10, 2018
2 parents a918c89 + 711cb78 commit c0532b7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Added
- Added the `transformGifs` settings that defaults to `true`. Changing it to `false` prevents GIF files from being transformed or cleansed on upload.
- Added `craft\helpers\FileHelper::isGif()`.

### Changed
- Craft no longer logs warnings about missing translation files when Dev Mode isn’t enabled. ([#1531](https://github.com/craftcms/cms/issues/1531))
- Added `craft\services\Deprecator::$logTarget`. ([#2870](https://github.com/craftcms/cms/issues/2870))
Expand Down
4 changes: 4 additions & 0 deletions src/config/GeneralConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,10 @@ class GeneralConfig extends BaseObject
* This can be set to one of PHP’s supported timezones (http://php.net/manual/en/timezones.php).
*/
public $timezone;
/**
* @var bool Tells Craft whether GIF files should be transformed or cleansed. Defaults to true.
*/
public $transformGifs = true;
/**
* @var bool Tells Craft whether to surround all translatable strings with “@” symbols, to help find any strings that are not
* being run through Craft::t() or the |translate filter.
Expand Down
5 changes: 5 additions & 0 deletions src/elements/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use craft\helpers\FileHelper;
use craft\helpers\Html;
use craft\helpers\Image;
use craft\helpers\StringHelper;
use craft\helpers\Template;
use craft\helpers\UrlHelper;
use craft\models\AssetTransform;
Expand Down Expand Up @@ -704,6 +705,10 @@ public function getUrl($transform = null)
return null;
}

if (FileHelper::isGif($this->filename) && !Craft::$app->getConfig()->getGeneral()->transformGifs) {
return AssetsHelper::generateUrl($volume, $this);
}

// Normalize empty transform values
$transform = $transform ?: null;

Expand Down
28 changes: 26 additions & 2 deletions src/helpers/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,17 @@ public static function isWritable(string $path): bool
*/
public static function getMimeType($file, $magicFile = null, $checkExtension = true)
{
$mimeType = parent::getMimeType($file, $magicFile, $checkExtension);
try {
$mimeType = parent::getMimeType($file, $magicFile, $checkExtension);
} catch (\Throwable $e) {
if (!$checkExtension) {
throw $e;
}
$mimeType = null;
}

// Be forgiving of SVG files, etc., that don't have an XML declaration
if ($checkExtension && in_array($mimeType, ['text/plain', 'text/html', 'application/xml'])) {
if ($checkExtension && in_array($mimeType, [null, 'text/plain', 'text/html', 'application/xml'], true)) {
return static::getMimeTypeByExtension($file, $magicFile) ?? $mimeType;
}

Expand All @@ -284,6 +291,23 @@ public static function isSvg(string $file, string $magicFile = null, bool $check
return strpos($mimeType, 'image/svg') === 0;
}

/**
* Returns whether the given file path is an GIF image.
*
* @param string $file the file name.
* @param string $magicFile name of the optional magic database file (or alias), usually something like `/path/to/magic.mime`.
* This will be passed as the second parameter to [finfo_open()](http://php.net/manual/en/function.finfo-open.php)
* when the `fileinfo` extension is installed. If the MIME type is being determined based via [[getMimeTypeByExtension()]]
* and this is null, it will use the file specified by [[mimeMagicFile]].
* @param bool $checkExtension whether to use the file extension to determine the MIME type in case
* `finfo_open()` cannot determine it.
* @return bool
*/
public static function isGif(string $file, string $magicFile = null, bool $checkExtension = true): bool
{
return self::getMimeType($file, $magicFile, $checkExtension) === 'image/gif';
}

/**
* Writes contents to a file.
*
Expand Down
5 changes: 5 additions & 0 deletions src/services/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use craft\helpers\ConfigHelper;
use craft\helpers\FileHelper;
use craft\helpers\Image as ImageHelper;
use craft\helpers\StringHelper;
use craft\image\Raster;
use craft\image\Svg;
use enshrined\svgSanitize\Sanitizer;
Expand Down Expand Up @@ -298,6 +299,10 @@ public function cleanImage(string $filePath)
return;
}

if (FileHelper::isGif($filePath) && !Craft::$app->getConfig()->getGeneral()->transformGifs) {
return;
}

try {
if (Craft::$app->getConfig()->getGeneral()->rotateImagesOnUploadByExifData) {
$cleanedByRotation = $this->rotateImageByExifData($filePath);
Expand Down

0 comments on commit c0532b7

Please sign in to comment.