Added missing thumbnail generators

This commit is contained in:
Marcin Kurczewski 2014-09-09 17:39:16 +02:00
parent 109aa1c39e
commit 65da8e9948
4 changed files with 145 additions and 2 deletions

View 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');
}
}

View 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']);
}
}

View 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');
}
}

View file

@ -1,2 +0,0 @@
*
!.gitignore