diff --git a/src/Services/ThumbnailGenerators/FlashThumbnailGenerator.php b/src/Services/ThumbnailGenerators/FlashThumbnailGenerator.php new file mode 100644 index 00000000..f12bf6e3 --- /dev/null +++ b/src/Services/ThumbnailGenerators/FlashThumbnailGenerator.php @@ -0,0 +1,44 @@ +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'); + } +} diff --git a/src/Services/ThumbnailGenerators/SmartThumbnailGenerator.php b/src/Services/ThumbnailGenerators/SmartThumbnailGenerator.php new file mode 100644 index 00000000..5dcef4b2 --- /dev/null +++ b/src/Services/ThumbnailGenerators/SmartThumbnailGenerator.php @@ -0,0 +1,53 @@ +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']); + } +} diff --git a/src/Services/ThumbnailGenerators/VideoThumbnailGenerator.php b/src/Services/ThumbnailGenerators/VideoThumbnailGenerator.php new file mode 100644 index 00000000..621d9f23 --- /dev/null +++ b/src/Services/ThumbnailGenerators/VideoThumbnailGenerator.php @@ -0,0 +1,48 @@ +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'); + } +} diff --git a/tests/files/.gitignore b/tests/files/.gitignore deleted file mode 100644 index d6b7ef32..00000000 --- a/tests/files/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore