Added missing thumbnail generators
This commit is contained in:
parent
109aa1c39e
commit
65da8e9948
4 changed files with 145 additions and 2 deletions
44
src/Services/ThumbnailGenerators/FlashThumbnailGenerator.php
Normal file
44
src/Services/ThumbnailGenerators/FlashThumbnailGenerator.php
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
namespace Szurubooru\Services\ThumbnailGenerators;
|
||||||
|
|
||||||
|
class FlashThumbnailGenerator implements IThumbnailGenerator
|
||||||
|
{
|
||||||
|
private $imageThumbnailGenerator;
|
||||||
|
|
||||||
|
public function __construct(ImageThumbnailGenerator $imageThumbnailGenerator)
|
||||||
|
{
|
||||||
|
$this->imageThumbnailGenerator = $imageThumbnailGenerator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generate($srcPath, $dstPath, $width, $height)
|
||||||
|
{
|
||||||
|
if (!file_exists($srcPath))
|
||||||
|
throw new \InvalidArgumentException($srcPath . ' does not exist');
|
||||||
|
|
||||||
|
$tmpPath = tempnam(sys_get_temp_dir(), 'thumb') . '.png';
|
||||||
|
|
||||||
|
$cmd = sprintf(
|
||||||
|
'dump-gnash --screenshot last --screenshot-file "%s" -1 -r1 --max-advances 15 "%s"',
|
||||||
|
$tmpPath,
|
||||||
|
$srcPath);
|
||||||
|
exec($cmd);
|
||||||
|
|
||||||
|
if (file_exists($tmpPath))
|
||||||
|
{
|
||||||
|
$this->imageThumbnailGenerator->generate($tmpPath, $dstPath, $width, $height);
|
||||||
|
unlink($tmpPath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
exec('swfrender ' . $srcPath . ' -o ' . $tmpPath);
|
||||||
|
|
||||||
|
if (file_exists($tmpPath))
|
||||||
|
{
|
||||||
|
$this->imageThumbnailGenerator->generate($tmpPath, $dstPath, $width, $height);
|
||||||
|
unlink($tmpPath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \RuntimeException('Failed to generate thumbnail');
|
||||||
|
}
|
||||||
|
}
|
53
src/Services/ThumbnailGenerators/SmartThumbnailGenerator.php
Normal file
53
src/Services/ThumbnailGenerators/SmartThumbnailGenerator.php
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
<?php
|
||||||
|
namespace Szurubooru\Services\ThumbnailGenerators;
|
||||||
|
|
||||||
|
class SmartThumbnailGenerator implements IThumbnailGenerator
|
||||||
|
{
|
||||||
|
private $flashThumbnailGenerator;
|
||||||
|
private $videoThumbnailGenerator;
|
||||||
|
private $imageThumbnailGenerator;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
FlashThumbnailGenerator $flashThumbnailGenerator,
|
||||||
|
VideoThumbnailGenerator $videoThumbnailGenerator,
|
||||||
|
ImageThumbnailGenerator $imageThumbnailGenerator)
|
||||||
|
{
|
||||||
|
$this->flashThumbnailGenerator = $flashThumbnailGenerator;
|
||||||
|
$this->videoThumbnailGenerator = $videoThumbnailGenerator;
|
||||||
|
$this->imageThumbnailGenerator = $imageThumbnailGenerator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generate($srcPath, $dstPath, $width, $height)
|
||||||
|
{
|
||||||
|
if (!file_exists($srcPath))
|
||||||
|
throw new \InvalidArgumentException($srcPath . ' does not exist');
|
||||||
|
|
||||||
|
$mime = mime_content_type($srcPath);
|
||||||
|
|
||||||
|
if ($this->isFlash($mime))
|
||||||
|
return $this->flashThumbnailGenerator->generate($srcPath, $dstPath, $width, $height);
|
||||||
|
|
||||||
|
if ($this->isVideo($mime))
|
||||||
|
return $this->videoThumbnailGenerator->generate($srcPath, $dstPath, $width, $height);
|
||||||
|
|
||||||
|
if ($this->isImage($mime))
|
||||||
|
return $this->imageThumbnailGenerator->generate($srcPath, $dstPath, $width, $height);
|
||||||
|
|
||||||
|
throw new \InvalidArgumentException('Invalid thumbnail file type: ' . $mime);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isFlash($mime)
|
||||||
|
{
|
||||||
|
return $mime === 'application/x-shockwave-flash';
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isVideo($mime)
|
||||||
|
{
|
||||||
|
return $mime === 'application/ogg' or preg_match('/video\//', $mime);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isImage($mime)
|
||||||
|
{
|
||||||
|
return in_array($mime, ['image/jpeg', 'image/png', 'image/gif']);
|
||||||
|
}
|
||||||
|
}
|
48
src/Services/ThumbnailGenerators/VideoThumbnailGenerator.php
Normal file
48
src/Services/ThumbnailGenerators/VideoThumbnailGenerator.php
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
<?php
|
||||||
|
namespace Szurubooru\Services\ThumbnailGenerators;
|
||||||
|
|
||||||
|
class VideoThumbnailGenerator implements IThumbnailGenerator
|
||||||
|
{
|
||||||
|
private $imageThumbnailGenerator;
|
||||||
|
|
||||||
|
public function __construct(ImageThumbnailGenerator $imageThumbnailGenerator)
|
||||||
|
{
|
||||||
|
$this->imageThumbnailGenerator = $imageThumbnailGenerator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generate($srcPath, $dstPath, $width, $height)
|
||||||
|
{
|
||||||
|
if (!file_exists($srcPath))
|
||||||
|
throw new \InvalidArgumentException($srcPath . ' does not exist');
|
||||||
|
|
||||||
|
$tmpPath = tempnam(sys_get_temp_dir(), 'thumb') . '.jpg';
|
||||||
|
|
||||||
|
$cmd = sprintf(
|
||||||
|
'ffmpegthumbnailer -i"%s" -o"%s" -s0 -t"12%%"',
|
||||||
|
$srcPath,
|
||||||
|
$tmpPath);
|
||||||
|
exec($cmd);
|
||||||
|
|
||||||
|
if (file_exists($tmpPath))
|
||||||
|
{
|
||||||
|
$this->imageThumbnailGenerator->generate($tmpPath, $dstPath, $width, $height);
|
||||||
|
unlink($tmpPath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$cmd = sprintf(
|
||||||
|
'ffmpeg -i "%s" -vframes 1 "%s"',
|
||||||
|
$srcPath,
|
||||||
|
$tmpPath);
|
||||||
|
exec($cmd);
|
||||||
|
|
||||||
|
if (file_exists($tmpPath))
|
||||||
|
{
|
||||||
|
$this->imageThumbnailGenerator->generate($tmpPath, $dstPath, $width, $height);
|
||||||
|
unlink($tmpPath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \RuntimeException('Failed to generate thumbnail');
|
||||||
|
}
|
||||||
|
}
|
2
tests/files/.gitignore
vendored
2
tests/files/.gitignore
vendored
|
@ -1,2 +0,0 @@
|
||||||
*
|
|
||||||
!.gitignore
|
|
Loading…
Reference in a new issue