From d08c15b9e7c52722cf5b3d1493dd66b26799a817 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 30 Apr 2014 09:54:04 +0200 Subject: [PATCH] Refactor to thumbnail generating --- src/Controllers/PostController.php | 2 +- src/Helpers/ThumbnailHelper.php | 118 +++++++++++++++++++++++++++++ src/Models/Entities/PostEntity.php | 106 +++----------------------- 3 files changed, 128 insertions(+), 98 deletions(-) diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 20a53103..cafedc0d 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -370,7 +370,7 @@ class PostController $post = PostModel::findByIdOrName($name); Access::assert(Privilege::ListPosts); Access::assert(Privilege::ListPosts, PostSafety::toString($post->safety)); - $post->makeThumb($width, $height); + $post->generateThumb($width, $height); if (!file_exists($path)) { $path = getConfig()->main->mediaPath . DS . 'img' . DS . 'thumb.jpg'; diff --git a/src/Helpers/ThumbnailHelper.php b/src/Helpers/ThumbnailHelper.php index 4f9d1ccf..1e3358f0 100644 --- a/src/Helpers/ThumbnailHelper.php +++ b/src/Helpers/ThumbnailHelper.php @@ -44,4 +44,122 @@ class ThumbnailHelper imagecopyresampled($dstImage, $srcImage, 0, 0, 0, 0, $w, $h, $srcWidth, $srcHeight); return $dstImage; } + + public static function generateFromUrl($url, $dstPath, $width, $height) + { + $tmpPath = tempnam(sys_get_temp_dir(), 'thumb') . '.jpg'; + + TransferHelper::download( + $url, + $tmpPath, + null); + + $ret = self::generateFromPath($tmpPath, $dstPath, $width, $height); + + unlink($tmpPath); + return $ret; + } + + public static function generateFromPath($srcPath, $dstPath, $width, $height) + { + $mime = mime_content_type($srcPath); + + switch ($mime) + { + case 'application/x-shockwave-flash': + $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)) + { + $ret = self::generateFromPath($tmpPath, $dstPath, $width, $height); + unlink($tmpPath); + return $ret; + } + + exec('swfrender ' . $srcPath . ' -o ' . $tmpPath); + + if (file_exists($tmpPath)) + { + $ret = self::generateFromPath($tmpPath, $dstPath, $width, $height); + unlink($tmpPath); + return $ret; + } + + return false; + + case 'video/mp4': + case 'video/webm': + case 'video/ogg': + case 'application/ogg': + case 'video/x-flv': + case 'video/3gpp': + $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)) + { + $ret = self::generateFromPath($tmpPath, $dstPath, $width, $height); + unlink($tmpPath); + return $ret; + } + + $cmd = sprintf( + 'ffmpeg -i "%s" -vframes 1 "%s"', + $srcPath, + $tmpPath); + exec($cmd); + + if (file_exists($tmpPath)) + { + $ret = self::generateFromPath($tmpPath, $dstPath, $width, $height); + unlink($tmpPath); + return $ret; + } + + return false; + + case 'image/jpeg': + $srcImage = imagecreatefromjpeg($srcPath); + break; + + case 'image/png': + $srcImage = imagecreatefrompng($srcPath); + break; + + case 'image/gif': + $srcImage = imagecreatefromgif($srcPath); + break; + + default: + throw new SimpleException('Invalid thumbnail file type'); + } + + $config = getConfig(); + switch ($config->browsing->thumbStyle) + { + case 'outside': + $dstImage = ThumbnailHelper::cropOutside($srcImage, $width, $height); + break; + case 'inside': + $dstImage = ThumbnailHelper::cropInside($srcImage, $width, $height); + break; + default: + throw new SimpleException('Unknown thumbnail crop style'); + } + + imagejpeg($dstImage, $dstPath); + imagedestroy($srcImage); + imagedestroy($dstImage); + } } diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index 7932f557..11094c8b 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -227,112 +227,24 @@ class PostEntity extends AbstractEntity TransferHelper::moveUpload($srcPath, $dstPath); } - public function makeThumb($width = null, $height = null) + public function generateThumb($width = null, $height = null) { list ($width, $height) = PostModel::validateThumbSize($width, $height); - $dstPath = $this->getThumbDefaultPath($width, $height); $srcPath = $this->getFullPath(); + $dstPath = $this->getThumbDefaultPath($width, $height); if ($this->type == PostType::Youtube) { - $tmpPath = tempnam(sys_get_temp_dir(), 'thumb') . '.jpg'; - $contents = file_get_contents('http://img.youtube.com/vi/' . $this->fileHash . '/mqdefault.jpg'); - file_put_contents($tmpPath, $contents); - if (file_exists($tmpPath)) - $srcImage = imagecreatefromjpeg($tmpPath); + return ThumbnailHelper::generateFromUrl( + 'http://img.youtube.com/vi/' . $this->fileHash . '/mqdefault.jpg', + $dstPath, + $width, + $height); } - else switch ($this->mimeType) + else { - case 'image/jpeg': - $srcImage = imagecreatefromjpeg($srcPath); - break; - case 'image/png': - $srcImage = imagecreatefrompng($srcPath); - break; - case 'image/gif': - $srcImage = imagecreatefromgif($srcPath); - break; - - case 'application/x-shockwave-flash': - $srcImage = null; - $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)) - $srcImage = imagecreatefrompng($tmpPath); - - if (!$srcImage) - { - $tmpPath = tempnam(sys_get_temp_dir(), 'thumb') . '.png'; - 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'; - - $cmd = sprintf( - 'ffmpegthumbnailer -i"%s" -o"%s" -s0 -t"12%"', - $srcPath, - $tmpPath); - exec($cmd); - - if (file_exists($tmpPath)) - $srcImage = imagecreatefrompng($tmpPath); - - if (!$srcImage) - { - exec($cmd); - $cmd = sprintf( - 'ffmpeg -i "%s" -vframes 1 "%s"', - $srcPath, - $tmpPath); - - if (file_exists($tmpPath)) - $srcImage = imagecreatefrompng($tmpPath); - } - break; - default: - break; + return ThumbnailHelper::generateFromPath($srcPath, $dstPath, $width, $height); } - - if (isset($tmpPath) and file_exists($tmpPath)) - unlink($tmpPath); - - if (!isset($srcImage)) - return false; - - $config = getConfig(); - switch ($config->browsing->thumbStyle) - { - case 'outside': - $dstImage = ThumbnailHelper::cropOutside($srcImage, $width, $height); - break; - case 'inside': - $dstImage = ThumbnailHelper::cropInside($srcImage, $width, $height); - break; - default: - throw new SimpleException('Unknown thumbnail crop style'); - } - - imagejpeg($dstImage, $dstPath); - imagedestroy($srcImage); - imagedestroy($dstImage); - - return true; } public function setContentFromPath($srcPath, $origName)