From 0e362c3d8adb28f5643689c2d45a5f2b808686f9 Mon Sep 17 00:00:00 2001 From: Christian Haschek Date: Fri, 27 Nov 2015 13:03:21 +0100 Subject: [PATCH] added gif resize functionality --- classes/image.php | 36 ++++++++------------------ inc/core.php | 13 +++++++--- models/pictsharemodel.php | 54 ++++++++++++++++++++++----------------- 3 files changed, 51 insertions(+), 52 deletions(-) diff --git a/classes/image.php b/classes/image.php index c94f1da..41ef11a 100644 --- a/classes/image.php +++ b/classes/image.php @@ -17,19 +17,11 @@ class Image function forceResize(&$img,$size) { - if(!is_numeric($size)) - $size = explode('x',$size); - - if(is_array($size)) - { - $maxwidth = $size[0]; - $maxheight = $size[1]; - } - else if($size) - { - $maxwidth = $size; - $maxheight = $size; - } + $pm = new PictshareModel(); + + $sd = $pm->sizeStringToWidthHeight($size); + $maxwidth = $sd['maxwidth']; + $maxheight = $sd['maxheight']; @@ -76,19 +68,11 @@ class Image */ function resize(&$img,$size) { - if(!is_numeric($size)) - $size = explode('x',$size); - - if(is_array($size)) - { - $maxwidth = $size[0]; - $maxheight = $size[1]; - } - else if($size) - { - $maxwidth = $size; - $maxheight = $size; - } + $pm = new PictshareModel(); + + $sd = $pm->sizeStringToWidthHeight($size); + $maxwidth = $sd['maxwidth']; + $maxheight = $sd['maxheight']; $width = imagesx($img); $height = imagesy($img); diff --git a/inc/core.php b/inc/core.php index b62ad1b..7fed2d1 100644 --- a/inc/core.php +++ b/inc/core.php @@ -157,7 +157,7 @@ function renderImage($data) if(!file_exists($mp4path) && !$data['preview']) //if mp4 does not exist, create it $pm->gifToMP4($gifpath,$mp4path); - + if($data['raw']) { serveFile($mp4path, $hash.'.mp4','video/mp4'); @@ -175,15 +175,22 @@ function renderImage($data) } else //user wants gif { + if(!$cached && $data['size']) + { + $pm->resizeFFMPEG($data,$cachepath,'gif'); + } header ("Content-type: image/gif"); - readfile($path); + if(file_exists($cachepath)) + readfile($cachepath); + else + readfile($path); } break; case 'mp4': if(!$cached && !$data['preview']) { - $pm->resizeMP4($data,$cachepath); + $pm->resizeFFMPEG($data,$cachepath,'mp4'); $path = $cachepath; } diff --git a/models/pictsharemodel.php b/models/pictsharemodel.php index 10d9e59..4ea51a0 100644 --- a/models/pictsharemodel.php +++ b/models/pictsharemodel.php @@ -650,7 +650,7 @@ class PictshareModel extends Model return $ismp4; } - function resizeMP4($data,$cachepath) + function resizeFFMPEG($data,$cachepath,$type='mp4') { $file = ROOT.DS.'upload'.DS.$data['hash'].DS.$data['hash']; $file = escapeshellarg($file); @@ -661,27 +661,20 @@ class PictshareModel extends Model if(!$size) return $file; - if(!is_numeric($size)) - $size = explode('x',$size); - - if(is_array($size)) - { - $maxwidth = $size[0]; - $maxheight = $size[1]; - } - else if($size) - { - $maxwidth = $size; - $maxheight = 0; - } - - if($maxwidth>1080) - $maxwidth = 1080; - - //if(!$maxheight) + $sd = $this->sizeStringToWidthHeight($size); + $maxwidth = $sd['width']; + $maxheight = $sd['height']; + + switch($type) + { + case 'mp4': + $addition = '-c:v libx264'; + break; + } + $maxheight = 'trunc(ow/a/2)*2'; - $cmd = "$bin -i $file -y -vf scale=\"$maxwidth:$maxheight\" -c:v libx264 -f mp4 $cachepath"; + $cmd = "$bin -i $file -y -vf scale=\"$maxwidth:$maxheight\" $addition -f $type $cachepath"; system($cmd); @@ -772,7 +765,6 @@ class PictshareModel extends Model "width"=> $data['width'], "height"=> $data['height'], "title"=> "PictShare", - //"url"=> $url.'/raw', "provider_name"=> "PictShare", "provider_url"=> DOMAINPATH, "html"=> '