From af1828a9e866e784c25244270ff4ea8dadab3dee Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 8 Apr 2014 16:54:36 +0200 Subject: [PATCH] Added HTML5 video support (closed #75) --- src/Models/Entities/PostEntity.php | 49 ++++++++++++++++++++++-------- src/Models/Enums/PostType.php | 1 + src/Views/post-file-render.phtml | 7 +++++ 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index b0d1d8c9..3e07d28a 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -260,33 +260,47 @@ class PostEntity extends AbstractEntity case 'image/gif': $srcImage = imagecreatefromgif($srcPath); break; + case 'application/x-shockwave-flash': $srcImage = null; - exec('which dump-gnash', $tmp, $exitCode); - if ($exitCode == 0) + $tmpPath = tempnam(sys_get_temp_dir(), 'thumb') . '.png'; + exec('dump-gnash --screenshot last --screenshot-file "' . $tmpPath . '" -1 -r1 --max-advances 15 "' . $srcPath . '"'); + if (file_exists($tmpPath)) + $srcImage = imagecreatefrompng($tmpPath); + + if (!$srcImage) { $tmpPath = tempnam(sys_get_temp_dir(), 'thumb') . '.png'; - exec('dump-gnash --screenshot last --screenshot-file ' . $tmpPath . ' -1 -r1 --max-advances 15 ' . $srcPath); + exec('swfrender ' . $srcPath . ' -o ' . $tmpPath); if (file_exists($tmpPath)) $srcImage = imagecreatefrompng($tmpPath); } + break; + + case 'video/mp4': + case 'video/webm': + case 'video/ogg': + case 'application/ogg': + case 'video/x-flv': + case 'video/3gpp': + $srcImage = null; + $tmpPath = tempnam(sys_get_temp_dir(), 'thumb') . '.png'; + exec('ffmpegthumbnailer -i"' . $srcPath . '" -o"' . $tmpPath . '" -s0 -t"12%"'); + if (file_exists($tmpPath)) + $srcImage = imagecreatefrompng($tmpPath); + if (!$srcImage) { - exec('which swfrender', $tmp, $exitCode); - if ($exitCode == 0) - { - $tmpPath = tempnam(sys_get_temp_dir(), 'thumb') . '.png'; - exec('swfrender ' . $srcPath . ' -o ' . $tmpPath); - if (file_exists($tmpPath)) - $srcImage = imagecreatefrompng($tmpPath); - } + exec('ffmpeg -i "' . $srcPath . '" -vframes 1 "' . $tmpPath . '"'); + if (file_exists($tmpPath)) + $srcImage = imagecreatefrompng($tmpPath); } break; default: break; } - if (isset($tmpPath)) + if (isset($tmpPath) and file_exists($tmpPath)) unlink($tmpPath); if (!isset($srcImage)) @@ -339,6 +353,17 @@ class PostEntity extends AbstractEntity $this->imageWidth = $imageWidth; $this->imageHeight = $imageHeight; break; + case 'video/webm': + case 'video/mp4': + case 'video/ogg': + case 'application/ogg': + case 'video/x-flv': + case 'video/3gpp': + list ($imageWidth, $imageHeight) = getimagesize($srcPath); + $this->type = PostType::Video; + $this->imageWidth = $imageWidth; + $this->imageHeight = $imageHeight; + break; default: throw new SimpleException('Invalid file type "' . $this->mimeType . '"'); } diff --git a/src/Models/Enums/PostType.php b/src/Models/Enums/PostType.php index 2a0a3407..3eb305a1 100644 --- a/src/Models/Enums/PostType.php +++ b/src/Models/Enums/PostType.php @@ -4,4 +4,5 @@ class PostType extends Enum const Image = 1; const Flash = 2; const Youtube = 3; + const Video = 4; } diff --git a/src/Views/post-file-render.phtml b/src/Views/post-file-render.phtml index 1dedd668..e1fdd442 100644 --- a/src/Views/post-file-render.phtml +++ b/src/Views/post-file-render.phtml @@ -24,4 +24,11 @@ +type == PostType::Video): ?> + + +