Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Remove animation for thumbnails by default. #1475

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
Comment on lines +116 to +128
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #1475 (comment) - this is now more complex logic, but that principle remains that we should adhere to the API of the AssetContainer interface. As such I've provided two ways to do this - one where there's an Image_Backend available and one where that's not available but RemoveAnimation() is.


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());
}
}
Loading