From 5cc690bbee21481526931942eb541ab9e64edf01 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 30 Dec 2018 19:24:56 +0100 Subject: [PATCH] added filters --- content-controllers/image/filters.php | 278 ++++++++++++++++++ .../image/image.controller.php | 27 ++ 2 files changed, 305 insertions(+) diff --git a/content-controllers/image/filters.php b/content-controllers/image/filters.php index e69de29..aa26129 100644 --- a/content-controllers/image/filters.php +++ b/content-controllers/image/filters.php @@ -0,0 +1,278 @@ +6) + $blurFactor = 6; + else if($blurFactor<0) + $blurFactor = 0; + // blurFactor has to be an integer + $blurFactor = round($blurFactor); + + $originalWidth = imagesx($im); + $originalHeight = imagesy($im); + + $smallestWidth = ceil($originalWidth * pow(0.5, $blurFactor)); + $smallestHeight = ceil($originalHeight * pow(0.5, $blurFactor)); + + // for the first run, the previous image is the original input + $prevImage = $im; + $prevWidth = $originalWidth; + $prevHeight = $originalHeight; + + // scale way down and gradually scale back up, blurring all the way + for($i = 0; $i < $blurFactor; $i += 1) + { + // determine dimensions of next image + $nextWidth = $smallestWidth * pow(2, $i); + $nextHeight = $smallestHeight * pow(2, $i); + + // resize previous image to next size + $nextImage = imagecreatetruecolor($nextWidth, $nextHeight); + imagecopyresized($nextImage, $prevImage, 0, 0, 0, 0, + $nextWidth, $nextHeight, $prevWidth, $prevHeight); + + // apply blur filter + imagefilter($nextImage, IMG_FILTER_GAUSSIAN_BLUR); + + // now the new image becomes the previous image for the next step + $prevImage = $nextImage; + $prevWidth = $nextWidth; + $prevHeight = $nextHeight; + } + + // scale back to original size and blur one more time + imagecopyresized($im, $nextImage, + 0, 0, 0, 0, $originalWidth, $originalHeight, $nextWidth, $nextHeight); + imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR); + + // clean up + imagedestroy($prevImage); + + // return result + return $im; + } +} \ No newline at end of file diff --git a/content-controllers/image/image.controller.php b/content-controllers/image/image.controller.php index d1c3351..bec053c 100644 --- a/content-controllers/image/image.controller.php +++ b/content-controllers/image/image.controller.php @@ -64,13 +64,30 @@ class ImageController implements ContentController //don't do this if it's a gif because PHP can't handle animated gifs if($type!='gif') { + $filters = getFilters(); foreach($url as $u) { if(isSize($u)) $modifiers['size'] = $u; else if(isRotation($u)) $modifiers['rotation'] = $u; + else // check for filters + { + foreach($filters as $filter) + { + if(startsWith($u,$filter) && ($u==$filter || startsWith($u,$filter.'_'))) + { + $a = explode('_',$u); + $value = $a[1]; + if(is_numeric($value)) + $modifiers['filters'][] = array('filter'=>$filter,'value'=>$value); + else + $modifiers['filters'][] = array('filter'=>$filter); + } + } + } } + if(in_array('webp',$url) && $type!='webp') $modifiers['webp'] = true; if(in_array('forcesize',$url) && $modifiers['size']) @@ -91,6 +108,7 @@ class ImageController implements ContentController $modhash = md5(http_build_query($modifiers,'',',')); $newpath = ROOT.DS.'data'.DS.$hash.DS.$modhash.'_'.$hash; $im = $this->getObjOfImage($path); + $f = new Filter(); if(!file_exists($newpath)) { @@ -98,6 +116,15 @@ class ImageController implements ContentController { switch($mod) { + case 'filters': + foreach($val as $fd) + { + $filter = $fd['filter']; + $value = $fd['value']; + $im = $f->$filter($im,$value); + } + break; + case 'size': ($modifiers['forcesize']?forceResize($im,$val):resize($im,$val)); break;