From 0153f09208622132dc85c28ef66c7fafca1ab5b6 Mon Sep 17 00:00:00 2001 From: Christian Haschek Date: Wed, 25 Nov 2015 12:19:49 +0100 Subject: [PATCH] rewrote mp4 and gif2mp4 functions to maintain the same naming structure as the rest of the resized images and stuff --- inc/core.php | 31 +++++++++++++++----------- models/pictsharemodel.php | 46 ++++++++++++++++++++------------------- template_mp4.php | 9 +++++++- 3 files changed, 50 insertions(+), 36 deletions(-) diff --git a/inc/core.php b/inc/core.php index 2360f93..e693f9f 100644 --- a/inc/core.php +++ b/inc/core.php @@ -152,21 +152,25 @@ function renderImage($data) case 'gif': if($data['mp4']) { - $mp4path = $path.'.mp4'; + $mp4path = $cachepath; + + if(!file_exists($mp4path)) //if mp4 does not exist, create it + $pm->gifToMP4($path,$mp4path); + if($data['raw']) { - serveFile($mp4path, 'gif/raw/'.$hash,'video/mp4'); + serveFile($mp4path, $hash.'.mp4','video/mp4'); } else if($data['preview']) { - $file = $mp4path.'.jpg'; + $file = $mp4path; if(!file_exists($file)) - $pm->saveFirstFrameOfMP4($mp4path); + $pm->saveFirstFrameOfMP4($mp4path,$path); header ("Content-type: image/jpeg"); readfile($file); } else - renderMP4($pm->gifToMP4($path),$data); + renderMP4($mp4path,$data); } else { @@ -176,26 +180,25 @@ function renderImage($data) break; case 'mp4': - if(!$cached) + if(!$cached && !$data['preview']) { $pm->resizeMP4($data,$cachepath); $path = $cachepath; } - if(filesize($path)==0) //if there was an error and the file is 0 bytes, use the original - $path = ROOT.DS.'upload'.DS.$hash.DS.$hash; + if(file_exists($cachepath) && filesize($cachepath)==0) //if there was an error and the file is 0 bytes, use the original + $cachepath = ROOT.DS.'upload'.DS.$hash.DS.$hash; if($data['raw']) { - serveFile($path, '/raw/'.$hash,'video/mp4'); + serveFile($cachepath, $hash,'video/mp4'); } else if($data['preview']) { - $file = $path.'.jpg'; - if(!file_exists($file)) - $pm->saveFirstFrameOfMP4($path); + if(!file_exists($cachepath)) + $pm->saveFirstFrameOfMP4($path,$cachepath); header ("Content-type: image/jpeg"); - readfile($file); + readfile($cachepath); } else renderMP4($path,$data); @@ -232,11 +235,13 @@ function renderMP4($path,$data) { $pm = new PictshareModel; $hash = $data['hash']; + $urldata = $pm->getURLInfo($path,true); if($data['size']) $hash = $data['size'].'/'.$hash; $info = $pm->getSizeOfMP4($path); $width = $info['width']; $height = $info['height']; + $filesize = $urldata['humansize']; include (ROOT . DS . 'template_mp4.php'); } diff --git a/models/pictsharemodel.php b/models/pictsharemodel.php index 3e1355e..10d9e59 100644 --- a/models/pictsharemodel.php +++ b/models/pictsharemodel.php @@ -7,7 +7,7 @@ class PictshareModel extends Model return array('status'=>'ok'); } - function getURLInfo($url) + function getURLInfo($url,$ispath=false) { $url = rawurldecode($url); $data = $this->urlToData($url); @@ -21,8 +21,10 @@ class PictshareModel extends Model $path = ROOT.DS.'upload'.DS.$hash.DS.$file; if(file_exists($path)) { - $type = $this->getTypeOfHash($hash); - $byte = filesize($path); + $type = $this->getType($path); + if($ispath) + $byte = filesize($url); + else $byte = filesize($path); if($type=='mp4') { $info = $this->getSizeOfMP4($path); @@ -191,8 +193,7 @@ class PictshareModel extends Model { ksort($data); unset($data['raw']); - unset($data['preview']); - unset($data['raw']); + //unset($data['preview']); $name = false; foreach($data as $key=>$val) { @@ -317,6 +318,11 @@ class PictshareModel extends Model } } + function getType($url) + { + return $this->isTypeAllowed($this->getTypeOfFile($url)); + } + function uploadImageFromURL($url) { $type = $this->getTypeOfFile($url); @@ -346,12 +352,6 @@ class PictshareModel extends Model mkdir(ROOT.DS.'upload'.DS.$hash); $file = ROOT.DS.'upload'.DS.$hash.DS.$hash; - - if($type=='mp4') - { - $this->saveFirstFrameOfMP4($file); - } - file_put_contents($file, file_get_contents($url)); //remove all exif data from jpeg @@ -681,27 +681,26 @@ class PictshareModel extends Model //if(!$maxheight) $maxheight = 'trunc(ow/a/2)*2'; - $cmd = "$bin -i $file -y -vf scale=\"$maxwidth:$maxheight\" -c:v libx264 $cachepath"; + $cmd = "$bin -i $file -y -vf scale=\"$maxwidth:$maxheight\" -c:v libx264 -f mp4 $cachepath"; system($cmd); return $cachepath; } - function gifToMP4($gifpath) + function gifToMP4($gifpath,$target) { $bin = escapeshellcmd(ROOT.DS.'bin'.DS.'ffmpeg'); $file = escapeshellarg($gifpath); - $mp4file = $gifpath.'.mp4'; - if(!file_exists($mp4file)) //simple caching.. have to think of something better + if(!file_exists($target)) //simple caching.. have to think of something better { - $cmd = "$bin -f gif -y -i $file -c:v libx264 $file.mp4"; + $cmd = "$bin -f gif -y -i $file -c:v libx264 -f mp4 $target"; system($cmd); } - return $mp4file; + return $target; } function saveAsMP4($source,$target) @@ -709,18 +708,16 @@ class PictshareModel extends Model $bin = escapeshellcmd(ROOT.DS.'bin'.DS.'ffmpeg'); $source = escapeshellarg($source); $target = escapeshellarg($target); - $cmd = "$bin -y -i $source -c:v libx264 $target"; + $cmd = "$bin -y -i $source -c:v libx264 -f mp4 $target"; system($cmd); } - function saveFirstFrameOfMP4($path) + function saveFirstFrameOfMP4($path,$target) { - //$path = ROOT.DS.'upload'.DS.$hash.DS.$hash; - $jpgpath = $path.'.jpg'; $bin = escapeshellcmd(ROOT.DS.'bin'.DS.'ffmpeg'); $file = escapeshellarg($path); - $cmd = "$bin -y -i $file -vframes 1 -f image2 $jpgpath"; + $cmd = "$bin -y -i $file -vframes 1 -f image2 $target"; system($cmd); } @@ -788,4 +785,9 @@ class PictshareModel extends Model break; } } + + function getMP4PathOfGif($gifpath) + { + + } } diff --git a/template_mp4.php b/template_mp4.php index c60c2b0..c97e508 100644 --- a/template_mp4.php +++ b/template_mp4.php @@ -7,9 +7,15 @@ @@ -40,6 +46,7 @@ +