Skip to content

Commit

Permalink
ENH Remove animation for thumbnails by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Jul 11, 2024
1 parent 0b20ec7 commit 6056770
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
22 changes: 22 additions & 0 deletions code/Model/ThumbnailGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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 +66,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,6 +110,14 @@ public function generateThumbnail(AssetContainer $file, $width, $height)
$file = $file->existingOnly();
}

// Try to remove the animation if we can
if (!$this->getAllowsAnimation() && $file->getIsAnimated() && ClassInfo::hasMethod($file, 'RemoveAnimation')) {
$noAnimation = $file->RemoveAnimation();
if ($noAnimation) {
$file = $noAnimation;
}
}

// Make large thumbnail
$method = $this->config()->get('method');
return $file->$method($width, $height);
Expand Down Expand Up @@ -173,4 +184,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;
}
}
24 changes: 24 additions & 0 deletions tests/php/Model/ThumbnailGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,28 @@ 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();

$generator = new ThumbnailGenerator();
$generator->setAllowsAnimation($allowAnimation);
$thumbnail = $generator->generateThumbnail($image, 100, 100);

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

0 comments on commit 6056770

Please sign in to comment.