Skip to content

Commit

Permalink
ENH Remove animation for thumbnails by default. (silverstripe#1475)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored and emteknetnz committed Aug 19, 2024
1 parent 966e07d commit 5f5db33
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
45 changes: 42 additions & 3 deletions code/Model/ThumbnailGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use LogicException;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Image_Backend;
use SilverStripe\Assets\Storage\AssetContainer;
use SilverStripe\Assets\Storage\AssetStore;
use SilverStripe\Assets\Storage\DBFile;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Configurable;

/**
Expand Down Expand Up @@ -65,6 +67,8 @@ class ThumbnailGenerator
*/
private static $method = 'FitMax';

private bool $allowsAnimation = false;

/**
* Generate thumbnail and return the "src" property for this thumbnail
*
Expand Down Expand Up @@ -107,9 +111,33 @@ public function generateThumbnail(AssetContainer $file, $width, $height)
$file = $file->existingOnly();
}

// Make large thumbnail
$method = $this->config()->get('method');
return $file->$method($width, $height);
// Disable animation while generating thumbnail
$origAllowAnimation = null;
if (!$this->getAllowsAnimation()) {
if (ClassInfo::hasMethod($file, 'getImageBackend')) {
/** @var Image_Backend $backend */
$backend = $file->getImageBackend();
$origAllowAnimation = $backend->getAllowsAnimationInManipulations();
$backend->setAllowsAnimationInManipulations(false);
} elseif ($file->getIsAnimated() && ClassInfo::hasMethod($file, 'RemoveAnimation')) {
$noAnimation = $file->RemoveAnimation();
if ($noAnimation) {
$file = $noAnimation;
}
}
}

try {
// Make large thumbnail
$method = $this->config()->get('method');
$thumbnail = $file->$method($width, $height);
} finally {
if ($origAllowAnimation !== null) {
$backend->setAllowsAnimationInManipulations($origAllowAnimation);
}
}

return $thumbnail;
}

/**
Expand Down Expand Up @@ -173,4 +201,15 @@ public function setGenerates($generates)
$this->generates = $generates;
return $this;
}

public function getAllowsAnimation(): bool
{
return $this->allowsAnimation;
}

public function setAllowsAnimation(bool $allows): static
{
$this->allowsAnimation = $allows;
return $this;
}
}
Binary file added tests/php/Forms/fixtures/animated.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions tests/php/Model/ThumbnailGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,29 @@ public function testGenerateLink()
$thumbnail = $generator->generateThumbnailLink($image, 100, 200);
$this->assertEquals('/assets/906835357d/TestImage__FitMaxWzEwMCwyMDBd.png', $thumbnail);
}

public function provideAnimatedThumbnail(): array
{
return [
[true],
[false],
];
}

/**
* @dataProvider provideAnimatedThumbnail
*/
public function testAnimatedThumbnail(bool $allowAnimation): void
{
$image = new Image();
$image->setFromLocalFile(__DIR__ . '/../Forms/fixtures/animated.gif', 'animated.gif');
$image->write();

ThumbnailGenerator::config()->set('method', 'Fit');
$generator = new ThumbnailGenerator();
$generator->setAllowsAnimation($allowAnimation);
$thumbnail = $generator->generateThumbnail($image, 100, 100);

$this->assertSame($allowAnimation, $thumbnail->getIsAnimated());
}
}

0 comments on commit 5f5db33

Please sign in to comment.