From aa75c56364daa739ea0190ec22231c1d85bd7e35 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 23 Dec 2018 15:35:47 +0100 Subject: [PATCH] implemented image resizing and conversion to webp --- content-controllers/image/conversion.php | 0 content-controllers/image/filters.php | 0 .../image/image.controller.php | 95 +++++++++++++- content-controllers/image/resize.php | 120 ++++++++++++++++++ inc/core.php | 2 + 5 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 content-controllers/image/conversion.php create mode 100644 content-controllers/image/filters.php create mode 100644 content-controllers/image/resize.php diff --git a/content-controllers/image/conversion.php b/content-controllers/image/conversion.php new file mode 100644 index 0000000..e69de29 diff --git a/content-controllers/image/filters.php b/content-controllers/image/filters.php new file mode 100644 index 0000000..e69de29 diff --git a/content-controllers/image/image.controller.php b/content-controllers/image/image.controller.php index 6824541..7bb52f3 100644 --- a/content-controllers/image/image.controller.php +++ b/content-controllers/image/image.controller.php @@ -69,8 +69,75 @@ class ImageController implements ContentController public function handleHash($hash,$url) { $path = ROOT.DS.'data'.DS.$hash.DS.$hash; - $type = getExtensionOfFilename($hash); + + //get all our sub files where all the good functions lie + include_once(dirname(__FILE__).DS.'resize.php'); + include_once(dirname(__FILE__).DS.'filters.php'); + include_once(dirname(__FILE__).DS.'conversion.php'); + + //don't do this if it's a gif because PHP can't handle animated gifs + if($type!='gif') + { + foreach($url as $u) + { + if(isSize($u)) + $modifiers['size'] = $u; + else if(isRotation($u)) + $modifiers['rotation'] = $u; + } + if(in_array('webp',$url) && $type!='webp') + $modifiers['webp'] = true; + if(in_array('forcesize',$url) && $modifiers['size']) + $modifiers['forcesize'] = true; + } + else //gif + { + if(in_array('mp4',$url)) + $modifiers['mp4']=true; + } + + if($modifiers) + { + //why in gods name would you use http build query here??? + //well we want a unique filename for every modied image + //so if we take all parameters in key=>value form and hash it + //we get one nice little hash for every eventuality + $modhash = md5(http_build_query($modifiers,'',',')); + $newpath = ROOT.DS.'data'.DS.$hash.DS.$modhash.'_'.$hash; + $im = $this->getObjOfImage($path); + + if(!file_exists($newpath)) + { + foreach($modifiers as $mod => $val) + { + switch($mod) + { + case 'size': + ($modifiers['forcesize']?forceResize($im,$val):resize($im,$val)); + break; + + case 'rotation': + rotate($im,$val); + break; + + case 'webp': + $type = 'webp'; + break; + + case 'mp4': + + break; + } + } + + $this->saveObjOfImage($im,$newpath,$type); + } + $path = $newpath; + + } + + switch($type) { case 'jpeg': @@ -95,4 +162,30 @@ class ImageController implements ContentController break; } } + + function getObjOfImage($path) + { + return imagecreatefromstring(file_get_contents($path)); + } + + function saveObjOfImage($im,$path,$type) + { + switch($type) + { + case 'jpeg': + case 'jpg': + imagejpeg($im,$path,(defined('JPEG_COMPRESSION')?JPEG_COMPRESSION:90)); + break; + + case 'png': + imagepng($im,$path,(defined('PNG_COMPRESSION')?PNG_COMPRESSION:6)); + break; + + case 'webp': + imagewebp($im,$path,(defined('WEBP_COMPRESSION')?WEBP_COMPRESSION:80)); + break; + } + + return $im; + } } \ No newline at end of file diff --git a/content-controllers/image/resize.php b/content-controllers/image/resize.php new file mode 100644 index 0000000..7bafc75 --- /dev/null +++ b/content-controllers/image/resize.php @@ -0,0 +1,120 @@ +$width?$width:$maxwidth); + $maxheight = ($maxheight>$height?$height:$maxheight); + + + $dst_img = imagecreatetruecolor($maxwidth, $maxheight); + $src_img = $img; + + $palsize = ImageColorsTotal($img); + for ($i = 0; $i < $palsize; $i++) + { + $colors = ImageColorsForIndex($img, $i); + ImageColorAllocate($dst_img, $colors['red'], $colors['green'], $colors['blue']); + } + + imagefill($dst_img, 0, 0, IMG_COLOR_TRANSPARENT); + imagesavealpha($dst_img,true); + imagealphablending($dst_img, true); + + $width_new = $height * $maxwidth / $maxheight; + $height_new = $width * $maxheight / $maxwidth; + //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa + if($width_new > $width){ + //cut point by height + $h_point = (($height - $height_new) / 2); + //copy image + imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $maxwidth, $maxheight, $width, $height_new); + }else{ + //cut point by width + $w_point = (($width - $width_new) / 2); + imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $maxwidth, $maxheight, $width_new, $height); + } + + $img = $dst_img; + } + + /** + * From: https://stackoverflow.com/questions/4590441/php-thumbnail-image-resizing-with-proportions + */ + function resize(&$img,$size) + { + $sd = sizeStringToWidthHeight($size); + $maxwidth = $sd['width']; + $maxheight = $sd['height']; + + $width = imagesx($img); + $height = imagesy($img); + + if(defined('ALLOW_BLOATING') && !ALLOW_BLOATING) + { + if($maxwidth>$width)$maxwidth = $width; + if($maxheight>$height)$maxheight = $height; + } + + if ($height > $width) + { + $ratio = $maxheight / $height; + $newheight = $maxheight; + $newwidth = $width * $ratio; + } + else + { + $ratio = $maxwidth / $width; + $newwidth = $maxwidth; + $newheight = $height * $ratio; + } + + $newimg = imagecreatetruecolor($newwidth,$newheight); + + $palsize = ImageColorsTotal($img); + for ($i = 0; $i < $palsize; $i++) + { + $colors = ImageColorsForIndex($img, $i); + ImageColorAllocate($newimg, $colors['red'], $colors['green'], $colors['blue']); + } + + imagefill($newimg, 0, 0, IMG_COLOR_TRANSPARENT); + imagesavealpha($newimg,true); + imagealphablending($newimg, true); + + imagecopyresampled($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); + + $img = $newimg; + } \ No newline at end of file diff --git a/inc/core.php b/inc/core.php index 4eee809..7859512 100644 --- a/inc/core.php +++ b/inc/core.php @@ -4,6 +4,8 @@ spl_autoload_register('autoload'); //disable output buffering if (ob_get_level()) ob_end_clean(); +error_reporting(E_ALL & ~E_NOTICE); + if(!defined('FFMPEG_BINARY')) define('FFMPEG_BINARY',ROOT.DS.'bin'.DS.'ffmpeg');