Refactor to thumbnail generating
This commit is contained in:
parent
c52531e8fc
commit
d08c15b9e7
3 changed files with 128 additions and 98 deletions
|
@ -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';
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
return ThumbnailHelper::generateFromPath($srcPath, $dstPath, $width, $height);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
Loading…
Reference in a new issue