big update including Filters and smarter algorithms

This commit is contained in:
Christian Haschek
2015-10-29 20:37:20 +01:00
parent f6258fbc3e
commit 3430ec1e15
7 changed files with 279 additions and 44 deletions

View File

@@ -41,6 +41,11 @@ function aasort (&$array, $key)
$array=$ret;
}
function sanatizeString($string)
{
return preg_replace("/[^a-zA-Z0-9._]+/", "", $string);
}
function callHook()
{
@@ -48,34 +53,98 @@ function callHook()
global $default;
$urlArray = explode("/",$url);
whatToDo($urlArray);
}
function whatToDo($url)
{
$pm = new PictshareModel();
$u1 = preg_replace("/[^a-zA-Z0-9._]+/", "", $urlArray[0]);
$u2 = preg_replace("/[^a-zA-Z0-9._]+/", "", $urlArray[1]);
foreach($url as $el)
{
$el = sanatizeString($el);
$el = strtolower($el);
if(!$el) continue;
if(isImage($el))
$data['hash']=$el;
else if(isSize($el))
$data['size'] = $el;
else if(isRotation($el))
$data['rotate'] = $el;
else if(isFilter($el))
$data['filter'][] = $el;
else if($legacy = isLegacyThumbnail($el)) //so old uploads will still work
{
$data['hash'] = $legacy['hash'];
$data['size'] = $legacy['size'];
}
}
if(isImage($u1)) // render the image
renderImage($u1);
else if($u1=='store' || $u1=='originals') //render also with legacy urls
renderImage($u2);
else if(isResizedImage($u1,$u2)) // resize and render
renderResizedImage($u1,$u2);
else if($u1=='thumbs')
renderLegacyResized($u2);
else if(!$u1)
if(!is_array($data) || !$data['hash'])
{
if($_POST['submit']==$pm->translate(3))
$o=$pm->ProcessUploads();
else
$o.= $pm->renderUploadForm();
$vars['content'] = $o;
$vars['slogan'] = $pm->translate(2);
render($vars);
}
else
header("HTTP/1.0 404 Not Found");
renderImage($data);
}
function isLegacyThumbnail($val)
{
if(strpos($val,'_'))
{
$a = explode('_',$val);
$size = $a[0];
$hash = $a[1];
if(!isSize($size) || !isImage($hash)) return false;
return array('hash'=>$hash,'size'=>$size);
}
else return false;
}
function isFilter($var)
{
if(strpos($var,'_'))
{
$a = explode('_',$var);
$var = $a[0];
$val = $a[1];
if(!is_numeric($val)) return false;
}
$vars['content'] = $o;
$vars['slogan'] = $pm->translate(2);
render($vars);
switch($var)
{
case 'negative':
case 'grayscale':
case 'brightness':
case 'edgedetect':
case 'smooth':
case 'contrast':
case 'pixelate': return true;
default: return false;
}
}
function isRotation($var)
{
switch($var)
{
case 'upside':
case 'left':
case 'right': return true;
default: return false;
}
}
function renderLegacyResized($path)
@@ -95,15 +164,24 @@ function renderLegacyResized($path)
renderResizedImage($size,$hash);
}
function renderImage($hash,$file=false)
function renderImage($data)
{
$hash = $data['hash'];
$pm = new PictshareModel();
if(!$file) $file = DS.$hash;
$path = ROOT.DS.'upload'.DS.$hash.$file;
$base_path = ROOT.DS.'upload'.DS.$hash.DS;
$path = $base_path.$hash;
$type = $pm->isTypeAllowed($pm->getTypeOfFile($path));
$cached = false;
if($pm->countResizedImages($hash)>MAX_RESIZED_IMAGES) //if the number of max resized images is reached, just show the real one
$cachename = getCacheName($data);
$cachepath = $base_path.$cachename;
if(file_exists($cachepath))
{
$path = $cachepath;
$cached = true;
}
if($pm->countResizedImages($hash)>=MAX_RESIZED_IMAGES) //if the number of max resized images is reached, just show the real one
$path = ROOT.DS.'upload'.DS.$hash.DS.$hash;
switch($type)
@@ -111,16 +189,31 @@ function renderImage($hash,$file=false)
case 'jpg':
header ("Content-type: image/jpeg");
$im = imagecreatefromjpeg($path);
if(!$cached)
{
changeImage($im,$data);
imagejpeg($im,$cachepath,95);
}
imagejpeg($im);
break;
case 'png':
header ("Content-type: image/png");
$im = imagecreatefrompng($path);
if(!$cached)
{
changeImage($im,$data);
imagepng($im,$cachepath,1);
}
imagepng($im);
break;
case 'gif':
header ("Content-type: image/gif");
$im = imagecreatefromgif($path);
if(!$cached)
{
changeImage($im,$data);
imagegif($im,$cachepath);
}
imagegif($im);
break;
}
@@ -128,17 +221,48 @@ function renderImage($hash,$file=false)
exit();
}
function renderResizedImage($size,$hash)
function changeImage(&$im,$data)
{
$pm = new PictshareModel();
$im = new Image();
$image = new Image();
foreach($data as $action=>$val)
{
switch($action)
{
case 'rotate': $image->rotate($im,$val);break;
case 'size': $image->resize($im,$val);break;
case 'filter': $image->filter($im,$val);break;
}
}
}
function getCacheName($data)
{
ksort($data);
$name = array();
foreach($data as $key=>$val)
{
if($key!='hash')
{
if(!is_array($val))
$name[] = $key.'_'.$val;
else
foreach($val as $valdata)
$name[] = $valdata;
}
}
if(is_numeric($size))
$path = $im->getImage($hash,$size);
else
$path = $im->getImage($hash,explode('x',$size));
return implode('.',$name).'.'.$data['hash'];
}
function isSize($var)
{
if(is_numeric($var)) return true;
$a = explode('x',$var);
if(count($a)!=2 || !is_numeric($a[0]) || !is_numeric($a[1])) return false;
renderImage($hash,$path);
return true;
}
function isImage($hash)
@@ -148,15 +272,6 @@ function isImage($hash)
return $pm->hashExists($hash);
}
function isResizedImage($resize,$hash)
{
if(!isImage($hash) || !$resize || !$hash) return false;
$a = explode('x',$resize);
if(!is_numeric($resize) && count($a)!=2) return false;
return true;
}
function render($variables=null)
{
if(is_array($variables))