From 3c22db4392a052a0f4a21ba39d7eb3a83e2b532f Mon Sep 17 00:00:00 2001 From: Anatolie Diordita Date: Tue, 24 Nov 2015 17:00:53 -0500 Subject: [PATCH 01/20] Added specific HTTP status messages to each error. --- webroot/img.php | 82 ++++++++++++++++++------------ webroot/imgd.php | 130 +++++++++++++++++++++++++++-------------------- webroot/imgp.php | 130 +++++++++++++++++++++++++++-------------------- 3 files changed, 198 insertions(+), 144 deletions(-) diff --git a/webroot/img.php b/webroot/img.php index d0b9be2..ed5114d 100644 --- a/webroot/img.php +++ b/webroot/img.php @@ -16,21 +16,39 @@ $version = "v0.7.7 (2015-10-21)"; * Display error message. * * @param string $msg to display. + * @param int $type of HTTP error to display. * * @return void */ -function errorPage($msg) +function errorPage($msg, $type = 500) { global $mode; - header("HTTP/1.0 500 Internal Server Error"); + switch ($type) { + case 400: + $header = "400 Bad Request"; + break; + case 401: + $header = "401 Unauthorized"; + break; + case 403: + $header = "403 Forbidden"; + break; + case 404: + $header = "404 Not Found"; + break; + default: + $header = "500 Internal Server Error"; + } + + header("HTTP/1.0 $header"); if ($mode == 'development') { die("[img.php] $msg"); } error_log("[img.php] $msg"); - die("HTTP/1.0 500 Internal Server Error"); + die("HTTP/1.0 $header"); } @@ -45,7 +63,7 @@ set_exception_handler(function ($exception) { . "

"
         . $exception->getTraceAsString()
         . "
" - ); + , 500); }); @@ -175,7 +193,7 @@ set_time_limit(20); ini_set('gd.jpeg_ignore_warning', 1); if (!extension_loaded('gd')) { - errorPage("Extension gd is nod loaded."); + errorPage("Extension gd is not loaded.", 500); } // Specific settings for each mode @@ -187,7 +205,7 @@ if ($mode == 'strict') { $verbose = false; $status = false; $verboseFile = false; - + } elseif ($mode == 'production') { error_reporting(-1); @@ -211,7 +229,7 @@ if ($mode == 'strict') { ini_set('log_errors', 0); } else { - errorPage("Unknown mode: $mode"); + errorPage("Unknown mode: $mode", 500); } verbose("mode = $mode"); @@ -260,7 +278,7 @@ if ($pwd) { } if ($pwdAlways && $passwordMatch !== true) { - errorPage("Password required and does not match or exists."); + errorPage("Password required and does not match or exists.", 401); } verbose("password match = $passwordMatch"); @@ -284,9 +302,9 @@ if (!$allowHotlinking) { ; // Always allow when password match verbose("Hotlinking since passwordmatch"); } elseif ($passwordMatch === false) { - errorPage("Hotlinking/leeching not allowed when password missmatch."); + errorPage("Hotlinking/leeching not allowed when password missmatch.", 401); } elseif (!$referer) { - errorPage("Hotlinking/leeching not allowed and referer is missing."); + errorPage("Hotlinking/leeching not allowed and referer is missing.", 403); } elseif (strcmp($serverName, $refererHost) == 0) { ; // Allow when serverName matches refererHost verbose("Hotlinking disallowed but serverName matches refererHost."); @@ -297,11 +315,11 @@ if (!$allowHotlinking) { if ($allowedByWhitelist) { verbose("Hotlinking/leeching allowed by whitelist."); } else { - errorPage("Hotlinking/leeching not allowed by whitelist. Referer: $referer."); + errorPage("Hotlinking/leeching not allowed by whitelist. Referer: $referer.", 403); } } else { - errorPage("Hotlinking/leeching not allowed."); + errorPage("Hotlinking/leeching not allowed.", 403); } } @@ -375,7 +393,7 @@ if (isset($shortcut) * src - the source image file. */ $srcImage = urldecode(get('src')) - or errorPage('Must set src-attribute.'); + or errorPage('Must set src-attribute.', 400); // Check for valid/invalid characters $imagePath = getConfig('image_path', __DIR__ . '/img/'); @@ -388,7 +406,7 @@ $dummyFilename = getConfig('dummy_filename', 'dummy'); $dummyImage = false; preg_match($validFilename, $srcImage) - or errorPage('Filename contains invalid characters.'); + or errorPage('Filename contains invalid characters.', 400); if ($dummyEnabled && $srcImage === $dummyFilename) { @@ -409,13 +427,13 @@ if ($dummyEnabled && $srcImage === $dummyFilename) { or errorPage( 'Source image is not a valid file, check the filename and that a matching file exists on the filesystem.' - ); + , 404); substr_compare($imageDir, $pathToImage, 0, strlen($imageDir)) == 0 or errorPage( 'Security constraint: Source image is not below the directory "image_path" as specified in the config file img_config.php.' - ); + , 500); } verbose("src = $srcImage"); @@ -464,11 +482,11 @@ if (isset($sizes[$newWidth])) { // Support width as % of original width if ($newWidth[strlen($newWidth)-1] == '%') { is_numeric(substr($newWidth, 0, -1)) - or errorPage('Width % not numeric.'); + or errorPage('Width % not numeric.', 400); } else { is_null($newWidth) or ($newWidth > 10 && $newWidth <= $maxWidth) - or errorPage('Width out of range.'); + or errorPage('Width out of range.', 400); } verbose("new width = $newWidth"); @@ -489,11 +507,11 @@ if (isset($sizes[$newHeight])) { // height if ($newHeight[strlen($newHeight)-1] == '%') { is_numeric(substr($newHeight, 0, -1)) - or errorPage('Height % out of range.'); + or errorPage('Height % out of range.', 400); } else { is_null($newHeight) or ($newHeight > 10 && $newHeight <= $maxHeight) - or errorPage('Hight out of range.'); + or errorPage('Height out of range.', 400); } verbose("new height = $newHeight"); @@ -531,7 +549,7 @@ if ($negateAspectRatio) { is_null($aspectRatio) or is_numeric($aspectRatio) - or errorPage('Aspect ratio out of range'); + or errorPage('Aspect ratio out of range', 400); verbose("aspect ratio = $aspectRatio"); @@ -653,7 +671,7 @@ $qualityDefault = getConfig('jpg_quality', null); is_null($quality) or ($quality > 0 and $quality <= 100) - or errorPage('Quality out of range'); + or errorPage('Quality out of range', 400); if (is_null($quality) && !is_null($qualityDefault)) { $quality = $qualityDefault; @@ -671,7 +689,7 @@ $compressDefault = getConfig('png_compression', null); is_null($compress) or ($compress > 0 and $compress <= 9) - or errorPage('Compress out of range'); + or errorPage('Compress out of range', 400); if (is_null($compress) && !is_null($compressDefault)) { $compress = $compressDefault; @@ -697,7 +715,7 @@ $scale = get(array('scale', 's')); is_null($scale) or ($scale >= 0 and $scale <= 400) - or errorPage('Scale out of range'); + or errorPage('Scale out of range', 400); verbose("scale = $scale"); @@ -746,7 +764,7 @@ $rotateBefore = get(array('rotateBefore', 'rotate-before', 'rb')); is_null($rotateBefore) or ($rotateBefore >= -360 and $rotateBefore <= 360) - or errorPage('RotateBefore out of range'); + or errorPage('RotateBefore out of range', 400); verbose("rotateBefore = $rotateBefore"); @@ -759,7 +777,7 @@ $rotateAfter = get(array('rotateAfter', 'rotate-after', 'ra', 'rotate', 'r')); is_null($rotateAfter) or ($rotateAfter >= -360 and $rotateAfter <= 360) - or errorPage('RotateBefore out of range'); + or errorPage('RotateBefore out of range', 400); verbose("rotateAfter = $rotateAfter"); @@ -908,13 +926,13 @@ if ($alias && $aliasPath && $passwordMatch) { $useCache = false; is_writable($aliasPath) - or errorPage("Directory for alias is not writable."); + or errorPage("Directory for alias is not writable.", 500); preg_match($validAliasname, $alias) - or errorPage('Filename for alias contains invalid characters. Do not add extension.'); + or errorPage('Filename for alias contains invalid characters. Do not add extension.', 500); } elseif ($alias) { - errorPage('Alias is not enabled in the config file or password not matching.'); + errorPage('Alias is not enabled in the config file or password not matching.', 500); } verbose("alias = $alias"); @@ -966,7 +984,7 @@ if ($dummyImage === true) { $srcImage = $img->getTarget(); $imagePath = null; - + verbose("src (updated) = $srcImage"); } @@ -1025,7 +1043,7 @@ $hookBeforeCImage = getConfig('hook_before_CImage', null); if (is_callable($hookBeforeCImage)) { verbose("hookBeforeCImage activated"); - + $allConfig = $hookBeforeCImage($img, array( // Options for calculate dimensions 'newWidth' => $newWidth, @@ -1058,7 +1076,7 @@ if (is_callable($hookBeforeCImage)) { // Output format 'outputFormat' => $outputFormat, 'dpr' => $dpr, - + // Other 'postProcessing' => $postProcessing, )); diff --git a/webroot/imgd.php b/webroot/imgd.php index 9537886..8a55b3e 100644 --- a/webroot/imgd.php +++ b/webroot/imgd.php @@ -95,7 +95,7 @@ class CHttpGet public function setUrl($url) { $parts = parse_url($url); - + $path = ""; if (isset($parts['path'])) { $pathParts = explode('/', $parts['path']); @@ -139,7 +139,7 @@ class CHttpGet public function parseHeader() { //$header = explode("\r\n", rtrim($this->response['headerRaw'], "\r\n")); - + $rawHeaders = rtrim($this->response['headerRaw'], "\r\n"); # Handle multiple responses e.g. with redirections (proxies too) $headerGroups = explode("\r\n\r\n", $rawHeaders); @@ -624,7 +624,7 @@ class CRemoteImage $date = strtotime($this->cache['Date']); $maxAge = $this->cache['Max-Age']; $now = time(); - + if ($imageExists && $date + $maxAge > $now) { return $this->fileName; } @@ -687,11 +687,11 @@ class CWhitelist if ($whitelist !== null) { $this->set($whitelist); } - + if (empty($item) or empty($this->whitelist)) { return false; } - + foreach ($this->whitelist as $regexp) { if (preg_match("#$regexp#", $item)) { return true; @@ -791,17 +791,17 @@ class CAsciiArt "customCharacterSet" => null, ); $default = array_merge($default, $options); - + if (!is_null($default['customCharacterSet'])) { $this->addCharacterSet('custom', $default['customCharacterSet']); $default['characterSet'] = 'custom'; } - + $this->scale = $default['scale']; $this->characters = $this->characterSet[$default['characterSet']]; $this->charCount = strlen($this->characters); $this->luminanceStrategy = $default['luminanceStrategy']; - + return $this; } @@ -822,7 +822,7 @@ class CAsciiArt $ascii = null; $incY = $this->scale; $incX = $this->scale / 2; - + for ($y = 0; $y < $height - 1; $y += $incY) { for ($x = 0; $x < $width - 1; $x += $incX) { $toX = min($x + $this->scale / 2, $width - 1); @@ -853,7 +853,7 @@ class CAsciiArt { $numPixels = ($x2 - $x1 + 1) * ($y2 - $y1 + 1); $luminance = 0; - + for ($x = $x1; $x <= $x2; $x++) { for ($y = $y1; $y <= $y2; $y++) { $rgb = imagecolorat($img, $x, $y); @@ -863,7 +863,7 @@ class CAsciiArt $luminance += $this->getLuminance($red, $green, $blue); } } - + return $luminance / $numPixels; } @@ -1545,7 +1545,7 @@ class CImage private function normalizeFileExtension($extension = null) { $extension = strtolower($extension ? $extension : $this->extension); - + if ($extension == 'jpeg') { $extension = 'jpg'; } @@ -1567,7 +1567,7 @@ class CImage if (!$this->isRemoteSourceOnWhitelist($src)) { throw new Exception("Hostname is not on whitelist for remote sources."); } - + $remote = new CRemoteImage(); $cache = $this->saveFolder . "/remote/"; @@ -2241,7 +2241,7 @@ class CImage if ($this->copyStrategy === self::RESIZE) { $copyStrat = "_rs"; } - + $width = $this->newWidth; $height = $this->newHeight; @@ -2292,7 +2292,7 @@ class CImage $subdir = ($subdir == '.') ? '_.' : $subdir; $subdir .= '_'; } - + $file = $subdir . $filename . '_' . $width . '_' . $height . $offset . $crop . $cropToFit . $fillToFit . $crop_x . $crop_y . $upscale @@ -2363,7 +2363,7 @@ class CImage if ($this->image === false) { throw new Exception("Could not load image."); } - + /* Removed v0.7.7 if (image_type_to_mime_type($this->fileType) == 'image/png') { $type = $this->getPngType(); @@ -2403,14 +2403,14 @@ class CImage public function getPngType($filename = null) { $filename = $filename ? $filename : $this->pathToImage; - + $pngType = ord(file_get_contents($filename, false, null, 25, 1)); if ($this->verbose) { $this->log("Checking png type of: " . $filename); $this->log($this->getPngTypeAsString($pngType)); } - + return $pngType; } @@ -2434,7 +2434,7 @@ class CImage $index = imagecolortransparent($this->image); $transparent = null; if ($index != -1) { - $transparent = " (transparent)"; + $transparent = " (transparent)"; } switch ($pngType) { @@ -3141,7 +3141,7 @@ class CImage $index = $this->image ? imagecolortransparent($this->image) : -1; - + if ($index != -1) { imagealphablending($img, true); @@ -3209,8 +3209,8 @@ class CImage return substr(image_type_to_extension($this->fileType), 1); } } - - + + /** * Save image. @@ -3433,7 +3433,7 @@ class CImage $this->log("Content-type: " . $mime); $this->log("Content-length: " . $size); $this->verboseOutput(); - + if (is_null($this->verboseFileName)) { exit; } @@ -3485,7 +3485,7 @@ class CImage $details['memoryPeek'] = round(memory_get_peak_usage()/1024/1024, 3) . " MB" ; $details['memoryCurrent'] = round(memory_get_usage()/1024/1024, 3) . " MB"; $details['memoryLimit'] = ini_get('memory_limit'); - + if (isset($_SERVER['REQUEST_TIME_FLOAT'])) { $details['loadTime'] = (string) round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 3) . "s"; } @@ -3642,21 +3642,39 @@ $version = "v0.7.7 (2015-10-21)"; * Display error message. * * @param string $msg to display. + * @param int $type of HTTP error to display. * * @return void */ -function errorPage($msg) +function errorPage($msg, $type = 500) { global $mode; - header("HTTP/1.0 500 Internal Server Error"); + switch ($type) { + case 400: + $header = "400 Bad Request"; + break; + case 401: + $header = "401 Unauthorized"; + break; + case 403: + $header = "403 Forbidden"; + break; + case 404: + $header = "404 Not Found"; + break; + default: + $header = "500 Internal Server Error"; + } + + header("HTTP/1.0 $header"); if ($mode == 'development') { die("[img.php] $msg"); } error_log("[img.php] $msg"); - die("HTTP/1.0 500 Internal Server Error"); + die("HTTP/1.0 $header"); } @@ -3671,7 +3689,7 @@ set_exception_handler(function ($exception) { . "

"
         . $exception->getTraceAsString()
         . "
" - ); + , 500); }); @@ -3801,7 +3819,7 @@ set_time_limit(20); ini_set('gd.jpeg_ignore_warning', 1); if (!extension_loaded('gd')) { - errorPage("Extension gd is nod loaded."); + errorPage("Extension gd is not loaded.", 500); } // Specific settings for each mode @@ -3813,7 +3831,7 @@ if ($mode == 'strict') { $verbose = false; $status = false; $verboseFile = false; - + } elseif ($mode == 'production') { error_reporting(-1); @@ -3837,7 +3855,7 @@ if ($mode == 'strict') { ini_set('log_errors', 0); } else { - errorPage("Unknown mode: $mode"); + errorPage("Unknown mode: $mode", 500); } verbose("mode = $mode"); @@ -3886,7 +3904,7 @@ if ($pwd) { } if ($pwdAlways && $passwordMatch !== true) { - errorPage("Password required and does not match or exists."); + errorPage("Password required and does not match or exists.", 401); } verbose("password match = $passwordMatch"); @@ -3910,9 +3928,9 @@ if (!$allowHotlinking) { ; // Always allow when password match verbose("Hotlinking since passwordmatch"); } elseif ($passwordMatch === false) { - errorPage("Hotlinking/leeching not allowed when password missmatch."); + errorPage("Hotlinking/leeching not allowed when password missmatch.", 401); } elseif (!$referer) { - errorPage("Hotlinking/leeching not allowed and referer is missing."); + errorPage("Hotlinking/leeching not allowed and referer is missing.", 403); } elseif (strcmp($serverName, $refererHost) == 0) { ; // Allow when serverName matches refererHost verbose("Hotlinking disallowed but serverName matches refererHost."); @@ -3923,11 +3941,11 @@ if (!$allowHotlinking) { if ($allowedByWhitelist) { verbose("Hotlinking/leeching allowed by whitelist."); } else { - errorPage("Hotlinking/leeching not allowed by whitelist. Referer: $referer."); + errorPage("Hotlinking/leeching not allowed by whitelist. Referer: $referer.", 403); } } else { - errorPage("Hotlinking/leeching not allowed."); + errorPage("Hotlinking/leeching not allowed.", 403); } } @@ -4001,7 +4019,7 @@ if (isset($shortcut) * src - the source image file. */ $srcImage = urldecode(get('src')) - or errorPage('Must set src-attribute.'); + or errorPage('Must set src-attribute.', 400); // Check for valid/invalid characters $imagePath = getConfig('image_path', __DIR__ . '/img/'); @@ -4014,7 +4032,7 @@ $dummyFilename = getConfig('dummy_filename', 'dummy'); $dummyImage = false; preg_match($validFilename, $srcImage) - or errorPage('Filename contains invalid characters.'); + or errorPage('Filename contains invalid characters.', 400); if ($dummyEnabled && $srcImage === $dummyFilename) { @@ -4035,13 +4053,13 @@ if ($dummyEnabled && $srcImage === $dummyFilename) { or errorPage( 'Source image is not a valid file, check the filename and that a matching file exists on the filesystem.' - ); + , 404); substr_compare($imageDir, $pathToImage, 0, strlen($imageDir)) == 0 or errorPage( 'Security constraint: Source image is not below the directory "image_path" as specified in the config file img_config.php.' - ); + , 500); } verbose("src = $srcImage"); @@ -4090,11 +4108,11 @@ if (isset($sizes[$newWidth])) { // Support width as % of original width if ($newWidth[strlen($newWidth)-1] == '%') { is_numeric(substr($newWidth, 0, -1)) - or errorPage('Width % not numeric.'); + or errorPage('Width % not numeric.', 400); } else { is_null($newWidth) or ($newWidth > 10 && $newWidth <= $maxWidth) - or errorPage('Width out of range.'); + or errorPage('Width out of range.', 400); } verbose("new width = $newWidth"); @@ -4115,11 +4133,11 @@ if (isset($sizes[$newHeight])) { // height if ($newHeight[strlen($newHeight)-1] == '%') { is_numeric(substr($newHeight, 0, -1)) - or errorPage('Height % out of range.'); + or errorPage('Height % out of range.', 400); } else { is_null($newHeight) or ($newHeight > 10 && $newHeight <= $maxHeight) - or errorPage('Hight out of range.'); + or errorPage('Height out of range.', 400); } verbose("new height = $newHeight"); @@ -4157,7 +4175,7 @@ if ($negateAspectRatio) { is_null($aspectRatio) or is_numeric($aspectRatio) - or errorPage('Aspect ratio out of range'); + or errorPage('Aspect ratio out of range', 400); verbose("aspect ratio = $aspectRatio"); @@ -4279,7 +4297,7 @@ $qualityDefault = getConfig('jpg_quality', null); is_null($quality) or ($quality > 0 and $quality <= 100) - or errorPage('Quality out of range'); + or errorPage('Quality out of range', 400); if (is_null($quality) && !is_null($qualityDefault)) { $quality = $qualityDefault; @@ -4297,7 +4315,7 @@ $compressDefault = getConfig('png_compression', null); is_null($compress) or ($compress > 0 and $compress <= 9) - or errorPage('Compress out of range'); + or errorPage('Compress out of range', 400); if (is_null($compress) && !is_null($compressDefault)) { $compress = $compressDefault; @@ -4323,7 +4341,7 @@ $scale = get(array('scale', 's')); is_null($scale) or ($scale >= 0 and $scale <= 400) - or errorPage('Scale out of range'); + or errorPage('Scale out of range', 400); verbose("scale = $scale"); @@ -4372,7 +4390,7 @@ $rotateBefore = get(array('rotateBefore', 'rotate-before', 'rb')); is_null($rotateBefore) or ($rotateBefore >= -360 and $rotateBefore <= 360) - or errorPage('RotateBefore out of range'); + or errorPage('RotateBefore out of range', 400); verbose("rotateBefore = $rotateBefore"); @@ -4385,7 +4403,7 @@ $rotateAfter = get(array('rotateAfter', 'rotate-after', 'ra', 'rotate', 'r')); is_null($rotateAfter) or ($rotateAfter >= -360 and $rotateAfter <= 360) - or errorPage('RotateBefore out of range'); + or errorPage('RotateBefore out of range', 400); verbose("rotateAfter = $rotateAfter"); @@ -4534,13 +4552,13 @@ if ($alias && $aliasPath && $passwordMatch) { $useCache = false; is_writable($aliasPath) - or errorPage("Directory for alias is not writable."); + or errorPage("Directory for alias is not writable.", 500); preg_match($validAliasname, $alias) - or errorPage('Filename for alias contains invalid characters. Do not add extension.'); + or errorPage('Filename for alias contains invalid characters. Do not add extension.', 500); } elseif ($alias) { - errorPage('Alias is not enabled in the config file or password not matching.'); + errorPage('Alias is not enabled in the config file or password not matching.', 500); } verbose("alias = $alias"); @@ -4592,7 +4610,7 @@ if ($dummyImage === true) { $srcImage = $img->getTarget(); $imagePath = null; - + verbose("src (updated) = $srcImage"); } @@ -4651,7 +4669,7 @@ $hookBeforeCImage = getConfig('hook_before_CImage', null); if (is_callable($hookBeforeCImage)) { verbose("hookBeforeCImage activated"); - + $allConfig = $hookBeforeCImage($img, array( // Options for calculate dimensions 'newWidth' => $newWidth, @@ -4684,7 +4702,7 @@ if (is_callable($hookBeforeCImage)) { // Output format 'outputFormat' => $outputFormat, 'dpr' => $dpr, - + // Other 'postProcessing' => $postProcessing, )); diff --git a/webroot/imgp.php b/webroot/imgp.php index 08975aa..03a45e9 100644 --- a/webroot/imgp.php +++ b/webroot/imgp.php @@ -95,7 +95,7 @@ class CHttpGet public function setUrl($url) { $parts = parse_url($url); - + $path = ""; if (isset($parts['path'])) { $pathParts = explode('/', $parts['path']); @@ -139,7 +139,7 @@ class CHttpGet public function parseHeader() { //$header = explode("\r\n", rtrim($this->response['headerRaw'], "\r\n")); - + $rawHeaders = rtrim($this->response['headerRaw'], "\r\n"); # Handle multiple responses e.g. with redirections (proxies too) $headerGroups = explode("\r\n\r\n", $rawHeaders); @@ -624,7 +624,7 @@ class CRemoteImage $date = strtotime($this->cache['Date']); $maxAge = $this->cache['Max-Age']; $now = time(); - + if ($imageExists && $date + $maxAge > $now) { return $this->fileName; } @@ -687,11 +687,11 @@ class CWhitelist if ($whitelist !== null) { $this->set($whitelist); } - + if (empty($item) or empty($this->whitelist)) { return false; } - + foreach ($this->whitelist as $regexp) { if (preg_match("#$regexp#", $item)) { return true; @@ -791,17 +791,17 @@ class CAsciiArt "customCharacterSet" => null, ); $default = array_merge($default, $options); - + if (!is_null($default['customCharacterSet'])) { $this->addCharacterSet('custom', $default['customCharacterSet']); $default['characterSet'] = 'custom'; } - + $this->scale = $default['scale']; $this->characters = $this->characterSet[$default['characterSet']]; $this->charCount = strlen($this->characters); $this->luminanceStrategy = $default['luminanceStrategy']; - + return $this; } @@ -822,7 +822,7 @@ class CAsciiArt $ascii = null; $incY = $this->scale; $incX = $this->scale / 2; - + for ($y = 0; $y < $height - 1; $y += $incY) { for ($x = 0; $x < $width - 1; $x += $incX) { $toX = min($x + $this->scale / 2, $width - 1); @@ -853,7 +853,7 @@ class CAsciiArt { $numPixels = ($x2 - $x1 + 1) * ($y2 - $y1 + 1); $luminance = 0; - + for ($x = $x1; $x <= $x2; $x++) { for ($y = $y1; $y <= $y2; $y++) { $rgb = imagecolorat($img, $x, $y); @@ -863,7 +863,7 @@ class CAsciiArt $luminance += $this->getLuminance($red, $green, $blue); } } - + return $luminance / $numPixels; } @@ -1545,7 +1545,7 @@ class CImage private function normalizeFileExtension($extension = null) { $extension = strtolower($extension ? $extension : $this->extension); - + if ($extension == 'jpeg') { $extension = 'jpg'; } @@ -1567,7 +1567,7 @@ class CImage if (!$this->isRemoteSourceOnWhitelist($src)) { throw new Exception("Hostname is not on whitelist for remote sources."); } - + $remote = new CRemoteImage(); $cache = $this->saveFolder . "/remote/"; @@ -2241,7 +2241,7 @@ class CImage if ($this->copyStrategy === self::RESIZE) { $copyStrat = "_rs"; } - + $width = $this->newWidth; $height = $this->newHeight; @@ -2292,7 +2292,7 @@ class CImage $subdir = ($subdir == '.') ? '_.' : $subdir; $subdir .= '_'; } - + $file = $subdir . $filename . '_' . $width . '_' . $height . $offset . $crop . $cropToFit . $fillToFit . $crop_x . $crop_y . $upscale @@ -2363,7 +2363,7 @@ class CImage if ($this->image === false) { throw new Exception("Could not load image."); } - + /* Removed v0.7.7 if (image_type_to_mime_type($this->fileType) == 'image/png') { $type = $this->getPngType(); @@ -2403,14 +2403,14 @@ class CImage public function getPngType($filename = null) { $filename = $filename ? $filename : $this->pathToImage; - + $pngType = ord(file_get_contents($filename, false, null, 25, 1)); if ($this->verbose) { $this->log("Checking png type of: " . $filename); $this->log($this->getPngTypeAsString($pngType)); } - + return $pngType; } @@ -2434,7 +2434,7 @@ class CImage $index = imagecolortransparent($this->image); $transparent = null; if ($index != -1) { - $transparent = " (transparent)"; + $transparent = " (transparent)"; } switch ($pngType) { @@ -3141,7 +3141,7 @@ class CImage $index = $this->image ? imagecolortransparent($this->image) : -1; - + if ($index != -1) { imagealphablending($img, true); @@ -3209,8 +3209,8 @@ class CImage return substr(image_type_to_extension($this->fileType), 1); } } - - + + /** * Save image. @@ -3433,7 +3433,7 @@ class CImage $this->log("Content-type: " . $mime); $this->log("Content-length: " . $size); $this->verboseOutput(); - + if (is_null($this->verboseFileName)) { exit; } @@ -3485,7 +3485,7 @@ class CImage $details['memoryPeek'] = round(memory_get_peak_usage()/1024/1024, 3) . " MB" ; $details['memoryCurrent'] = round(memory_get_usage()/1024/1024, 3) . " MB"; $details['memoryLimit'] = ini_get('memory_limit'); - + if (isset($_SERVER['REQUEST_TIME_FLOAT'])) { $details['loadTime'] = (string) round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 3) . "s"; } @@ -3642,21 +3642,39 @@ $version = "v0.7.7 (2015-10-21)"; * Display error message. * * @param string $msg to display. + * @param int $type of HTTP error to display. * * @return void */ -function errorPage($msg) +function errorPage($msg, $type = 500) { global $mode; - header("HTTP/1.0 500 Internal Server Error"); + switch ($type) { + case 400: + $header = "400 Bad Request"; + break; + case 401: + $header = "401 Unauthorized"; + break; + case 403: + $header = "403 Forbidden"; + break; + case 404: + $header = "404 Not Found"; + break; + default: + $header = "500 Internal Server Error"; + } + + header("HTTP/1.0 $header"); if ($mode == 'development') { die("[img.php] $msg"); } error_log("[img.php] $msg"); - die("HTTP/1.0 500 Internal Server Error"); + die("HTTP/1.0 $header"); } @@ -3671,7 +3689,7 @@ set_exception_handler(function ($exception) { . "

"
         . $exception->getTraceAsString()
         . "
" - ); + , 500); }); @@ -3801,7 +3819,7 @@ set_time_limit(20); ini_set('gd.jpeg_ignore_warning', 1); if (!extension_loaded('gd')) { - errorPage("Extension gd is nod loaded."); + errorPage("Extension gd is not loaded.", 500); } // Specific settings for each mode @@ -3813,7 +3831,7 @@ if ($mode == 'strict') { $verbose = false; $status = false; $verboseFile = false; - + } elseif ($mode == 'production') { error_reporting(-1); @@ -3837,7 +3855,7 @@ if ($mode == 'strict') { ini_set('log_errors', 0); } else { - errorPage("Unknown mode: $mode"); + errorPage("Unknown mode: $mode", 500); } verbose("mode = $mode"); @@ -3886,7 +3904,7 @@ if ($pwd) { } if ($pwdAlways && $passwordMatch !== true) { - errorPage("Password required and does not match or exists."); + errorPage("Password required and does not match or exists.", 401); } verbose("password match = $passwordMatch"); @@ -3910,9 +3928,9 @@ if (!$allowHotlinking) { ; // Always allow when password match verbose("Hotlinking since passwordmatch"); } elseif ($passwordMatch === false) { - errorPage("Hotlinking/leeching not allowed when password missmatch."); + errorPage("Hotlinking/leeching not allowed when password missmatch.", 401); } elseif (!$referer) { - errorPage("Hotlinking/leeching not allowed and referer is missing."); + errorPage("Hotlinking/leeching not allowed and referer is missing.", 403); } elseif (strcmp($serverName, $refererHost) == 0) { ; // Allow when serverName matches refererHost verbose("Hotlinking disallowed but serverName matches refererHost."); @@ -3923,11 +3941,11 @@ if (!$allowHotlinking) { if ($allowedByWhitelist) { verbose("Hotlinking/leeching allowed by whitelist."); } else { - errorPage("Hotlinking/leeching not allowed by whitelist. Referer: $referer."); + errorPage("Hotlinking/leeching not allowed by whitelist. Referer: $referer.", 403); } } else { - errorPage("Hotlinking/leeching not allowed."); + errorPage("Hotlinking/leeching not allowed.", 403); } } @@ -4001,7 +4019,7 @@ if (isset($shortcut) * src - the source image file. */ $srcImage = urldecode(get('src')) - or errorPage('Must set src-attribute.'); + or errorPage('Must set src-attribute.', 400); // Check for valid/invalid characters $imagePath = getConfig('image_path', __DIR__ . '/img/'); @@ -4014,7 +4032,7 @@ $dummyFilename = getConfig('dummy_filename', 'dummy'); $dummyImage = false; preg_match($validFilename, $srcImage) - or errorPage('Filename contains invalid characters.'); + or errorPage('Filename contains invalid characters.', 400); if ($dummyEnabled && $srcImage === $dummyFilename) { @@ -4035,13 +4053,13 @@ if ($dummyEnabled && $srcImage === $dummyFilename) { or errorPage( 'Source image is not a valid file, check the filename and that a matching file exists on the filesystem.' - ); + , 404); substr_compare($imageDir, $pathToImage, 0, strlen($imageDir)) == 0 or errorPage( 'Security constraint: Source image is not below the directory "image_path" as specified in the config file img_config.php.' - ); + , 500); } verbose("src = $srcImage"); @@ -4090,11 +4108,11 @@ if (isset($sizes[$newWidth])) { // Support width as % of original width if ($newWidth[strlen($newWidth)-1] == '%') { is_numeric(substr($newWidth, 0, -1)) - or errorPage('Width % not numeric.'); + or errorPage('Width % not numeric.', 400); } else { is_null($newWidth) or ($newWidth > 10 && $newWidth <= $maxWidth) - or errorPage('Width out of range.'); + or errorPage('Width out of range.', 400); } verbose("new width = $newWidth"); @@ -4115,11 +4133,11 @@ if (isset($sizes[$newHeight])) { // height if ($newHeight[strlen($newHeight)-1] == '%') { is_numeric(substr($newHeight, 0, -1)) - or errorPage('Height % out of range.'); + or errorPage('Height % out of range.', 400); } else { is_null($newHeight) or ($newHeight > 10 && $newHeight <= $maxHeight) - or errorPage('Hight out of range.'); + or errorPage('Height out of range.', 400); } verbose("new height = $newHeight"); @@ -4157,7 +4175,7 @@ if ($negateAspectRatio) { is_null($aspectRatio) or is_numeric($aspectRatio) - or errorPage('Aspect ratio out of range'); + or errorPage('Aspect ratio out of range', 400); verbose("aspect ratio = $aspectRatio"); @@ -4279,7 +4297,7 @@ $qualityDefault = getConfig('jpg_quality', null); is_null($quality) or ($quality > 0 and $quality <= 100) - or errorPage('Quality out of range'); + or errorPage('Quality out of range', 400); if (is_null($quality) && !is_null($qualityDefault)) { $quality = $qualityDefault; @@ -4297,7 +4315,7 @@ $compressDefault = getConfig('png_compression', null); is_null($compress) or ($compress > 0 and $compress <= 9) - or errorPage('Compress out of range'); + or errorPage('Compress out of range', 400); if (is_null($compress) && !is_null($compressDefault)) { $compress = $compressDefault; @@ -4323,7 +4341,7 @@ $scale = get(array('scale', 's')); is_null($scale) or ($scale >= 0 and $scale <= 400) - or errorPage('Scale out of range'); + or errorPage('Scale out of range', 400); verbose("scale = $scale"); @@ -4372,7 +4390,7 @@ $rotateBefore = get(array('rotateBefore', 'rotate-before', 'rb')); is_null($rotateBefore) or ($rotateBefore >= -360 and $rotateBefore <= 360) - or errorPage('RotateBefore out of range'); + or errorPage('RotateBefore out of range', 400); verbose("rotateBefore = $rotateBefore"); @@ -4385,7 +4403,7 @@ $rotateAfter = get(array('rotateAfter', 'rotate-after', 'ra', 'rotate', 'r')); is_null($rotateAfter) or ($rotateAfter >= -360 and $rotateAfter <= 360) - or errorPage('RotateBefore out of range'); + or errorPage('RotateBefore out of range', 400); verbose("rotateAfter = $rotateAfter"); @@ -4534,13 +4552,13 @@ if ($alias && $aliasPath && $passwordMatch) { $useCache = false; is_writable($aliasPath) - or errorPage("Directory for alias is not writable."); + or errorPage("Directory for alias is not writable.", 500); preg_match($validAliasname, $alias) - or errorPage('Filename for alias contains invalid characters. Do not add extension.'); + or errorPage('Filename for alias contains invalid characters. Do not add extension.', 500); } elseif ($alias) { - errorPage('Alias is not enabled in the config file or password not matching.'); + errorPage('Alias is not enabled in the config file or password not matching.', 500); } verbose("alias = $alias"); @@ -4592,7 +4610,7 @@ if ($dummyImage === true) { $srcImage = $img->getTarget(); $imagePath = null; - + verbose("src (updated) = $srcImage"); } @@ -4651,7 +4669,7 @@ $hookBeforeCImage = getConfig('hook_before_CImage', null); if (is_callable($hookBeforeCImage)) { verbose("hookBeforeCImage activated"); - + $allConfig = $hookBeforeCImage($img, array( // Options for calculate dimensions 'newWidth' => $newWidth, @@ -4684,7 +4702,7 @@ if (is_callable($hookBeforeCImage)) { // Output format 'outputFormat' => $outputFormat, 'dpr' => $dpr, - + // Other 'postProcessing' => $postProcessing, )); From d7b34a64880bb737893bbdb936520bcf973dec4e Mon Sep 17 00:00:00 2001 From: Anatolie Diordita Date: Sun, 29 Nov 2015 15:23:39 -0500 Subject: [PATCH 02/20] Updated to reflect changes proposed in issue #127 --- webroot/img.php | 42 +++++++-------- webroot/imgd.php | 130 ++++++++++++++++++++--------------------------- webroot/imgp.php | 130 ++++++++++++++++++++--------------------------- webroot/imgs.php | 0 4 files changed, 130 insertions(+), 172 deletions(-) mode change 100644 => 100755 webroot/imgd.php mode change 100644 => 100755 webroot/imgp.php mode change 100644 => 100755 webroot/imgs.php diff --git a/webroot/img.php b/webroot/img.php index ed5114d..72e6bea 100644 --- a/webroot/img.php +++ b/webroot/img.php @@ -25,12 +25,6 @@ function errorPage($msg, $type = 500) global $mode; switch ($type) { - case 400: - $header = "400 Bad Request"; - break; - case 401: - $header = "401 Unauthorized"; - break; case 403: $header = "403 Forbidden"; break; @@ -278,7 +272,7 @@ if ($pwd) { } if ($pwdAlways && $passwordMatch !== true) { - errorPage("Password required and does not match or exists.", 401); + errorPage("Password required and does not match or exists.", 403); } verbose("password match = $passwordMatch"); @@ -302,7 +296,7 @@ if (!$allowHotlinking) { ; // Always allow when password match verbose("Hotlinking since passwordmatch"); } elseif ($passwordMatch === false) { - errorPage("Hotlinking/leeching not allowed when password missmatch.", 401); + errorPage("Hotlinking/leeching not allowed when password missmatch.", 403); } elseif (!$referer) { errorPage("Hotlinking/leeching not allowed and referer is missing.", 403); } elseif (strcmp($serverName, $refererHost) == 0) { @@ -393,7 +387,7 @@ if (isset($shortcut) * src - the source image file. */ $srcImage = urldecode(get('src')) - or errorPage('Must set src-attribute.', 400); + or errorPage('Must set src-attribute.', 404); // Check for valid/invalid characters $imagePath = getConfig('image_path', __DIR__ . '/img/'); @@ -406,7 +400,7 @@ $dummyFilename = getConfig('dummy_filename', 'dummy'); $dummyImage = false; preg_match($validFilename, $srcImage) - or errorPage('Filename contains invalid characters.', 400); + or errorPage('Filename contains invalid characters.', 404); if ($dummyEnabled && $srcImage === $dummyFilename) { @@ -433,7 +427,7 @@ if ($dummyEnabled && $srcImage === $dummyFilename) { or errorPage( 'Security constraint: Source image is not below the directory "image_path" as specified in the config file img_config.php.' - , 500); + , 404); } verbose("src = $srcImage"); @@ -482,11 +476,11 @@ if (isset($sizes[$newWidth])) { // Support width as % of original width if ($newWidth[strlen($newWidth)-1] == '%') { is_numeric(substr($newWidth, 0, -1)) - or errorPage('Width % not numeric.', 400); + or errorPage('Width % not numeric.', 404); } else { is_null($newWidth) or ($newWidth > 10 && $newWidth <= $maxWidth) - or errorPage('Width out of range.', 400); + or errorPage('Width out of range.', 404); } verbose("new width = $newWidth"); @@ -507,11 +501,11 @@ if (isset($sizes[$newHeight])) { // height if ($newHeight[strlen($newHeight)-1] == '%') { is_numeric(substr($newHeight, 0, -1)) - or errorPage('Height % out of range.', 400); + or errorPage('Height % out of range.', 404); } else { is_null($newHeight) or ($newHeight > 10 && $newHeight <= $maxHeight) - or errorPage('Height out of range.', 400); + or errorPage('Height out of range.', 404); } verbose("new height = $newHeight"); @@ -549,7 +543,7 @@ if ($negateAspectRatio) { is_null($aspectRatio) or is_numeric($aspectRatio) - or errorPage('Aspect ratio out of range', 400); + or errorPage('Aspect ratio out of range', 404); verbose("aspect ratio = $aspectRatio"); @@ -671,7 +665,7 @@ $qualityDefault = getConfig('jpg_quality', null); is_null($quality) or ($quality > 0 and $quality <= 100) - or errorPage('Quality out of range', 400); + or errorPage('Quality out of range', 404); if (is_null($quality) && !is_null($qualityDefault)) { $quality = $qualityDefault; @@ -689,7 +683,7 @@ $compressDefault = getConfig('png_compression', null); is_null($compress) or ($compress > 0 and $compress <= 9) - or errorPage('Compress out of range', 400); + or errorPage('Compress out of range', 404); if (is_null($compress) && !is_null($compressDefault)) { $compress = $compressDefault; @@ -715,7 +709,7 @@ $scale = get(array('scale', 's')); is_null($scale) or ($scale >= 0 and $scale <= 400) - or errorPage('Scale out of range', 400); + or errorPage('Scale out of range', 404); verbose("scale = $scale"); @@ -764,7 +758,7 @@ $rotateBefore = get(array('rotateBefore', 'rotate-before', 'rb')); is_null($rotateBefore) or ($rotateBefore >= -360 and $rotateBefore <= 360) - or errorPage('RotateBefore out of range', 400); + or errorPage('RotateBefore out of range', 404); verbose("rotateBefore = $rotateBefore"); @@ -777,7 +771,7 @@ $rotateAfter = get(array('rotateAfter', 'rotate-after', 'ra', 'rotate', 'r')); is_null($rotateAfter) or ($rotateAfter >= -360 and $rotateAfter <= 360) - or errorPage('RotateBefore out of range', 400); + or errorPage('RotateBefore out of range', 404); verbose("rotateAfter = $rotateAfter"); @@ -926,13 +920,13 @@ if ($alias && $aliasPath && $passwordMatch) { $useCache = false; is_writable($aliasPath) - or errorPage("Directory for alias is not writable.", 500); + or errorPage("Directory for alias is not writable.", 403); preg_match($validAliasname, $alias) - or errorPage('Filename for alias contains invalid characters. Do not add extension.', 500); + or errorPage('Filename for alias contains invalid characters. Do not add extension.', 404); } elseif ($alias) { - errorPage('Alias is not enabled in the config file or password not matching.', 500); + errorPage('Alias is not enabled in the config file or password not matching.', 403); } verbose("alias = $alias"); diff --git a/webroot/imgd.php b/webroot/imgd.php old mode 100644 new mode 100755 index 8a55b3e..9537886 --- a/webroot/imgd.php +++ b/webroot/imgd.php @@ -95,7 +95,7 @@ class CHttpGet public function setUrl($url) { $parts = parse_url($url); - + $path = ""; if (isset($parts['path'])) { $pathParts = explode('/', $parts['path']); @@ -139,7 +139,7 @@ class CHttpGet public function parseHeader() { //$header = explode("\r\n", rtrim($this->response['headerRaw'], "\r\n")); - + $rawHeaders = rtrim($this->response['headerRaw'], "\r\n"); # Handle multiple responses e.g. with redirections (proxies too) $headerGroups = explode("\r\n\r\n", $rawHeaders); @@ -624,7 +624,7 @@ class CRemoteImage $date = strtotime($this->cache['Date']); $maxAge = $this->cache['Max-Age']; $now = time(); - + if ($imageExists && $date + $maxAge > $now) { return $this->fileName; } @@ -687,11 +687,11 @@ class CWhitelist if ($whitelist !== null) { $this->set($whitelist); } - + if (empty($item) or empty($this->whitelist)) { return false; } - + foreach ($this->whitelist as $regexp) { if (preg_match("#$regexp#", $item)) { return true; @@ -791,17 +791,17 @@ class CAsciiArt "customCharacterSet" => null, ); $default = array_merge($default, $options); - + if (!is_null($default['customCharacterSet'])) { $this->addCharacterSet('custom', $default['customCharacterSet']); $default['characterSet'] = 'custom'; } - + $this->scale = $default['scale']; $this->characters = $this->characterSet[$default['characterSet']]; $this->charCount = strlen($this->characters); $this->luminanceStrategy = $default['luminanceStrategy']; - + return $this; } @@ -822,7 +822,7 @@ class CAsciiArt $ascii = null; $incY = $this->scale; $incX = $this->scale / 2; - + for ($y = 0; $y < $height - 1; $y += $incY) { for ($x = 0; $x < $width - 1; $x += $incX) { $toX = min($x + $this->scale / 2, $width - 1); @@ -853,7 +853,7 @@ class CAsciiArt { $numPixels = ($x2 - $x1 + 1) * ($y2 - $y1 + 1); $luminance = 0; - + for ($x = $x1; $x <= $x2; $x++) { for ($y = $y1; $y <= $y2; $y++) { $rgb = imagecolorat($img, $x, $y); @@ -863,7 +863,7 @@ class CAsciiArt $luminance += $this->getLuminance($red, $green, $blue); } } - + return $luminance / $numPixels; } @@ -1545,7 +1545,7 @@ class CImage private function normalizeFileExtension($extension = null) { $extension = strtolower($extension ? $extension : $this->extension); - + if ($extension == 'jpeg') { $extension = 'jpg'; } @@ -1567,7 +1567,7 @@ class CImage if (!$this->isRemoteSourceOnWhitelist($src)) { throw new Exception("Hostname is not on whitelist for remote sources."); } - + $remote = new CRemoteImage(); $cache = $this->saveFolder . "/remote/"; @@ -2241,7 +2241,7 @@ class CImage if ($this->copyStrategy === self::RESIZE) { $copyStrat = "_rs"; } - + $width = $this->newWidth; $height = $this->newHeight; @@ -2292,7 +2292,7 @@ class CImage $subdir = ($subdir == '.') ? '_.' : $subdir; $subdir .= '_'; } - + $file = $subdir . $filename . '_' . $width . '_' . $height . $offset . $crop . $cropToFit . $fillToFit . $crop_x . $crop_y . $upscale @@ -2363,7 +2363,7 @@ class CImage if ($this->image === false) { throw new Exception("Could not load image."); } - + /* Removed v0.7.7 if (image_type_to_mime_type($this->fileType) == 'image/png') { $type = $this->getPngType(); @@ -2403,14 +2403,14 @@ class CImage public function getPngType($filename = null) { $filename = $filename ? $filename : $this->pathToImage; - + $pngType = ord(file_get_contents($filename, false, null, 25, 1)); if ($this->verbose) { $this->log("Checking png type of: " . $filename); $this->log($this->getPngTypeAsString($pngType)); } - + return $pngType; } @@ -2434,7 +2434,7 @@ class CImage $index = imagecolortransparent($this->image); $transparent = null; if ($index != -1) { - $transparent = " (transparent)"; + $transparent = " (transparent)"; } switch ($pngType) { @@ -3141,7 +3141,7 @@ class CImage $index = $this->image ? imagecolortransparent($this->image) : -1; - + if ($index != -1) { imagealphablending($img, true); @@ -3209,8 +3209,8 @@ class CImage return substr(image_type_to_extension($this->fileType), 1); } } - - + + /** * Save image. @@ -3433,7 +3433,7 @@ class CImage $this->log("Content-type: " . $mime); $this->log("Content-length: " . $size); $this->verboseOutput(); - + if (is_null($this->verboseFileName)) { exit; } @@ -3485,7 +3485,7 @@ class CImage $details['memoryPeek'] = round(memory_get_peak_usage()/1024/1024, 3) . " MB" ; $details['memoryCurrent'] = round(memory_get_usage()/1024/1024, 3) . " MB"; $details['memoryLimit'] = ini_get('memory_limit'); - + if (isset($_SERVER['REQUEST_TIME_FLOAT'])) { $details['loadTime'] = (string) round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 3) . "s"; } @@ -3642,39 +3642,21 @@ $version = "v0.7.7 (2015-10-21)"; * Display error message. * * @param string $msg to display. - * @param int $type of HTTP error to display. * * @return void */ -function errorPage($msg, $type = 500) +function errorPage($msg) { global $mode; - switch ($type) { - case 400: - $header = "400 Bad Request"; - break; - case 401: - $header = "401 Unauthorized"; - break; - case 403: - $header = "403 Forbidden"; - break; - case 404: - $header = "404 Not Found"; - break; - default: - $header = "500 Internal Server Error"; - } - - header("HTTP/1.0 $header"); + header("HTTP/1.0 500 Internal Server Error"); if ($mode == 'development') { die("[img.php] $msg"); } error_log("[img.php] $msg"); - die("HTTP/1.0 $header"); + die("HTTP/1.0 500 Internal Server Error"); } @@ -3689,7 +3671,7 @@ set_exception_handler(function ($exception) { . "

"
         . $exception->getTraceAsString()
         . "
" - , 500); + ); }); @@ -3819,7 +3801,7 @@ set_time_limit(20); ini_set('gd.jpeg_ignore_warning', 1); if (!extension_loaded('gd')) { - errorPage("Extension gd is not loaded.", 500); + errorPage("Extension gd is nod loaded."); } // Specific settings for each mode @@ -3831,7 +3813,7 @@ if ($mode == 'strict') { $verbose = false; $status = false; $verboseFile = false; - + } elseif ($mode == 'production') { error_reporting(-1); @@ -3855,7 +3837,7 @@ if ($mode == 'strict') { ini_set('log_errors', 0); } else { - errorPage("Unknown mode: $mode", 500); + errorPage("Unknown mode: $mode"); } verbose("mode = $mode"); @@ -3904,7 +3886,7 @@ if ($pwd) { } if ($pwdAlways && $passwordMatch !== true) { - errorPage("Password required and does not match or exists.", 401); + errorPage("Password required and does not match or exists."); } verbose("password match = $passwordMatch"); @@ -3928,9 +3910,9 @@ if (!$allowHotlinking) { ; // Always allow when password match verbose("Hotlinking since passwordmatch"); } elseif ($passwordMatch === false) { - errorPage("Hotlinking/leeching not allowed when password missmatch.", 401); + errorPage("Hotlinking/leeching not allowed when password missmatch."); } elseif (!$referer) { - errorPage("Hotlinking/leeching not allowed and referer is missing.", 403); + errorPage("Hotlinking/leeching not allowed and referer is missing."); } elseif (strcmp($serverName, $refererHost) == 0) { ; // Allow when serverName matches refererHost verbose("Hotlinking disallowed but serverName matches refererHost."); @@ -3941,11 +3923,11 @@ if (!$allowHotlinking) { if ($allowedByWhitelist) { verbose("Hotlinking/leeching allowed by whitelist."); } else { - errorPage("Hotlinking/leeching not allowed by whitelist. Referer: $referer.", 403); + errorPage("Hotlinking/leeching not allowed by whitelist. Referer: $referer."); } } else { - errorPage("Hotlinking/leeching not allowed.", 403); + errorPage("Hotlinking/leeching not allowed."); } } @@ -4019,7 +4001,7 @@ if (isset($shortcut) * src - the source image file. */ $srcImage = urldecode(get('src')) - or errorPage('Must set src-attribute.', 400); + or errorPage('Must set src-attribute.'); // Check for valid/invalid characters $imagePath = getConfig('image_path', __DIR__ . '/img/'); @@ -4032,7 +4014,7 @@ $dummyFilename = getConfig('dummy_filename', 'dummy'); $dummyImage = false; preg_match($validFilename, $srcImage) - or errorPage('Filename contains invalid characters.', 400); + or errorPage('Filename contains invalid characters.'); if ($dummyEnabled && $srcImage === $dummyFilename) { @@ -4053,13 +4035,13 @@ if ($dummyEnabled && $srcImage === $dummyFilename) { or errorPage( 'Source image is not a valid file, check the filename and that a matching file exists on the filesystem.' - , 404); + ); substr_compare($imageDir, $pathToImage, 0, strlen($imageDir)) == 0 or errorPage( 'Security constraint: Source image is not below the directory "image_path" as specified in the config file img_config.php.' - , 500); + ); } verbose("src = $srcImage"); @@ -4108,11 +4090,11 @@ if (isset($sizes[$newWidth])) { // Support width as % of original width if ($newWidth[strlen($newWidth)-1] == '%') { is_numeric(substr($newWidth, 0, -1)) - or errorPage('Width % not numeric.', 400); + or errorPage('Width % not numeric.'); } else { is_null($newWidth) or ($newWidth > 10 && $newWidth <= $maxWidth) - or errorPage('Width out of range.', 400); + or errorPage('Width out of range.'); } verbose("new width = $newWidth"); @@ -4133,11 +4115,11 @@ if (isset($sizes[$newHeight])) { // height if ($newHeight[strlen($newHeight)-1] == '%') { is_numeric(substr($newHeight, 0, -1)) - or errorPage('Height % out of range.', 400); + or errorPage('Height % out of range.'); } else { is_null($newHeight) or ($newHeight > 10 && $newHeight <= $maxHeight) - or errorPage('Height out of range.', 400); + or errorPage('Hight out of range.'); } verbose("new height = $newHeight"); @@ -4175,7 +4157,7 @@ if ($negateAspectRatio) { is_null($aspectRatio) or is_numeric($aspectRatio) - or errorPage('Aspect ratio out of range', 400); + or errorPage('Aspect ratio out of range'); verbose("aspect ratio = $aspectRatio"); @@ -4297,7 +4279,7 @@ $qualityDefault = getConfig('jpg_quality', null); is_null($quality) or ($quality > 0 and $quality <= 100) - or errorPage('Quality out of range', 400); + or errorPage('Quality out of range'); if (is_null($quality) && !is_null($qualityDefault)) { $quality = $qualityDefault; @@ -4315,7 +4297,7 @@ $compressDefault = getConfig('png_compression', null); is_null($compress) or ($compress > 0 and $compress <= 9) - or errorPage('Compress out of range', 400); + or errorPage('Compress out of range'); if (is_null($compress) && !is_null($compressDefault)) { $compress = $compressDefault; @@ -4341,7 +4323,7 @@ $scale = get(array('scale', 's')); is_null($scale) or ($scale >= 0 and $scale <= 400) - or errorPage('Scale out of range', 400); + or errorPage('Scale out of range'); verbose("scale = $scale"); @@ -4390,7 +4372,7 @@ $rotateBefore = get(array('rotateBefore', 'rotate-before', 'rb')); is_null($rotateBefore) or ($rotateBefore >= -360 and $rotateBefore <= 360) - or errorPage('RotateBefore out of range', 400); + or errorPage('RotateBefore out of range'); verbose("rotateBefore = $rotateBefore"); @@ -4403,7 +4385,7 @@ $rotateAfter = get(array('rotateAfter', 'rotate-after', 'ra', 'rotate', 'r')); is_null($rotateAfter) or ($rotateAfter >= -360 and $rotateAfter <= 360) - or errorPage('RotateBefore out of range', 400); + or errorPage('RotateBefore out of range'); verbose("rotateAfter = $rotateAfter"); @@ -4552,13 +4534,13 @@ if ($alias && $aliasPath && $passwordMatch) { $useCache = false; is_writable($aliasPath) - or errorPage("Directory for alias is not writable.", 500); + or errorPage("Directory for alias is not writable."); preg_match($validAliasname, $alias) - or errorPage('Filename for alias contains invalid characters. Do not add extension.', 500); + or errorPage('Filename for alias contains invalid characters. Do not add extension.'); } elseif ($alias) { - errorPage('Alias is not enabled in the config file or password not matching.', 500); + errorPage('Alias is not enabled in the config file or password not matching.'); } verbose("alias = $alias"); @@ -4610,7 +4592,7 @@ if ($dummyImage === true) { $srcImage = $img->getTarget(); $imagePath = null; - + verbose("src (updated) = $srcImage"); } @@ -4669,7 +4651,7 @@ $hookBeforeCImage = getConfig('hook_before_CImage', null); if (is_callable($hookBeforeCImage)) { verbose("hookBeforeCImage activated"); - + $allConfig = $hookBeforeCImage($img, array( // Options for calculate dimensions 'newWidth' => $newWidth, @@ -4702,7 +4684,7 @@ if (is_callable($hookBeforeCImage)) { // Output format 'outputFormat' => $outputFormat, 'dpr' => $dpr, - + // Other 'postProcessing' => $postProcessing, )); diff --git a/webroot/imgp.php b/webroot/imgp.php old mode 100644 new mode 100755 index 03a45e9..08975aa --- a/webroot/imgp.php +++ b/webroot/imgp.php @@ -95,7 +95,7 @@ class CHttpGet public function setUrl($url) { $parts = parse_url($url); - + $path = ""; if (isset($parts['path'])) { $pathParts = explode('/', $parts['path']); @@ -139,7 +139,7 @@ class CHttpGet public function parseHeader() { //$header = explode("\r\n", rtrim($this->response['headerRaw'], "\r\n")); - + $rawHeaders = rtrim($this->response['headerRaw'], "\r\n"); # Handle multiple responses e.g. with redirections (proxies too) $headerGroups = explode("\r\n\r\n", $rawHeaders); @@ -624,7 +624,7 @@ class CRemoteImage $date = strtotime($this->cache['Date']); $maxAge = $this->cache['Max-Age']; $now = time(); - + if ($imageExists && $date + $maxAge > $now) { return $this->fileName; } @@ -687,11 +687,11 @@ class CWhitelist if ($whitelist !== null) { $this->set($whitelist); } - + if (empty($item) or empty($this->whitelist)) { return false; } - + foreach ($this->whitelist as $regexp) { if (preg_match("#$regexp#", $item)) { return true; @@ -791,17 +791,17 @@ class CAsciiArt "customCharacterSet" => null, ); $default = array_merge($default, $options); - + if (!is_null($default['customCharacterSet'])) { $this->addCharacterSet('custom', $default['customCharacterSet']); $default['characterSet'] = 'custom'; } - + $this->scale = $default['scale']; $this->characters = $this->characterSet[$default['characterSet']]; $this->charCount = strlen($this->characters); $this->luminanceStrategy = $default['luminanceStrategy']; - + return $this; } @@ -822,7 +822,7 @@ class CAsciiArt $ascii = null; $incY = $this->scale; $incX = $this->scale / 2; - + for ($y = 0; $y < $height - 1; $y += $incY) { for ($x = 0; $x < $width - 1; $x += $incX) { $toX = min($x + $this->scale / 2, $width - 1); @@ -853,7 +853,7 @@ class CAsciiArt { $numPixels = ($x2 - $x1 + 1) * ($y2 - $y1 + 1); $luminance = 0; - + for ($x = $x1; $x <= $x2; $x++) { for ($y = $y1; $y <= $y2; $y++) { $rgb = imagecolorat($img, $x, $y); @@ -863,7 +863,7 @@ class CAsciiArt $luminance += $this->getLuminance($red, $green, $blue); } } - + return $luminance / $numPixels; } @@ -1545,7 +1545,7 @@ class CImage private function normalizeFileExtension($extension = null) { $extension = strtolower($extension ? $extension : $this->extension); - + if ($extension == 'jpeg') { $extension = 'jpg'; } @@ -1567,7 +1567,7 @@ class CImage if (!$this->isRemoteSourceOnWhitelist($src)) { throw new Exception("Hostname is not on whitelist for remote sources."); } - + $remote = new CRemoteImage(); $cache = $this->saveFolder . "/remote/"; @@ -2241,7 +2241,7 @@ class CImage if ($this->copyStrategy === self::RESIZE) { $copyStrat = "_rs"; } - + $width = $this->newWidth; $height = $this->newHeight; @@ -2292,7 +2292,7 @@ class CImage $subdir = ($subdir == '.') ? '_.' : $subdir; $subdir .= '_'; } - + $file = $subdir . $filename . '_' . $width . '_' . $height . $offset . $crop . $cropToFit . $fillToFit . $crop_x . $crop_y . $upscale @@ -2363,7 +2363,7 @@ class CImage if ($this->image === false) { throw new Exception("Could not load image."); } - + /* Removed v0.7.7 if (image_type_to_mime_type($this->fileType) == 'image/png') { $type = $this->getPngType(); @@ -2403,14 +2403,14 @@ class CImage public function getPngType($filename = null) { $filename = $filename ? $filename : $this->pathToImage; - + $pngType = ord(file_get_contents($filename, false, null, 25, 1)); if ($this->verbose) { $this->log("Checking png type of: " . $filename); $this->log($this->getPngTypeAsString($pngType)); } - + return $pngType; } @@ -2434,7 +2434,7 @@ class CImage $index = imagecolortransparent($this->image); $transparent = null; if ($index != -1) { - $transparent = " (transparent)"; + $transparent = " (transparent)"; } switch ($pngType) { @@ -3141,7 +3141,7 @@ class CImage $index = $this->image ? imagecolortransparent($this->image) : -1; - + if ($index != -1) { imagealphablending($img, true); @@ -3209,8 +3209,8 @@ class CImage return substr(image_type_to_extension($this->fileType), 1); } } - - + + /** * Save image. @@ -3433,7 +3433,7 @@ class CImage $this->log("Content-type: " . $mime); $this->log("Content-length: " . $size); $this->verboseOutput(); - + if (is_null($this->verboseFileName)) { exit; } @@ -3485,7 +3485,7 @@ class CImage $details['memoryPeek'] = round(memory_get_peak_usage()/1024/1024, 3) . " MB" ; $details['memoryCurrent'] = round(memory_get_usage()/1024/1024, 3) . " MB"; $details['memoryLimit'] = ini_get('memory_limit'); - + if (isset($_SERVER['REQUEST_TIME_FLOAT'])) { $details['loadTime'] = (string) round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 3) . "s"; } @@ -3642,39 +3642,21 @@ $version = "v0.7.7 (2015-10-21)"; * Display error message. * * @param string $msg to display. - * @param int $type of HTTP error to display. * * @return void */ -function errorPage($msg, $type = 500) +function errorPage($msg) { global $mode; - switch ($type) { - case 400: - $header = "400 Bad Request"; - break; - case 401: - $header = "401 Unauthorized"; - break; - case 403: - $header = "403 Forbidden"; - break; - case 404: - $header = "404 Not Found"; - break; - default: - $header = "500 Internal Server Error"; - } - - header("HTTP/1.0 $header"); + header("HTTP/1.0 500 Internal Server Error"); if ($mode == 'development') { die("[img.php] $msg"); } error_log("[img.php] $msg"); - die("HTTP/1.0 $header"); + die("HTTP/1.0 500 Internal Server Error"); } @@ -3689,7 +3671,7 @@ set_exception_handler(function ($exception) { . "

"
         . $exception->getTraceAsString()
         . "
" - , 500); + ); }); @@ -3819,7 +3801,7 @@ set_time_limit(20); ini_set('gd.jpeg_ignore_warning', 1); if (!extension_loaded('gd')) { - errorPage("Extension gd is not loaded.", 500); + errorPage("Extension gd is nod loaded."); } // Specific settings for each mode @@ -3831,7 +3813,7 @@ if ($mode == 'strict') { $verbose = false; $status = false; $verboseFile = false; - + } elseif ($mode == 'production') { error_reporting(-1); @@ -3855,7 +3837,7 @@ if ($mode == 'strict') { ini_set('log_errors', 0); } else { - errorPage("Unknown mode: $mode", 500); + errorPage("Unknown mode: $mode"); } verbose("mode = $mode"); @@ -3904,7 +3886,7 @@ if ($pwd) { } if ($pwdAlways && $passwordMatch !== true) { - errorPage("Password required and does not match or exists.", 401); + errorPage("Password required and does not match or exists."); } verbose("password match = $passwordMatch"); @@ -3928,9 +3910,9 @@ if (!$allowHotlinking) { ; // Always allow when password match verbose("Hotlinking since passwordmatch"); } elseif ($passwordMatch === false) { - errorPage("Hotlinking/leeching not allowed when password missmatch.", 401); + errorPage("Hotlinking/leeching not allowed when password missmatch."); } elseif (!$referer) { - errorPage("Hotlinking/leeching not allowed and referer is missing.", 403); + errorPage("Hotlinking/leeching not allowed and referer is missing."); } elseif (strcmp($serverName, $refererHost) == 0) { ; // Allow when serverName matches refererHost verbose("Hotlinking disallowed but serverName matches refererHost."); @@ -3941,11 +3923,11 @@ if (!$allowHotlinking) { if ($allowedByWhitelist) { verbose("Hotlinking/leeching allowed by whitelist."); } else { - errorPage("Hotlinking/leeching not allowed by whitelist. Referer: $referer.", 403); + errorPage("Hotlinking/leeching not allowed by whitelist. Referer: $referer."); } } else { - errorPage("Hotlinking/leeching not allowed.", 403); + errorPage("Hotlinking/leeching not allowed."); } } @@ -4019,7 +4001,7 @@ if (isset($shortcut) * src - the source image file. */ $srcImage = urldecode(get('src')) - or errorPage('Must set src-attribute.', 400); + or errorPage('Must set src-attribute.'); // Check for valid/invalid characters $imagePath = getConfig('image_path', __DIR__ . '/img/'); @@ -4032,7 +4014,7 @@ $dummyFilename = getConfig('dummy_filename', 'dummy'); $dummyImage = false; preg_match($validFilename, $srcImage) - or errorPage('Filename contains invalid characters.', 400); + or errorPage('Filename contains invalid characters.'); if ($dummyEnabled && $srcImage === $dummyFilename) { @@ -4053,13 +4035,13 @@ if ($dummyEnabled && $srcImage === $dummyFilename) { or errorPage( 'Source image is not a valid file, check the filename and that a matching file exists on the filesystem.' - , 404); + ); substr_compare($imageDir, $pathToImage, 0, strlen($imageDir)) == 0 or errorPage( 'Security constraint: Source image is not below the directory "image_path" as specified in the config file img_config.php.' - , 500); + ); } verbose("src = $srcImage"); @@ -4108,11 +4090,11 @@ if (isset($sizes[$newWidth])) { // Support width as % of original width if ($newWidth[strlen($newWidth)-1] == '%') { is_numeric(substr($newWidth, 0, -1)) - or errorPage('Width % not numeric.', 400); + or errorPage('Width % not numeric.'); } else { is_null($newWidth) or ($newWidth > 10 && $newWidth <= $maxWidth) - or errorPage('Width out of range.', 400); + or errorPage('Width out of range.'); } verbose("new width = $newWidth"); @@ -4133,11 +4115,11 @@ if (isset($sizes[$newHeight])) { // height if ($newHeight[strlen($newHeight)-1] == '%') { is_numeric(substr($newHeight, 0, -1)) - or errorPage('Height % out of range.', 400); + or errorPage('Height % out of range.'); } else { is_null($newHeight) or ($newHeight > 10 && $newHeight <= $maxHeight) - or errorPage('Height out of range.', 400); + or errorPage('Hight out of range.'); } verbose("new height = $newHeight"); @@ -4175,7 +4157,7 @@ if ($negateAspectRatio) { is_null($aspectRatio) or is_numeric($aspectRatio) - or errorPage('Aspect ratio out of range', 400); + or errorPage('Aspect ratio out of range'); verbose("aspect ratio = $aspectRatio"); @@ -4297,7 +4279,7 @@ $qualityDefault = getConfig('jpg_quality', null); is_null($quality) or ($quality > 0 and $quality <= 100) - or errorPage('Quality out of range', 400); + or errorPage('Quality out of range'); if (is_null($quality) && !is_null($qualityDefault)) { $quality = $qualityDefault; @@ -4315,7 +4297,7 @@ $compressDefault = getConfig('png_compression', null); is_null($compress) or ($compress > 0 and $compress <= 9) - or errorPage('Compress out of range', 400); + or errorPage('Compress out of range'); if (is_null($compress) && !is_null($compressDefault)) { $compress = $compressDefault; @@ -4341,7 +4323,7 @@ $scale = get(array('scale', 's')); is_null($scale) or ($scale >= 0 and $scale <= 400) - or errorPage('Scale out of range', 400); + or errorPage('Scale out of range'); verbose("scale = $scale"); @@ -4390,7 +4372,7 @@ $rotateBefore = get(array('rotateBefore', 'rotate-before', 'rb')); is_null($rotateBefore) or ($rotateBefore >= -360 and $rotateBefore <= 360) - or errorPage('RotateBefore out of range', 400); + or errorPage('RotateBefore out of range'); verbose("rotateBefore = $rotateBefore"); @@ -4403,7 +4385,7 @@ $rotateAfter = get(array('rotateAfter', 'rotate-after', 'ra', 'rotate', 'r')); is_null($rotateAfter) or ($rotateAfter >= -360 and $rotateAfter <= 360) - or errorPage('RotateBefore out of range', 400); + or errorPage('RotateBefore out of range'); verbose("rotateAfter = $rotateAfter"); @@ -4552,13 +4534,13 @@ if ($alias && $aliasPath && $passwordMatch) { $useCache = false; is_writable($aliasPath) - or errorPage("Directory for alias is not writable.", 500); + or errorPage("Directory for alias is not writable."); preg_match($validAliasname, $alias) - or errorPage('Filename for alias contains invalid characters. Do not add extension.', 500); + or errorPage('Filename for alias contains invalid characters. Do not add extension.'); } elseif ($alias) { - errorPage('Alias is not enabled in the config file or password not matching.', 500); + errorPage('Alias is not enabled in the config file or password not matching.'); } verbose("alias = $alias"); @@ -4610,7 +4592,7 @@ if ($dummyImage === true) { $srcImage = $img->getTarget(); $imagePath = null; - + verbose("src (updated) = $srcImage"); } @@ -4669,7 +4651,7 @@ $hookBeforeCImage = getConfig('hook_before_CImage', null); if (is_callable($hookBeforeCImage)) { verbose("hookBeforeCImage activated"); - + $allConfig = $hookBeforeCImage($img, array( // Options for calculate dimensions 'newWidth' => $newWidth, @@ -4702,7 +4684,7 @@ if (is_callable($hookBeforeCImage)) { // Output format 'outputFormat' => $outputFormat, 'dpr' => $dpr, - + // Other 'postProcessing' => $postProcessing, )); diff --git a/webroot/imgs.php b/webroot/imgs.php old mode 100644 new mode 100755 From c32ae6dace3c69b47ae39adcb37a465f49216bff Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Sun, 6 Dec 2015 12:05:24 +0100 Subject: [PATCH 03/20] remove whitespace --- webroot/img.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webroot/img.php b/webroot/img.php index 72e6bea..eb6ba3b 100644 --- a/webroot/img.php +++ b/webroot/img.php @@ -8,7 +8,7 @@ * */ -$version = "v0.7.7 (2015-10-21)"; +$version = "v0.7.8 (2015-12-06)"; From 28be266d15ba7535399aa7c0529a3ac4ae10d9c1 Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Sun, 6 Dec 2015 12:25:22 +0100 Subject: [PATCH 04/20] moving to new travis environment --- .travis.yml | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index dc98566..fca188e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: php + php: - 5.4 - 5.5 @@ -7,19 +8,85 @@ php: - nightly - "7.0" + + +sudo: false + + + +git: + submodules: false + + + +addons: + apt: + packages: + #- php-codesniffer + #- phpmd + #- shellcheck + + matrix: allow_failures: - - php: nightly - php: hhvm + - php: nightly + + + +before_script: + + # Create a build directory for output + # Store all files in your own bin + #- install --directory build/bin + #- export PATH=$PATH:$PWD/build/bin/ + + + # Install validation tools + #- npm install -g htmlhint csslint jshint jscs jsonlint js-yaml html-minifier@0.8.0 clean-css uglify-js + + # Install phpcs + #- curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar + #- install --mode 0755 phpcs.phar $PWD/build/bin/phpcs + + # Install phpmd + #- wget -c http://static.phpmd.org/php/latest/phpmd.phar + #- install --mode 0755 phpmd.phar $PWD/build/bin/phpmd + + script: + # Check versions of validation tools + #- node --version + #- npm --version + + #- htmlhint --version + #- csslint --version + #- jscs --version + #- jshint --version + #- phpcs --version + #- phpmd --version + #- jsonlint --version + #- js-yaml --version + #- shellcheck --version + + #- html-minifier --version + #- cleancss --version + #- uglifyjs --version + + # Run validation & publish + #- make phpunit - phpunit + #- make phpcs + + notifications: irc: "irc.freenode.org#dbwebb" + webhooks: urls: - - https://webhooks.gitter.im/e/cce29c69604daa8a60ab + - https://webhooks.gitter.im/e/a89832db4f939e85ba97 on_success: change # options: [always|never|change] default: always on_failure: always # options: [always|never|change] default: always on_start: never # options: [always|never|change] default: always From 22f24cc75a0e536a6e05772cabf344502636479e Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Sun, 6 Dec 2015 12:26:20 +0100 Subject: [PATCH 05/20] remove whitespace --- CImage.php | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/CImage.php b/CImage.php index fca6dc7..f8b7b17 100644 --- a/CImage.php +++ b/CImage.php @@ -626,7 +626,7 @@ class CImage private function normalizeFileExtension($extension = null) { $extension = strtolower($extension ? $extension : $this->extension); - + if ($extension == 'jpeg') { $extension = 'jpg'; } @@ -648,7 +648,7 @@ class CImage if (!$this->isRemoteSourceOnWhitelist($src)) { throw new Exception("Hostname is not on whitelist for remote sources."); } - + $remote = new CRemoteImage(); $cache = $this->saveFolder . "/remote/"; @@ -1323,7 +1323,7 @@ class CImage if ($this->copyStrategy === self::RESIZE) { $copyStrat = "_rs"; } - + $width = $this->newWidth ? '_' . $this->newWidth : null; $height = $this->newHeight ? '_' . $this->newHeight : null; @@ -1374,8 +1374,8 @@ class CImage $subdir = ($subdir == '.') ? '_.' : $subdir; $subdir .= '_'; } - - $file = $prefix . $subdir . $filename . $width . $height + + $file = $prefix . $subdir . $filename . $width . $height . $offset . $crop . $cropToFit . $fillToFit . $crop_x . $crop_y . $upscale . $quality . $filters . $sharpen . $emboss . $blur . $palette @@ -1445,7 +1445,7 @@ class CImage if ($this->image === false) { throw new Exception("Could not load image."); } - + /* Removed v0.7.7 if (image_type_to_mime_type($this->fileType) == 'image/png') { $type = $this->getPngType(); @@ -1485,14 +1485,14 @@ class CImage public function getPngType($filename = null) { $filename = $filename ? $filename : $this->pathToImage; - + $pngType = ord(file_get_contents($filename, false, null, 25, 1)); if ($this->verbose) { $this->log("Checking png type of: " . $filename); $this->log($this->getPngTypeAsString($pngType)); } - + return $pngType; } @@ -1516,7 +1516,7 @@ class CImage $index = imagecolortransparent($this->image); $transparent = null; if ($index != -1) { - $transparent = " (transparent)"; + $transparent = " (transparent)"; } switch ($pngType) { @@ -2223,7 +2223,7 @@ class CImage $index = $this->image ? imagecolortransparent($this->image) : -1; - + if ($index != -1) { imagealphablending($img, true); @@ -2291,8 +2291,8 @@ class CImage return substr(image_type_to_extension($this->fileType), 1); } } - - + + /** * Save image. @@ -2445,7 +2445,7 @@ class CImage $colorspace = $image->getImageColorspace(); $this->log(" Current colorspace: " . $colorspace); - $profiles = $image->getImageProfiles('*', false); + $profiles = $image->getImageProfiles('*', false); $hasICCProfile = (array_search('icc', $profiles) !== false); $this->log(" Has ICC color profile: " . ($hasICCProfile ? "YES" : "NO")); @@ -2454,13 +2454,13 @@ class CImage $sRGBicc = file_get_contents($iccFile); $image->profileImage('icc', $sRGBicc); - + $image->transformImageColorspace(Imagick::COLORSPACE_SRGB); $image->writeImage($this->cacheFileName); return $this->cacheFileName; } } - + return false; } @@ -2583,7 +2583,7 @@ class CImage $this->log("Content-type: " . $mime); $this->log("Content-length: " . $size); $this->verboseOutput(); - + if (is_null($this->verboseFileName)) { exit; } @@ -2635,7 +2635,7 @@ class CImage $details['memoryPeek'] = round(memory_get_peak_usage()/1024/1024, 3) . " MB" ; $details['memoryCurrent'] = round(memory_get_usage()/1024/1024, 3) . " MB"; $details['memoryLimit'] = ini_get('memory_limit'); - + if (isset($_SERVER['REQUEST_TIME_FLOAT'])) { $details['loadTime'] = (string) round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 3) . "s"; } From a205d65ad55b56555439e3d13ca1508406670ee5 Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Sun, 6 Dec 2015 12:27:54 +0100 Subject: [PATCH 06/20] moving to new travis environment --- .travis.yml | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 9f15485..fca188e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: php + php: - 5.4 - 5.5 @@ -7,14 +8,85 @@ php: - nightly - "7.0" + + +sudo: false + + + +git: + submodules: false + + + +addons: + apt: + packages: + #- php-codesniffer + #- phpmd + #- shellcheck + + +matrix: + allow_failures: + - php: hhvm + - php: nightly + + + +before_script: + + # Create a build directory for output + # Store all files in your own bin + #- install --directory build/bin + #- export PATH=$PATH:$PWD/build/bin/ + + + # Install validation tools + #- npm install -g htmlhint csslint jshint jscs jsonlint js-yaml html-minifier@0.8.0 clean-css uglify-js + + # Install phpcs + #- curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar + #- install --mode 0755 phpcs.phar $PWD/build/bin/phpcs + + # Install phpmd + #- wget -c http://static.phpmd.org/php/latest/phpmd.phar + #- install --mode 0755 phpmd.phar $PWD/build/bin/phpmd + + + script: + # Check versions of validation tools + #- node --version + #- npm --version + + #- htmlhint --version + #- csslint --version + #- jscs --version + #- jshint --version + #- phpcs --version + #- phpmd --version + #- jsonlint --version + #- js-yaml --version + #- shellcheck --version + + #- html-minifier --version + #- cleancss --version + #- uglifyjs --version + + # Run validation & publish + #- make phpunit - phpunit + #- make phpcs + + notifications: irc: "irc.freenode.org#dbwebb" + webhooks: urls: - - https://webhooks.gitter.im/e/cce29c69604daa8a60ab + - https://webhooks.gitter.im/e/a89832db4f939e85ba97 on_success: change # options: [always|never|change] default: always on_failure: always # options: [always|never|change] default: always on_start: never # options: [always|never|change] default: always From 6c487c6f344cc1d4fa5e47e72cee694a50d3ba01 Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Sun, 6 Dec 2015 12:38:17 +0100 Subject: [PATCH 07/20] ignore build dir --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 8993e0b..29205e0 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ coverage.clover # Composer composer.lock vendor + +# Build and test +build/ From 902c0aaef8d4dbfea5f90ea44eec2a34704859f6 Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Sun, 6 Dec 2015 12:40:54 +0100 Subject: [PATCH 08/20] generate new bundles --- webroot/imgd.php | 34 +++++++++++++++++----------------- webroot/imgp.php | 34 +++++++++++++++++----------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/webroot/imgd.php b/webroot/imgd.php index 7125e49..fa060bb 100755 --- a/webroot/imgd.php +++ b/webroot/imgd.php @@ -1545,7 +1545,7 @@ class CImage private function normalizeFileExtension($extension = null) { $extension = strtolower($extension ? $extension : $this->extension); - + if ($extension == 'jpeg') { $extension = 'jpg'; } @@ -1567,7 +1567,7 @@ class CImage if (!$this->isRemoteSourceOnWhitelist($src)) { throw new Exception("Hostname is not on whitelist for remote sources."); } - + $remote = new CRemoteImage(); $cache = $this->saveFolder . "/remote/"; @@ -2242,7 +2242,7 @@ class CImage if ($this->copyStrategy === self::RESIZE) { $copyStrat = "_rs"; } - + $width = $this->newWidth ? '_' . $this->newWidth : null; $height = $this->newHeight ? '_' . $this->newHeight : null; @@ -2293,8 +2293,8 @@ class CImage $subdir = ($subdir == '.') ? '_.' : $subdir; $subdir .= '_'; } - - $file = $prefix . $subdir . $filename . $width . $height + + $file = $prefix . $subdir . $filename . $width . $height . $offset . $crop . $cropToFit . $fillToFit . $crop_x . $crop_y . $upscale . $quality . $filters . $sharpen . $emboss . $blur . $palette @@ -2364,7 +2364,7 @@ class CImage if ($this->image === false) { throw new Exception("Could not load image."); } - + /* Removed v0.7.7 if (image_type_to_mime_type($this->fileType) == 'image/png') { $type = $this->getPngType(); @@ -2404,14 +2404,14 @@ class CImage public function getPngType($filename = null) { $filename = $filename ? $filename : $this->pathToImage; - + $pngType = ord(file_get_contents($filename, false, null, 25, 1)); if ($this->verbose) { $this->log("Checking png type of: " . $filename); $this->log($this->getPngTypeAsString($pngType)); } - + return $pngType; } @@ -2435,7 +2435,7 @@ class CImage $index = imagecolortransparent($this->image); $transparent = null; if ($index != -1) { - $transparent = " (transparent)"; + $transparent = " (transparent)"; } switch ($pngType) { @@ -3142,7 +3142,7 @@ class CImage $index = $this->image ? imagecolortransparent($this->image) : -1; - + if ($index != -1) { imagealphablending($img, true); @@ -3210,8 +3210,8 @@ class CImage return substr(image_type_to_extension($this->fileType), 1); } } - - + + /** * Save image. @@ -3364,7 +3364,7 @@ class CImage $colorspace = $image->getImageColorspace(); $this->log(" Current colorspace: " . $colorspace); - $profiles = $image->getImageProfiles('*', false); + $profiles = $image->getImageProfiles('*', false); $hasICCProfile = (array_search('icc', $profiles) !== false); $this->log(" Has ICC color profile: " . ($hasICCProfile ? "YES" : "NO")); @@ -3373,13 +3373,13 @@ class CImage $sRGBicc = file_get_contents($iccFile); $image->profileImage('icc', $sRGBicc); - + $image->transformImageColorspace(Imagick::COLORSPACE_SRGB); $image->writeImage($this->cacheFileName); return $this->cacheFileName; } } - + return false; } @@ -3502,7 +3502,7 @@ class CImage $this->log("Content-type: " . $mime); $this->log("Content-length: " . $size); $this->verboseOutput(); - + if (is_null($this->verboseFileName)) { exit; } @@ -3554,7 +3554,7 @@ class CImage $details['memoryPeek'] = round(memory_get_peak_usage()/1024/1024, 3) . " MB" ; $details['memoryCurrent'] = round(memory_get_usage()/1024/1024, 3) . " MB"; $details['memoryLimit'] = ini_get('memory_limit'); - + if (isset($_SERVER['REQUEST_TIME_FLOAT'])) { $details['loadTime'] = (string) round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 3) . "s"; } diff --git a/webroot/imgp.php b/webroot/imgp.php index c73b541..734ad8a 100755 --- a/webroot/imgp.php +++ b/webroot/imgp.php @@ -1545,7 +1545,7 @@ class CImage private function normalizeFileExtension($extension = null) { $extension = strtolower($extension ? $extension : $this->extension); - + if ($extension == 'jpeg') { $extension = 'jpg'; } @@ -1567,7 +1567,7 @@ class CImage if (!$this->isRemoteSourceOnWhitelist($src)) { throw new Exception("Hostname is not on whitelist for remote sources."); } - + $remote = new CRemoteImage(); $cache = $this->saveFolder . "/remote/"; @@ -2242,7 +2242,7 @@ class CImage if ($this->copyStrategy === self::RESIZE) { $copyStrat = "_rs"; } - + $width = $this->newWidth ? '_' . $this->newWidth : null; $height = $this->newHeight ? '_' . $this->newHeight : null; @@ -2293,8 +2293,8 @@ class CImage $subdir = ($subdir == '.') ? '_.' : $subdir; $subdir .= '_'; } - - $file = $prefix . $subdir . $filename . $width . $height + + $file = $prefix . $subdir . $filename . $width . $height . $offset . $crop . $cropToFit . $fillToFit . $crop_x . $crop_y . $upscale . $quality . $filters . $sharpen . $emboss . $blur . $palette @@ -2364,7 +2364,7 @@ class CImage if ($this->image === false) { throw new Exception("Could not load image."); } - + /* Removed v0.7.7 if (image_type_to_mime_type($this->fileType) == 'image/png') { $type = $this->getPngType(); @@ -2404,14 +2404,14 @@ class CImage public function getPngType($filename = null) { $filename = $filename ? $filename : $this->pathToImage; - + $pngType = ord(file_get_contents($filename, false, null, 25, 1)); if ($this->verbose) { $this->log("Checking png type of: " . $filename); $this->log($this->getPngTypeAsString($pngType)); } - + return $pngType; } @@ -2435,7 +2435,7 @@ class CImage $index = imagecolortransparent($this->image); $transparent = null; if ($index != -1) { - $transparent = " (transparent)"; + $transparent = " (transparent)"; } switch ($pngType) { @@ -3142,7 +3142,7 @@ class CImage $index = $this->image ? imagecolortransparent($this->image) : -1; - + if ($index != -1) { imagealphablending($img, true); @@ -3210,8 +3210,8 @@ class CImage return substr(image_type_to_extension($this->fileType), 1); } } - - + + /** * Save image. @@ -3364,7 +3364,7 @@ class CImage $colorspace = $image->getImageColorspace(); $this->log(" Current colorspace: " . $colorspace); - $profiles = $image->getImageProfiles('*', false); + $profiles = $image->getImageProfiles('*', false); $hasICCProfile = (array_search('icc', $profiles) !== false); $this->log(" Has ICC color profile: " . ($hasICCProfile ? "YES" : "NO")); @@ -3373,13 +3373,13 @@ class CImage $sRGBicc = file_get_contents($iccFile); $image->profileImage('icc', $sRGBicc); - + $image->transformImageColorspace(Imagick::COLORSPACE_SRGB); $image->writeImage($this->cacheFileName); return $this->cacheFileName; } } - + return false; } @@ -3502,7 +3502,7 @@ class CImage $this->log("Content-type: " . $mime); $this->log("Content-length: " . $size); $this->verboseOutput(); - + if (is_null($this->verboseFileName)) { exit; } @@ -3554,7 +3554,7 @@ class CImage $details['memoryPeek'] = round(memory_get_peak_usage()/1024/1024, 3) . " MB" ; $details['memoryCurrent'] = round(memory_get_usage()/1024/1024, 3) . " MB"; $details['memoryLimit'] = ini_get('memory_limit'); - + if (isset($_SERVER['REQUEST_TIME_FLOAT'])) { $details['loadTime'] = (string) round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 3) . "s"; } From 29743b75ec386e38b06e4219bb99f2ad99f3ae7a Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Sun, 6 Dec 2015 12:47:47 +0100 Subject: [PATCH 09/20] prepare to tag v0.7.8 --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8193cf2..3733bef 100644 --- a/README.md +++ b/README.md @@ -46,14 +46,14 @@ There are several ways of installing. You either install the whole project which The [sourcode is available on GitHub](https://github.com/mosbth/cimage). Clone, fork or [download as zip](https://github.com/mosbth/cimage/archive/master.zip). -**Latest stable version is v0.7.7 released 2015-10-21.** +**Latest stable version is v0.7.8 released 2015-12-06.** I prefer cloning like this. Do switch to the latest stable version. ```bash git clone git://github.com/mosbth/cimage.git cd cimage -git checkout v0.7.7 +git checkout v0.7.8 ``` Make the cache-directory writable by the webserver. @@ -76,7 +76,7 @@ There are some all-included bundles of `img.php` that can be downloaded and used Dowload the version of your choice like this. ```bash -wget https://raw.githubusercontent.com/mosbth/cimage/v0.7.7/webroot/imgp.php +wget https://raw.githubusercontent.com/mosbth/cimage/v0.7.8/webroot/imgp.php ``` Open up the file in your editor and edit the array `$config`. Ensure that the paths to the image directory and the cache directory matches your environment, or create an own config-file for the script. From 376a40e5381bfaae4d881b1651d6884ff6ef27aa Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Sun, 6 Dec 2015 19:53:34 +0100 Subject: [PATCH 10/20] adding CCache to improve cache handling of dummy and srgb --- .gitignore | 3 ++ CCache.php | 107 +++++++++++++++++++++++++++++++++++++++ REVISION.md | 6 +++ cache/dummy/README.md | 0 composer.json | 1 + test/CCacheTest.php | 68 +++++++++++++++++++++++++ test/CImageDummyTest.php | 63 ++++++++++++++++++++--- webroot/img.php | 34 ++++++------- webroot/img_config.php | 20 +++----- 9 files changed, 265 insertions(+), 37 deletions(-) create mode 100644 CCache.php delete mode 100644 cache/dummy/README.md create mode 100644 test/CCacheTest.php diff --git a/.gitignore b/.gitignore index 29205e0..dd3f375 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,6 @@ vendor # Build and test build/ + +# Mac OS +.DS_Store diff --git a/CCache.php b/CCache.php new file mode 100644 index 0000000..45b4b60 --- /dev/null +++ b/CCache.php @@ -0,0 +1,107 @@ +path = $path; + + return $this; + } + + + + /** + * Get the path to the cache subdir and try to create it if its not there. + * + * @param string $subdir name of subdir + * @param array $create default is to try to create the subdir + * + * @return string | boolean as real path to the subdir or + * false if it does not exists + */ + public function getPathToSubdir($subdir, $create = true) + { + $path = realpath($this->path . "/" . $subdir); + + if (is_dir($path)) { + return $path; + } + + if ($create && is_writable($this->path)) { + $path = $this->path . "/" . $subdir; + + if (mkdir($path)) { + return realpath($path); + } + } + + return false; + } + + + + /** + * Get status of the cache subdir. + * + * @param string $subdir name of subdir + * + * @return string with status + */ + public function getStatusOfSubdir($subdir) + { + $path = realpath($this->path . "/" . $subdir); + + $exists = is_dir($path); + $res = $exists ? "exists" : "does not exist"; + + if ($exists) { + $res .= is_writable($path) ? ", writable" : ", not writable"; + } + + return $res; + } + + + + /** + * Remove the cache subdir. + * + * @param string $subdir name of subdir + * + * @return null | boolean true if success else false, null if no operation + */ + public function removeSubdir($subdir) + { + $path = realpath($this->path . "/" . $subdir); + + if (is_dir($path)) { + return rmdir($path); + } + + return null; + } +} diff --git a/REVISION.md b/REVISION.md index e9bc046..d3e8d2a 100644 --- a/REVISION.md +++ b/REVISION.md @@ -5,6 +5,12 @@ Revision history [![Build Status](https://scrutinizer-ci.com/g/mosbth/cimage/badges/build.png?b=master)](https://scrutinizer-ci.com/g/mosbth/cimage/build-status/master) +v0.7.8* (2015-12-06) +------------------------------------- + +* Adding CCache to improve cache handling of dummy and srgb, #130. + + v0.7.8 (2015-12-06) ------------------------------------- diff --git a/cache/dummy/README.md b/cache/dummy/README.md deleted file mode 100644 index e69de29..0000000 diff --git a/composer.json b/composer.json index de994fb..7c7ef44 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,7 @@ "CRemoteImage.php", "CWhitelist.php", "CAsciiArt.php" + "CCache.php" ] } } diff --git a/test/CCacheTest.php b/test/CCacheTest.php new file mode 100644 index 0000000..574b988 --- /dev/null +++ b/test/CCacheTest.php @@ -0,0 +1,68 @@ +setDir(CACHE_PATH); + + $exp = "exists, writable"; + $res = $cache->getStatusOfSubdir(""); + $this->assertEquals($exp, $res, "Status of cache dir missmatch."); + } + + + + /** + * Test + * + * @expectedException Exception + * + * @return void + */ + public function testSetWrongCacheDir() + { + $cache = new CCache(); + $cache->setDir(CACHE_PATH . "/NO_EXISTS"); + } + + + + /** + * Test + * + * @return void + */ + public function testCreateSubdir() + { + $cache = new CCache(); + $cache->setDir(CACHE_PATH); + + $subdir = "__test__"; + $cache->removeSubdir($subdir); + + $exp = "does not exist"; + $res = $cache->getStatusOfSubdir($subdir, false); + $this->assertEquals($exp, $res, "Subdir should not be created."); + + $res = $cache->getPathToSubdir($subdir); + $exp = realpath(CACHE_PATH . "/$subdir"); + $this->assertEquals($exp, $res, "Subdir path missmatch."); + + $exp = "exists, writable"; + $res = $cache->getStatusOfSubdir($subdir); + $this->assertEquals($exp, $res, "Subdir should exist."); + + $res = $cache->removeSubdir($subdir); + $this->assertTrue($res, "Remove subdir."); + } +} diff --git a/test/CImageDummyTest.php b/test/CImageDummyTest.php index 6d831ee..70af316 100644 --- a/test/CImageDummyTest.php +++ b/test/CImageDummyTest.php @@ -5,6 +5,57 @@ */ class CImageDummyTest extends \PHPUnit_Framework_TestCase { + const DUMMY = "__dummy__"; + private $cachepath; + + + + /** + * Setup environment + * + * @return void + */ + protected function setUp() + { + $cache = new CCache(); + $cache->setDir(CACHE_PATH); + $this->cachepath = $cache->getPathToSubdir(self::DUMMY); + } + + + + /** + * Clean up cache dir content. + * + * @return void + */ + protected function removeFilesInCacheDir() + { + $files = glob($this->cachepath . "/*"); + foreach ($files as $file) { + if (is_file($file)) { + unlink($file); + } + } + } + + + + /** + * Teardown environment + * + * @return void + */ + protected function tearDown() + { + $cache = new CCache(); + $cache->setDir(CACHE_PATH); + $this->removeFilesInCacheDir(); + $cache->removeSubdir(self::DUMMY); + } + + + /** * Test * @@ -14,15 +65,15 @@ class CImageDummyTest extends \PHPUnit_Framework_TestCase { $img = new CImage(); - $img->setSaveFolder(CACHE_PATH . "/dummy"); - $img->setSource('dummy', CACHE_PATH . "/dummy"); + $img->setSaveFolder($this->cachepath); + $img->setSource(self::DUMMY, $this->cachepath); $img->createDummyImage(); $img->generateFilename(null, false); $img->save(null, null, false); $filename = $img->getTarget(); - $this->assertEquals(basename($filename), "dummy_100_100", "Filename not as expected on dummy image."); + $this->assertEquals(basename($filename), self::DUMMY . "_100_100", "Filename not as expected on dummy image."); } @@ -36,14 +87,14 @@ class CImageDummyTest extends \PHPUnit_Framework_TestCase { $img = new CImage(); - $img->setSaveFolder(CACHE_PATH . "/dummy"); - $img->setSource('dummy', CACHE_PATH . "/dummy"); + $img->setSaveFolder($this->cachepath); + $img->setSource(self::DUMMY, $this->cachepath); $img->createDummyImage(200, 400); $img->generateFilename(null, false); $img->save(null, null, false); $filename = $img->getTarget(); - $this->assertEquals(basename($filename), "dummy_200_400", "Filename not as expected on dummy image."); + $this->assertEquals(basename($filename), self::DUMMY . "_200_400", "Filename not as expected on dummy image."); } } diff --git a/webroot/img.php b/webroot/img.php index d4644a5..4ac8106 100644 --- a/webroot/img.php +++ b/webroot/img.php @@ -8,7 +8,7 @@ * */ -$version = "v0.7.8 (2015-12-06)"; +$version = "v0.7.8* (2015-12-06)"; @@ -943,11 +943,13 @@ verbose("alias = $alias"); * Get the cachepath from config. */ $cachePath = getConfig('cache_path', __DIR__ . '/../cache/'); +$cache = new CCache(); +$cache->setDir($cachePath); /** - * Get the cachepath from config. + * Add cache control HTTP header. */ $cacheControl = getConfig('cache_control', null); @@ -961,11 +963,8 @@ if ($cacheControl) { /** * Prepare a dummy image and use it as source image. */ -$dummyDir = getConfig('dummy_dir', $cachePath. "/" . $dummyFilename); - if ($dummyImage === true) { - is_writable($dummyDir) - or verbose("dummy dir not writable = $dummyDir"); + $dummyDir = $cache->getPathToSubdir("dummy"); $img->setSaveFolder($dummyDir) ->setSource($dummyFilename, $dummyDir) @@ -993,24 +992,16 @@ if ($dummyImage === true) { /** * Prepare a sRGB version of the image and use it as source image. */ -$srgbDirName = "/srgb"; -$srgbDir = realpath(getConfig('srgb_dir', $cachePath . $srgbDirName)); $srgbDefault = getConfig('srgb_default', false); $srgbColorProfile = getConfig('srgb_colorprofile', __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc'); $srgb = getDefined('srgb', true, null); if ($srgb || $srgbDefault) { - if (!is_writable($srgbDir)) { - if (is_writable($cachePath)) { - mkdir($srgbDir); - } - } - $filename = $img->convert2sRGBColorSpace( $srcImage, $imagePath, - $srgbDir, + $cache->getPathToSubdir("srgb"), $srgbColorProfile, $useCache ); @@ -1034,9 +1025,16 @@ if ($status) { $text .= "PHP version = " . PHP_VERSION . "\n"; $text .= "Running on: " . $_SERVER['SERVER_SOFTWARE'] . "\n"; $text .= "Allow remote images = $allowRemote\n"; - $text .= "Cache writable = " . is_writable($cachePath) . "\n"; - $text .= "Cache dummy writable = " . is_writable($dummyDir) . "\n"; - $text .= "Cache srgb writable = " . is_writable($srgbDir) . "\n"; + + $res = $cache->getStatusOfSubdir(""); + $text .= "Cache $res\n"; + + $res = $cache->getStatusOfSubdir("dummy"); + $text .= "Cache dummy $res\n"; + + $res = $cache->getStatusOfSubdir("srgb"); + $text .= "Cache srgb $res\n"; + $text .= "Alias path writable = " . is_writable($aliasPath) . "\n"; $no = extension_loaded('exif') ? null : 'NOT'; diff --git a/webroot/img_config.php b/webroot/img_config.php index 38e75f9..1e9a0dd 100644 --- a/webroot/img_config.php +++ b/webroot/img_config.php @@ -14,7 +14,7 @@ return array( * mode: 'production' */ //'mode' => 'production', // 'development', 'strict' - //'mode' => 'development', // 'development', 'strict' + 'mode' => 'development', // 'development', 'strict' @@ -124,17 +124,15 @@ return array( /** * Convert the image to srgb before processing. Saves the converted - * image in the sub cache dir. This option is default false but can + * image in a cache subdir 'srgb'. This option is default false but can * be changed to default true to do this conversion for all images. * This option requires PHP extension imagick and will silently fail * if that is not installed. * * Default value: - * srgb_dir: $cachePath/srgb * srgb_default: false * srgb_colorprofile: __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc' */ - //'srgb_dir' => __DIR__ . '/../cache/srgb/', //'srgb_default' => false, //'srgb_colorprofile' => __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc', @@ -170,23 +168,19 @@ return array( /** * The name representing a dummy image which is automatically created - * and stored at the defined path. The dummy image can then be used - * inplace of an original image as a placeholder. - * The dummy_dir must be writable and it defaults to a subdir of the - * cache directory. - * Write protect the dummy_dir to prevent creation of new dummy images, - * but continue to use the existing ones. + * and stored as a image in the dir CACHE_PATH/dummy. The dummy image + * can then be used as a placeholder image. + * The dir CACHE_PATH/dummy is automatically created when needed. + * Write protect the CACHE_PATH/dummy to prevent creation of new + * dummy images, but continue to use the existing ones. * * Default value: * dummy_enabled: true as default, disable dummy feature by setting * to false. * dummy_filename: 'dummy' use this as ?src=dummy to create a dummy image. - * dummy_dir: Defaults to subdirectory of 'cache_path', - * named the same as 'dummy_filename' */ //'dummy_enabled' => true, //'dummy_filename' => 'dummy', - //'dummy_dir' => 'some writable directory', From 1dc04e7c53549a133c99bf7e8a80ffd75251c4b4 Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Sun, 6 Dec 2015 20:45:44 +0100 Subject: [PATCH 11/20] fix invalid composer.json --- .travis.yml | 1 + composer.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index fca188e..cd1a421 100644 --- a/.travis.yml +++ b/.travis.yml @@ -76,6 +76,7 @@ script: # Run validation & publish #- make phpunit + - composer validate - phpunit #- make phpcs diff --git a/composer.json b/composer.json index 7c7ef44..7b6ee81 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "CHttpGet.php", "CRemoteImage.php", "CWhitelist.php", - "CAsciiArt.php" + "CAsciiArt.php", "CCache.php" ] } From 3271d165ff48fdeb82014c9bdf481c1b7a185353 Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Mon, 7 Dec 2015 10:09:26 +0100 Subject: [PATCH 12/20] add correct version to remote headers, fix #131 --- CRemoteImage.php | 7 ++++++- webroot/img.php | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CRemoteImage.php b/CRemoteImage.php index c081cb3..4eb418e 100644 --- a/CRemoteImage.php +++ b/CRemoteImage.php @@ -147,7 +147,12 @@ class CRemoteImage */ public function setHeaderFields() { - $this->http->setHeader("User-Agent", "CImage/0.7.2 (PHP/". phpversion() . " cURL)"); + $cimageVersion = "CImage"; + if (defined("CIMAGE_USER_AGENT")) { + $cimageVersion = CIMAGE_USER_AGENT; + } + + $this->http->setHeader("User-Agent", "$cimageVersion (PHP/". phpversion() . " cURL)"); $this->http->setHeader("Accept", "image/jpeg,image/png,image/gif"); if ($this->useCache) { diff --git a/webroot/img.php b/webroot/img.php index 4ac8106..be3004f 100644 --- a/webroot/img.php +++ b/webroot/img.php @@ -10,6 +10,9 @@ $version = "v0.7.8* (2015-12-06)"; +// For CRemoteImage +define(CIMAGE_USER_AGENT, "CImage/$version"); + /** From 79a7fd17d850b26fd680de5cd62a3fa61683da38 Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Mon, 7 Dec 2015 15:12:20 +0100 Subject: [PATCH 13/20] improved cache handling for remote, #130 --- CImage.php | 17 +++++------------ REVISION.md | 5 +++-- webroot/img.php | 23 +++++++++++++---------- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/CImage.php b/CImage.php index f8b7b17..326353a 100644 --- a/CImage.php +++ b/CImage.php @@ -512,13 +512,15 @@ class CImage * Allow or disallow remote image download. * * @param boolean $allow true or false to enable and disable. + * @param string $cache path to cache dir. * @param string $pattern to use to detect if its a remote file. * * @return $this */ - public function setRemoteDownload($allow, $pattern = null) + public function setRemoteDownload($allow, $cache, $pattern = null) { $this->allowRemote = $allow; + $this->remoteCache = $cache; $this->remotePattern = is_null($pattern) ? $this->remotePattern : $pattern; $this->log( @@ -650,21 +652,12 @@ class CImage } $remote = new CRemoteImage(); - $cache = $this->saveFolder . "/remote/"; - if (!is_dir($cache)) { - if (!is_writable($this->saveFolder)) { - throw new Exception("Can not create remote cache, cachefolder not writable."); - } - mkdir($cache); - $this->log("The remote cache does not exists, creating it."); - } - - if (!is_writable($cache)) { + if (!is_writable($this->remoteCache)) { $this->log("The remote cache is not writable."); } - $remote->setCache($cache); + $remote->setCache($this->remoteCache); $remote->useCache($this->useCache); $src = $remote->download($src); diff --git a/REVISION.md b/REVISION.md index d3e8d2a..9f6cd9b 100644 --- a/REVISION.md +++ b/REVISION.md @@ -5,10 +5,11 @@ Revision history [![Build Status](https://scrutinizer-ci.com/g/mosbth/cimage/badges/build.png?b=master)](https://scrutinizer-ci.com/g/mosbth/cimage/build-status/master) -v0.7.8* (2015-12-06) +v0.7.8* (2015-12-07) ------------------------------------- -* Adding CCache to improve cache handling of dummy and srgb, #130. +* Added correct CImage version to remote agent string, #131. +* Adding CCache to improve cache handling of caching for dummy, remote and srgb. #130. v0.7.8 (2015-12-06) diff --git a/webroot/img.php b/webroot/img.php index be3004f..08a8cf8 100644 --- a/webroot/img.php +++ b/webroot/img.php @@ -348,6 +348,16 @@ $img->setVerbose($verbose || $verboseFile); +/** + * Get the cachepath from config. + */ +$cachePath = getConfig('cache_path', __DIR__ . '/../cache/'); +$cache = new CCache(); +$cache->setDir($cachePath); + + + + /** * Allow or disallow remote download of images from other servers. * Passwords apply if used. @@ -356,8 +366,10 @@ $img->setVerbose($verbose || $verboseFile); $allowRemote = getConfig('remote_allow', false); if ($allowRemote && $passwordMatch !== false) { + $cacheRemote = $cache->getPathToSubdir("remote"); + $pattern = getConfig('remote_pattern', null); - $img->setRemoteDownload($allowRemote, $pattern); + $img->setRemoteDownload($allowRemote, $pattern, $cacheRemote); $whitelist = getConfig('remote_whitelist', null); $img->setRemoteHostWhitelist($whitelist); @@ -942,15 +954,6 @@ verbose("alias = $alias"); -/** - * Get the cachepath from config. - */ -$cachePath = getConfig('cache_path', __DIR__ . '/../cache/'); -$cache = new CCache(); -$cache->setDir($cachePath); - - - /** * Add cache control HTTP header. */ From c009f423a260f56e53af0f1b33e99216ca3d5b10 Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Mon, 7 Dec 2015 15:22:52 +0100 Subject: [PATCH 14/20] tested cahce with remote --- webroot/img.php | 7 +++++-- webroot/img_config.php | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/webroot/img.php b/webroot/img.php index 08a8cf8..dada5a6 100644 --- a/webroot/img.php +++ b/webroot/img.php @@ -11,7 +11,7 @@ $version = "v0.7.8* (2015-12-06)"; // For CRemoteImage -define(CIMAGE_USER_AGENT, "CImage/$version"); +define("CIMAGE_USER_AGENT", "CImage/$version"); @@ -369,7 +369,7 @@ if ($allowRemote && $passwordMatch !== false) { $cacheRemote = $cache->getPathToSubdir("remote"); $pattern = getConfig('remote_pattern', null); - $img->setRemoteDownload($allowRemote, $pattern, $cacheRemote); + $img->setRemoteDownload($allowRemote, $cacheRemote, $pattern); $whitelist = getConfig('remote_whitelist', null); $img->setRemoteHostWhitelist($whitelist); @@ -1035,6 +1035,9 @@ if ($status) { $res = $cache->getStatusOfSubdir(""); $text .= "Cache $res\n"; + $res = $cache->getStatusOfSubdir("remote"); + $text .= "Cache remote $res\n"; + $res = $cache->getStatusOfSubdir("dummy"); $text .= "Cache dummy $res\n"; diff --git a/webroot/img_config.php b/webroot/img_config.php index 1e9a0dd..b436683 100644 --- a/webroot/img_config.php +++ b/webroot/img_config.php @@ -14,7 +14,7 @@ return array( * mode: 'production' */ //'mode' => 'production', // 'development', 'strict' - 'mode' => 'development', // 'development', 'strict' + //'mode' => 'development', // 'development', 'strict' From 0b2723feeeedc00a613dc99fc37033a494d86ca5 Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Mon, 7 Dec 2015 15:30:34 +0100 Subject: [PATCH 15/20] Strict mode only reporting 404 when failure, #127. --- README.md | 6 +++--- REVISION.md | 3 ++- webroot/img.php | 8 ++++++-- webroot/img_config.php | 5 +++-- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3733bef..23c3b01 100644 --- a/README.md +++ b/README.md @@ -46,14 +46,14 @@ There are several ways of installing. You either install the whole project which The [sourcode is available on GitHub](https://github.com/mosbth/cimage). Clone, fork or [download as zip](https://github.com/mosbth/cimage/archive/master.zip). -**Latest stable version is v0.7.8 released 2015-12-06.** +**Latest stable version is v0.7.9 released 2015-12-07.** I prefer cloning like this. Do switch to the latest stable version. ```bash git clone git://github.com/mosbth/cimage.git cd cimage -git checkout v0.7.8 +git checkout v0.7.9 ``` Make the cache-directory writable by the webserver. @@ -76,7 +76,7 @@ There are some all-included bundles of `img.php` that can be downloaded and used Dowload the version of your choice like this. ```bash -wget https://raw.githubusercontent.com/mosbth/cimage/v0.7.8/webroot/imgp.php +wget https://raw.githubusercontent.com/mosbth/cimage/v0.7.9/webroot/imgp.php ``` Open up the file in your editor and edit the array `$config`. Ensure that the paths to the image directory and the cache directory matches your environment, or create an own config-file for the script. diff --git a/REVISION.md b/REVISION.md index 9f6cd9b..5985afb 100644 --- a/REVISION.md +++ b/REVISION.md @@ -5,9 +5,10 @@ Revision history [![Build Status](https://scrutinizer-ci.com/g/mosbth/cimage/badges/build.png?b=master)](https://scrutinizer-ci.com/g/mosbth/cimage/build-status/master) -v0.7.8* (2015-12-07) +v0.7.9 (2015-12-07) ------------------------------------- +* Strict mode only reporting 404 when failure, #127. * Added correct CImage version to remote agent string, #131. * Adding CCache to improve cache handling of caching for dummy, remote and srgb. #130. diff --git a/webroot/img.php b/webroot/img.php index dada5a6..1250049 100644 --- a/webroot/img.php +++ b/webroot/img.php @@ -8,7 +8,7 @@ * */ -$version = "v0.7.8* (2015-12-06)"; +$version = "v0.7.9 (2015-12-07)"; // For CRemoteImage define("CIMAGE_USER_AGENT", "CImage/$version"); @@ -40,10 +40,14 @@ function errorPage($msg, $type = 500) header("HTTP/1.0 $header"); - if ($mode == 'development') { + if ($mode == "development") { die("[img.php] $msg"); } + if ($mode == "strict") { + $header = "404 Not Found"; + } + error_log("[img.php] $msg"); die("HTTP/1.0 $header"); } diff --git a/webroot/img_config.php b/webroot/img_config.php index b436683..b91b7e9 100644 --- a/webroot/img_config.php +++ b/webroot/img_config.php @@ -13,8 +13,9 @@ return array( * Default values: * mode: 'production' */ - //'mode' => 'production', // 'development', 'strict' - //'mode' => 'development', // 'development', 'strict' + //'mode' => 'production', + //'mode' => 'development', + //'mode' => 'strict', From 179469334aa8521542f0d608a72136d84c9358bf Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Mon, 7 Dec 2015 15:31:41 +0100 Subject: [PATCH 16/20] bundles to include CCache --- bin/create-img-single.bash | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bin/create-img-single.bash b/bin/create-img-single.bash index 0429ac4..c2d9895 100755 --- a/bin/create-img-single.bash +++ b/bin/create-img-single.bash @@ -39,6 +39,7 @@ $ECHO "\n CRemoteImage.php" $ECHO "\n CWhitelist.php" $ECHO "\n CAsciiArt.php" $ECHO "\n CImage.php" +$ECHO "\n CCache.php" $ECHO "\n webroot/img.php" $ECHO "\n" $ECHO "\n'$TARGET_D' is for development mode." @@ -74,6 +75,9 @@ $ECHO "$NEWLINES" | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null tail -n +2 CImage.php | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null $ECHO "$NEWLINES" | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null +tail -n +2 CCache.php | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null +$ECHO "$NEWLINES" | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null + tail -n +2 webroot/img.php | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null $ECHO "$NEWLINES" | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null From 6e0c775ede2316b7faa1c02ef5d0a8963f2852be Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Mon, 7 Dec 2015 15:35:19 +0100 Subject: [PATCH 17/20] clear whitespace --- CAsciiArt.php | 2 +- CCache.php | 2 +- CImage.php | 24 ++++++++++++------------ test/CImageRemoteDownloadTest.php | 4 ++-- test/CImageSRGBTest.php | 6 +++--- webroot/img.php | 21 ++++++++++++--------- 6 files changed, 31 insertions(+), 28 deletions(-) diff --git a/CAsciiArt.php b/CAsciiArt.php index 836d9fb..6a9a758 100644 --- a/CAsciiArt.php +++ b/CAsciiArt.php @@ -175,7 +175,7 @@ class CAsciiArt */ public function getLuminance($red, $green, $blue) { - switch($this->luminanceStrategy) { + switch ($this->luminanceStrategy) { case 1: $luminance = ($red * 0.2126 + $green * 0.7152 + $blue * 0.0722) / 255; break; diff --git a/CCache.php b/CCache.php index 45b4b60..7a35900 100644 --- a/CCache.php +++ b/CCache.php @@ -75,7 +75,7 @@ class CCache { $path = realpath($this->path . "/" . $subdir); - $exists = is_dir($path); + $exists = is_dir($path); $res = $exists ? "exists" : "does not exist"; if ($exists) { diff --git a/CImage.php b/CImage.php index 326353a..88a7c3f 100644 --- a/CImage.php +++ b/CImage.php @@ -208,8 +208,8 @@ class CImage /** * Path to command to optimize jpeg images, for example jpegtran or null. */ - private $jpegOptimize; - private $jpegOptimizeCmd; + private $jpegOptimize; + private $jpegOptimizeCmd; @@ -400,7 +400,7 @@ class CImage */ const RESIZE = 1; const RESAMPLE = 2; - private $copyStrategy = NULL; + private $copyStrategy = null; @@ -631,7 +631,7 @@ class CImage if ($extension == 'jpeg') { $extension = 'jpg'; - } + } return $extension; } @@ -1619,11 +1619,11 @@ class CImage * * @return $this */ - public function setCopyResizeStrategy($strategy) - { - $this->copyStrategy = $strategy; - return $this; - } + public function setCopyResizeStrategy($strategy) + { + $this->copyStrategy = $strategy; + return $this; + } @@ -1634,7 +1634,7 @@ class CImage */ public function imageCopyResampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { - if($this->copyStrategy == self::RESIZE) { + if ($this->copyStrategy == self::RESIZE) { $this->log("Copy by resize"); imagecopyresized($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } else { @@ -2312,7 +2312,7 @@ class CImage $type = $this->getTargetImageExtension(); $this->Log("Saving image as " . $type); - switch($type) { + switch ($type) { case 'jpeg': case 'jpg': @@ -2549,7 +2549,7 @@ class CImage header('Last-Modified: ' . $gmdate . " GMT"); } - foreach($this->HTTPHeader as $key => $val) { + foreach ($this->HTTPHeader as $key => $val) { header("$key: $val"); } diff --git a/test/CImageRemoteDownloadTest.php b/test/CImageRemoteDownloadTest.php index dd2af20..0dafae1 100644 --- a/test/CImageRemoteDownloadTest.php +++ b/test/CImageRemoteDownloadTest.php @@ -61,7 +61,7 @@ class CImageRemoteDownloadTest extends \PHPUnit_Framework_TestCase public function testAllowRemoteDownloadDefaultPatternValid($source) { $img = new CImage(); - $img->setRemoteDownload(true); + $img->setRemoteDownload(true, ""); $res = $img->isRemoteSource($source); $this->assertTrue($res, "Should be a valid remote source: '$source'."); @@ -79,7 +79,7 @@ class CImageRemoteDownloadTest extends \PHPUnit_Framework_TestCase public function testAllowRemoteDownloadDefaultPatternInvalid($source) { $img = new CImage(); - $img->setRemoteDownload(true); + $img->setRemoteDownload(true, ""); $res = $img->isRemoteSource($source); $this->assertFalse($res, "Should not be a valid remote source: '$source'."); diff --git a/test/CImageSRGBTest.php b/test/CImageSRGBTest.php index 49b816b..906220f 100644 --- a/test/CImageSRGBTest.php +++ b/test/CImageSRGBTest.php @@ -38,7 +38,7 @@ class CImageSRGBTest extends \PHPUnit_Framework_TestCase $img = new CImage(); $filename = $img->convert2sRGBColorSpace( - 'car.png', + 'car.png', IMAGE_PATH, $this->cache, $this->srgbColorProfile @@ -63,8 +63,8 @@ class CImageSRGBTest extends \PHPUnit_Framework_TestCase $img = new CImage(); $filename = $img->convert2sRGBColorSpace( - 'car.jpg', - IMAGE_PATH, + 'car.jpg', + IMAGE_PATH, $this->cache, $this->srgbColorProfile ); diff --git a/webroot/img.php b/webroot/img.php index 1250049..553c604 100644 --- a/webroot/img.php +++ b/webroot/img.php @@ -30,10 +30,10 @@ function errorPage($msg, $type = 500) switch ($type) { case 403: $header = "403 Forbidden"; - break; + break; case 404: $header = "404 Not Found"; - break; + break; default: $header = "500 Internal Server Error"; } @@ -63,8 +63,9 @@ set_exception_handler(function ($exception) { . $exception->getMessage() . "

"
         . $exception->getTraceAsString()
-        . "
" - , 500); + . "", + 500 + ); }); @@ -263,7 +264,7 @@ $pwd = get(array('password', 'pwd'), null); // Check if passwords match, if configured to use passwords $passwordMatch = null; if ($pwd) { - switch($pwdType) { + switch ($pwdType) { case 'md5': $passwordMatch = ($pwdConfig === md5($pwd)); break; @@ -439,14 +440,16 @@ if ($dummyEnabled && $srcImage === $dummyFilename) { is_file($pathToImage) or errorPage( 'Source image is not a valid file, check the filename and that a - matching file exists on the filesystem.' - , 404); + matching file exists on the filesystem.', + 404 + ); substr_compare($imageDir, $pathToImage, 0, strlen($imageDir)) == 0 or errorPage( 'Security constraint: Source image is not below the directory "image_path" - as specified in the config file img_config.php.' - , 404); + as specified in the config file img_config.php.', + 404 + ); } verbose("src = $srcImage"); From b069e322e99f5510171c5eb3f44f6e11f5b1daca Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Mon, 7 Dec 2015 15:36:06 +0100 Subject: [PATCH 18/20] create bundles --- webroot/imgd.php | 245 +++++++++++++++++++++++++++++++++++------------ webroot/imgp.php | 245 +++++++++++++++++++++++++++++++++++------------ webroot/imgs.php | 12 +-- 3 files changed, 372 insertions(+), 130 deletions(-) diff --git a/webroot/imgd.php b/webroot/imgd.php index fa060bb..65a42b5 100755 --- a/webroot/imgd.php +++ b/webroot/imgd.php @@ -476,7 +476,12 @@ class CRemoteImage */ public function setHeaderFields() { - $this->http->setHeader("User-Agent", "CImage/0.7.2 (PHP/". phpversion() . " cURL)"); + $cimageVersion = "CImage"; + if (defined("CIMAGE_USER_AGENT")) { + $cimageVersion = CIMAGE_USER_AGENT; + } + + $this->http->setHeader("User-Agent", "$cimageVersion (PHP/". phpversion() . " cURL)"); $this->http->setHeader("Accept", "image/jpeg,image/png,image/gif"); if ($this->useCache) { @@ -880,7 +885,7 @@ class CAsciiArt */ public function getLuminance($red, $green, $blue) { - switch($this->luminanceStrategy) { + switch ($this->luminanceStrategy) { case 1: $luminance = ($red * 0.2126 + $green * 0.7152 + $blue * 0.0722) / 255; break; @@ -1127,8 +1132,8 @@ class CImage /** * Path to command to optimize jpeg images, for example jpegtran or null. */ - private $jpegOptimize; - private $jpegOptimizeCmd; + private $jpegOptimize; + private $jpegOptimizeCmd; @@ -1319,7 +1324,7 @@ class CImage */ const RESIZE = 1; const RESAMPLE = 2; - private $copyStrategy = NULL; + private $copyStrategy = null; @@ -1431,13 +1436,15 @@ class CImage * Allow or disallow remote image download. * * @param boolean $allow true or false to enable and disable. + * @param string $cache path to cache dir. * @param string $pattern to use to detect if its a remote file. * * @return $this */ - public function setRemoteDownload($allow, $pattern = null) + public function setRemoteDownload($allow, $cache, $pattern = null) { $this->allowRemote = $allow; + $this->remoteCache = $cache; $this->remotePattern = is_null($pattern) ? $this->remotePattern : $pattern; $this->log( @@ -1548,7 +1555,7 @@ class CImage if ($extension == 'jpeg') { $extension = 'jpg'; - } + } return $extension; } @@ -1569,21 +1576,12 @@ class CImage } $remote = new CRemoteImage(); - $cache = $this->saveFolder . "/remote/"; - if (!is_dir($cache)) { - if (!is_writable($this->saveFolder)) { - throw new Exception("Can not create remote cache, cachefolder not writable."); - } - mkdir($cache); - $this->log("The remote cache does not exists, creating it."); - } - - if (!is_writable($cache)) { + if (!is_writable($this->remoteCache)) { $this->log("The remote cache is not writable."); } - $remote->setCache($cache); + $remote->setCache($this->remoteCache); $remote->useCache($this->useCache); $src = $remote->download($src); @@ -2545,11 +2543,11 @@ class CImage * * @return $this */ - public function setCopyResizeStrategy($strategy) - { - $this->copyStrategy = $strategy; - return $this; - } + public function setCopyResizeStrategy($strategy) + { + $this->copyStrategy = $strategy; + return $this; + } @@ -2560,7 +2558,7 @@ class CImage */ public function imageCopyResampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { - if($this->copyStrategy == self::RESIZE) { + if ($this->copyStrategy == self::RESIZE) { $this->log("Copy by resize"); imagecopyresized($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } else { @@ -3238,7 +3236,7 @@ class CImage $type = $this->getTargetImageExtension(); $this->Log("Saving image as " . $type); - switch($type) { + switch ($type) { case 'jpeg': case 'jpg': @@ -3475,7 +3473,7 @@ class CImage header('Last-Modified: ' . $gmdate . " GMT"); } - foreach($this->HTTPHeader as $key => $val) { + foreach ($this->HTTPHeader as $key => $val) { header("$key: $val"); } @@ -3694,6 +3692,115 @@ EOD; +/** + * Deal with the cache directory and cached items. + * + */ +class CCache +{ + /** + * Path to the cache directory. + */ + private $path; + + + + /** + * Set the path to the cache dir which must exist. + * + * @param string path to the cache dir. + * + * @throws Exception when $path is not a directory. + * + * @return $this + */ + public function setDir($path) + { + if (!is_dir($path)) { + throw new Exception("Cachedir is not a directory."); + } + + $this->path = $path; + + return $this; + } + + + + /** + * Get the path to the cache subdir and try to create it if its not there. + * + * @param string $subdir name of subdir + * @param array $create default is to try to create the subdir + * + * @return string | boolean as real path to the subdir or + * false if it does not exists + */ + public function getPathToSubdir($subdir, $create = true) + { + $path = realpath($this->path . "/" . $subdir); + + if (is_dir($path)) { + return $path; + } + + if ($create && is_writable($this->path)) { + $path = $this->path . "/" . $subdir; + + if (mkdir($path)) { + return realpath($path); + } + } + + return false; + } + + + + /** + * Get status of the cache subdir. + * + * @param string $subdir name of subdir + * + * @return string with status + */ + public function getStatusOfSubdir($subdir) + { + $path = realpath($this->path . "/" . $subdir); + + $exists = is_dir($path); + $res = $exists ? "exists" : "does not exist"; + + if ($exists) { + $res .= is_writable($path) ? ", writable" : ", not writable"; + } + + return $res; + } + + + + /** + * Remove the cache subdir. + * + * @param string $subdir name of subdir + * + * @return null | boolean true if success else false, null if no operation + */ + public function removeSubdir($subdir) + { + $path = realpath($this->path . "/" . $subdir); + + if (is_dir($path)) { + return rmdir($path); + } + + return null; + } +} + + + /** * Resize and crop images on the fly, store generated images in a cache. * @@ -3703,7 +3810,10 @@ EOD; * */ -$version = "v0.7.8 (2015-12-06)"; +$version = "v0.7.9 (2015-12-07)"; + +// For CRemoteImage +define("CIMAGE_USER_AGENT", "CImage/$version"); @@ -3722,20 +3832,24 @@ function errorPage($msg, $type = 500) switch ($type) { case 403: $header = "403 Forbidden"; - break; + break; case 404: $header = "404 Not Found"; - break; + break; default: $header = "500 Internal Server Error"; } header("HTTP/1.0 $header"); - if ($mode == 'development') { + if ($mode == "development") { die("[img.php] $msg"); } + if ($mode == "strict") { + $header = "404 Not Found"; + } + error_log("[img.php] $msg"); die("HTTP/1.0 $header"); } @@ -3751,8 +3865,9 @@ set_exception_handler(function ($exception) { . $exception->getMessage() . "

"
         . $exception->getTraceAsString()
-        . "
" - , 500); + . "", + 500 + ); }); @@ -3951,7 +4066,7 @@ $pwd = get(array('password', 'pwd'), null); // Check if passwords match, if configured to use passwords $passwordMatch = null; if ($pwd) { - switch($pwdType) { + switch ($pwdType) { case 'md5': $passwordMatch = ($pwdConfig === md5($pwd)); break; @@ -4040,6 +4155,16 @@ $img->setVerbose($verbose || $verboseFile); +/** + * Get the cachepath from config. + */ +$cachePath = getConfig('cache_path', __DIR__ . '/../cache/'); +$cache = new CCache(); +$cache->setDir($cachePath); + + + + /** * Allow or disallow remote download of images from other servers. * Passwords apply if used. @@ -4048,8 +4173,10 @@ $img->setVerbose($verbose || $verboseFile); $allowRemote = getConfig('remote_allow', false); if ($allowRemote && $passwordMatch !== false) { + $cacheRemote = $cache->getPathToSubdir("remote"); + $pattern = getConfig('remote_pattern', null); - $img->setRemoteDownload($allowRemote, $pattern); + $img->setRemoteDownload($allowRemote, $cacheRemote, $pattern); $whitelist = getConfig('remote_whitelist', null); $img->setRemoteHostWhitelist($whitelist); @@ -4115,14 +4242,16 @@ if ($dummyEnabled && $srcImage === $dummyFilename) { is_file($pathToImage) or errorPage( 'Source image is not a valid file, check the filename and that a - matching file exists on the filesystem.' - , 404); + matching file exists on the filesystem.', + 404 + ); substr_compare($imageDir, $pathToImage, 0, strlen($imageDir)) == 0 or errorPage( 'Security constraint: Source image is not below the directory "image_path" - as specified in the config file img_config.php.' - , 404); + as specified in the config file img_config.php.', + 404 + ); } verbose("src = $srcImage"); @@ -4635,14 +4764,7 @@ verbose("alias = $alias"); /** - * Get the cachepath from config. - */ -$cachePath = getConfig('cache_path', __DIR__ . '/../cache/'); - - - -/** - * Get the cachepath from config. + * Add cache control HTTP header. */ $cacheControl = getConfig('cache_control', null); @@ -4656,11 +4778,8 @@ if ($cacheControl) { /** * Prepare a dummy image and use it as source image. */ -$dummyDir = getConfig('dummy_dir', $cachePath. "/" . $dummyFilename); - if ($dummyImage === true) { - is_writable($dummyDir) - or verbose("dummy dir not writable = $dummyDir"); + $dummyDir = $cache->getPathToSubdir("dummy"); $img->setSaveFolder($dummyDir) ->setSource($dummyFilename, $dummyDir) @@ -4688,24 +4807,16 @@ if ($dummyImage === true) { /** * Prepare a sRGB version of the image and use it as source image. */ -$srgbDirName = "/srgb"; -$srgbDir = realpath(getConfig('srgb_dir', $cachePath . $srgbDirName)); $srgbDefault = getConfig('srgb_default', false); $srgbColorProfile = getConfig('srgb_colorprofile', __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc'); $srgb = getDefined('srgb', true, null); if ($srgb || $srgbDefault) { - if (!is_writable($srgbDir)) { - if (is_writable($cachePath)) { - mkdir($srgbDir); - } - } - $filename = $img->convert2sRGBColorSpace( $srcImage, $imagePath, - $srgbDir, + $cache->getPathToSubdir("srgb"), $srgbColorProfile, $useCache ); @@ -4729,9 +4840,19 @@ if ($status) { $text .= "PHP version = " . PHP_VERSION . "\n"; $text .= "Running on: " . $_SERVER['SERVER_SOFTWARE'] . "\n"; $text .= "Allow remote images = $allowRemote\n"; - $text .= "Cache writable = " . is_writable($cachePath) . "\n"; - $text .= "Cache dummy writable = " . is_writable($dummyDir) . "\n"; - $text .= "Cache srgb writable = " . is_writable($srgbDir) . "\n"; + + $res = $cache->getStatusOfSubdir(""); + $text .= "Cache $res\n"; + + $res = $cache->getStatusOfSubdir("remote"); + $text .= "Cache remote $res\n"; + + $res = $cache->getStatusOfSubdir("dummy"); + $text .= "Cache dummy $res\n"; + + $res = $cache->getStatusOfSubdir("srgb"); + $text .= "Cache srgb $res\n"; + $text .= "Alias path writable = " . is_writable($aliasPath) . "\n"; $no = extension_loaded('exif') ? null : 'NOT'; diff --git a/webroot/imgp.php b/webroot/imgp.php index 734ad8a..27358de 100755 --- a/webroot/imgp.php +++ b/webroot/imgp.php @@ -476,7 +476,12 @@ class CRemoteImage */ public function setHeaderFields() { - $this->http->setHeader("User-Agent", "CImage/0.7.2 (PHP/". phpversion() . " cURL)"); + $cimageVersion = "CImage"; + if (defined("CIMAGE_USER_AGENT")) { + $cimageVersion = CIMAGE_USER_AGENT; + } + + $this->http->setHeader("User-Agent", "$cimageVersion (PHP/". phpversion() . " cURL)"); $this->http->setHeader("Accept", "image/jpeg,image/png,image/gif"); if ($this->useCache) { @@ -880,7 +885,7 @@ class CAsciiArt */ public function getLuminance($red, $green, $blue) { - switch($this->luminanceStrategy) { + switch ($this->luminanceStrategy) { case 1: $luminance = ($red * 0.2126 + $green * 0.7152 + $blue * 0.0722) / 255; break; @@ -1127,8 +1132,8 @@ class CImage /** * Path to command to optimize jpeg images, for example jpegtran or null. */ - private $jpegOptimize; - private $jpegOptimizeCmd; + private $jpegOptimize; + private $jpegOptimizeCmd; @@ -1319,7 +1324,7 @@ class CImage */ const RESIZE = 1; const RESAMPLE = 2; - private $copyStrategy = NULL; + private $copyStrategy = null; @@ -1431,13 +1436,15 @@ class CImage * Allow or disallow remote image download. * * @param boolean $allow true or false to enable and disable. + * @param string $cache path to cache dir. * @param string $pattern to use to detect if its a remote file. * * @return $this */ - public function setRemoteDownload($allow, $pattern = null) + public function setRemoteDownload($allow, $cache, $pattern = null) { $this->allowRemote = $allow; + $this->remoteCache = $cache; $this->remotePattern = is_null($pattern) ? $this->remotePattern : $pattern; $this->log( @@ -1548,7 +1555,7 @@ class CImage if ($extension == 'jpeg') { $extension = 'jpg'; - } + } return $extension; } @@ -1569,21 +1576,12 @@ class CImage } $remote = new CRemoteImage(); - $cache = $this->saveFolder . "/remote/"; - if (!is_dir($cache)) { - if (!is_writable($this->saveFolder)) { - throw new Exception("Can not create remote cache, cachefolder not writable."); - } - mkdir($cache); - $this->log("The remote cache does not exists, creating it."); - } - - if (!is_writable($cache)) { + if (!is_writable($this->remoteCache)) { $this->log("The remote cache is not writable."); } - $remote->setCache($cache); + $remote->setCache($this->remoteCache); $remote->useCache($this->useCache); $src = $remote->download($src); @@ -2545,11 +2543,11 @@ class CImage * * @return $this */ - public function setCopyResizeStrategy($strategy) - { - $this->copyStrategy = $strategy; - return $this; - } + public function setCopyResizeStrategy($strategy) + { + $this->copyStrategy = $strategy; + return $this; + } @@ -2560,7 +2558,7 @@ class CImage */ public function imageCopyResampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { - if($this->copyStrategy == self::RESIZE) { + if ($this->copyStrategy == self::RESIZE) { $this->log("Copy by resize"); imagecopyresized($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } else { @@ -3238,7 +3236,7 @@ class CImage $type = $this->getTargetImageExtension(); $this->Log("Saving image as " . $type); - switch($type) { + switch ($type) { case 'jpeg': case 'jpg': @@ -3475,7 +3473,7 @@ class CImage header('Last-Modified: ' . $gmdate . " GMT"); } - foreach($this->HTTPHeader as $key => $val) { + foreach ($this->HTTPHeader as $key => $val) { header("$key: $val"); } @@ -3694,6 +3692,115 @@ EOD; +/** + * Deal with the cache directory and cached items. + * + */ +class CCache +{ + /** + * Path to the cache directory. + */ + private $path; + + + + /** + * Set the path to the cache dir which must exist. + * + * @param string path to the cache dir. + * + * @throws Exception when $path is not a directory. + * + * @return $this + */ + public function setDir($path) + { + if (!is_dir($path)) { + throw new Exception("Cachedir is not a directory."); + } + + $this->path = $path; + + return $this; + } + + + + /** + * Get the path to the cache subdir and try to create it if its not there. + * + * @param string $subdir name of subdir + * @param array $create default is to try to create the subdir + * + * @return string | boolean as real path to the subdir or + * false if it does not exists + */ + public function getPathToSubdir($subdir, $create = true) + { + $path = realpath($this->path . "/" . $subdir); + + if (is_dir($path)) { + return $path; + } + + if ($create && is_writable($this->path)) { + $path = $this->path . "/" . $subdir; + + if (mkdir($path)) { + return realpath($path); + } + } + + return false; + } + + + + /** + * Get status of the cache subdir. + * + * @param string $subdir name of subdir + * + * @return string with status + */ + public function getStatusOfSubdir($subdir) + { + $path = realpath($this->path . "/" . $subdir); + + $exists = is_dir($path); + $res = $exists ? "exists" : "does not exist"; + + if ($exists) { + $res .= is_writable($path) ? ", writable" : ", not writable"; + } + + return $res; + } + + + + /** + * Remove the cache subdir. + * + * @param string $subdir name of subdir + * + * @return null | boolean true if success else false, null if no operation + */ + public function removeSubdir($subdir) + { + $path = realpath($this->path . "/" . $subdir); + + if (is_dir($path)) { + return rmdir($path); + } + + return null; + } +} + + + /** * Resize and crop images on the fly, store generated images in a cache. * @@ -3703,7 +3810,10 @@ EOD; * */ -$version = "v0.7.8 (2015-12-06)"; +$version = "v0.7.9 (2015-12-07)"; + +// For CRemoteImage +define("CIMAGE_USER_AGENT", "CImage/$version"); @@ -3722,20 +3832,24 @@ function errorPage($msg, $type = 500) switch ($type) { case 403: $header = "403 Forbidden"; - break; + break; case 404: $header = "404 Not Found"; - break; + break; default: $header = "500 Internal Server Error"; } header("HTTP/1.0 $header"); - if ($mode == 'development') { + if ($mode == "development") { die("[img.php] $msg"); } + if ($mode == "strict") { + $header = "404 Not Found"; + } + error_log("[img.php] $msg"); die("HTTP/1.0 $header"); } @@ -3751,8 +3865,9 @@ set_exception_handler(function ($exception) { . $exception->getMessage() . "

"
         . $exception->getTraceAsString()
-        . "
" - , 500); + . "", + 500 + ); }); @@ -3951,7 +4066,7 @@ $pwd = get(array('password', 'pwd'), null); // Check if passwords match, if configured to use passwords $passwordMatch = null; if ($pwd) { - switch($pwdType) { + switch ($pwdType) { case 'md5': $passwordMatch = ($pwdConfig === md5($pwd)); break; @@ -4040,6 +4155,16 @@ $img->setVerbose($verbose || $verboseFile); +/** + * Get the cachepath from config. + */ +$cachePath = getConfig('cache_path', __DIR__ . '/../cache/'); +$cache = new CCache(); +$cache->setDir($cachePath); + + + + /** * Allow or disallow remote download of images from other servers. * Passwords apply if used. @@ -4048,8 +4173,10 @@ $img->setVerbose($verbose || $verboseFile); $allowRemote = getConfig('remote_allow', false); if ($allowRemote && $passwordMatch !== false) { + $cacheRemote = $cache->getPathToSubdir("remote"); + $pattern = getConfig('remote_pattern', null); - $img->setRemoteDownload($allowRemote, $pattern); + $img->setRemoteDownload($allowRemote, $cacheRemote, $pattern); $whitelist = getConfig('remote_whitelist', null); $img->setRemoteHostWhitelist($whitelist); @@ -4115,14 +4242,16 @@ if ($dummyEnabled && $srcImage === $dummyFilename) { is_file($pathToImage) or errorPage( 'Source image is not a valid file, check the filename and that a - matching file exists on the filesystem.' - , 404); + matching file exists on the filesystem.', + 404 + ); substr_compare($imageDir, $pathToImage, 0, strlen($imageDir)) == 0 or errorPage( 'Security constraint: Source image is not below the directory "image_path" - as specified in the config file img_config.php.' - , 404); + as specified in the config file img_config.php.', + 404 + ); } verbose("src = $srcImage"); @@ -4635,14 +4764,7 @@ verbose("alias = $alias"); /** - * Get the cachepath from config. - */ -$cachePath = getConfig('cache_path', __DIR__ . '/../cache/'); - - - -/** - * Get the cachepath from config. + * Add cache control HTTP header. */ $cacheControl = getConfig('cache_control', null); @@ -4656,11 +4778,8 @@ if ($cacheControl) { /** * Prepare a dummy image and use it as source image. */ -$dummyDir = getConfig('dummy_dir', $cachePath. "/" . $dummyFilename); - if ($dummyImage === true) { - is_writable($dummyDir) - or verbose("dummy dir not writable = $dummyDir"); + $dummyDir = $cache->getPathToSubdir("dummy"); $img->setSaveFolder($dummyDir) ->setSource($dummyFilename, $dummyDir) @@ -4688,24 +4807,16 @@ if ($dummyImage === true) { /** * Prepare a sRGB version of the image and use it as source image. */ -$srgbDirName = "/srgb"; -$srgbDir = realpath(getConfig('srgb_dir', $cachePath . $srgbDirName)); $srgbDefault = getConfig('srgb_default', false); $srgbColorProfile = getConfig('srgb_colorprofile', __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc'); $srgb = getDefined('srgb', true, null); if ($srgb || $srgbDefault) { - if (!is_writable($srgbDir)) { - if (is_writable($cachePath)) { - mkdir($srgbDir); - } - } - $filename = $img->convert2sRGBColorSpace( $srcImage, $imagePath, - $srgbDir, + $cache->getPathToSubdir("srgb"), $srgbColorProfile, $useCache ); @@ -4729,9 +4840,19 @@ if ($status) { $text .= "PHP version = " . PHP_VERSION . "\n"; $text .= "Running on: " . $_SERVER['SERVER_SOFTWARE'] . "\n"; $text .= "Allow remote images = $allowRemote\n"; - $text .= "Cache writable = " . is_writable($cachePath) . "\n"; - $text .= "Cache dummy writable = " . is_writable($dummyDir) . "\n"; - $text .= "Cache srgb writable = " . is_writable($srgbDir) . "\n"; + + $res = $cache->getStatusOfSubdir(""); + $text .= "Cache $res\n"; + + $res = $cache->getStatusOfSubdir("remote"); + $text .= "Cache remote $res\n"; + + $res = $cache->getStatusOfSubdir("dummy"); + $text .= "Cache dummy $res\n"; + + $res = $cache->getStatusOfSubdir("srgb"); + $text .= "Cache srgb $res\n"; + $text .= "Alias path writable = " . is_writable($aliasPath) . "\n"; $no = extension_loaded('exif') ? null : 'NOT'; diff --git a/webroot/imgs.php b/webroot/imgs.php index 50909e3..3d12226 100644 --- a/webroot/imgs.php +++ b/webroot/imgs.php @@ -1,17 +1,17 @@ 'strict', ); class CHttpGet { private $request = array(); private $response = array(); public function __construct() { $this->request['header'] = array(); } public function buildUrl($baseUrl, $merge) { $parts = parse_url($baseUrl); $parts = array_merge($parts, $merge); $url = $parts['scheme']; $url .= "://"; $url .= $parts['host']; $url .= isset($parts['port']) ? ":" . $parts['port'] : "" ; $url .= $parts['path']; return $url; } public function setUrl($url) { $parts = parse_url($url); $path = ""; if (isset($parts['path'])) { $pathParts = explode('/', $parts['path']); unset($pathParts[0]); foreach ($pathParts as $value) { $path .= "/" . rawurlencode($value); } } $url = $this->buildUrl($url, array("path" => $path)); $this->request['url'] = $url; return $this; } public function setHeader($field, $value) { $this->request['header'][] = "$field: $value"; return $this; } public function parseHeader() { $rawHeaders = rtrim($this->response['headerRaw'], "\r\n"); $headerGroups = explode("\r\n\r\n", $rawHeaders); $header = explode("\r\n", end($headerGroups)); $output = array(); if ('HTTP' === substr($header[0], 0, 4)) { list($output['version'], $output['status']) = explode(' ', $header[0]); unset($header[0]); } foreach ($header as $entry) { $pos = strpos($entry, ':'); $output[trim(substr($entry, 0, $pos))] = trim(substr($entry, $pos + 1)); } $this->response['header'] = $output; return $this; } public function doGet($debug = false) { $options = array( CURLOPT_URL => $this->request['url'], CURLOPT_HEADER => 1, CURLOPT_HTTPHEADER => $this->request['header'], CURLOPT_AUTOREFERER => true, CURLOPT_RETURNTRANSFER => true, CURLINFO_HEADER_OUT => $debug, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 5, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 2, ); $ch = curl_init(); curl_setopt_array($ch, $options); $response = curl_exec($ch); if (!$response) { throw new Exception("Failed retrieving url, details follows: " . curl_error($ch)); } $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $this->response['headerRaw'] = substr($response, 0, $headerSize); $this->response['body'] = substr($response, $headerSize); $this->parseHeader(); if ($debug) { $info = curl_getinfo($ch); echo "Request header
", var_dump($info['request_header']), "
"; echo "Response header (raw)
", var_dump($this->response['headerRaw']), "
"; echo "Response header (parsed)
", var_dump($this->response['header']), "
"; } curl_close($ch); return true; } public function getStatus() { return isset($this->response['header']['status']) ? (int) $this->response['header']['status'] : null; } public function getLastModified() { return isset($this->response['header']['Last-Modified']) ? strtotime($this->response['header']['Last-Modified']) : null; } public function getContentType() { $type = isset($this->response['header']['Content-Type']) ? $this->response['header']['Content-Type'] : null; return preg_match('#[a-z]+/[a-z]+#', $type) ? $type : null; } public function getDate($default = false) { return isset($this->response['header']['Date']) ? strtotime($this->response['header']['Date']) : $default; } public function getMaxAge($default = false) { $cacheControl = isset($this->response['header']['Cache-Control']) ? $this->response['header']['Cache-Control'] : null; $maxAge = null; if ($cacheControl) { $part = explode('=', $cacheControl); $maxAge = ($part[0] == "max-age") ? (int) $part[1] : null; } if ($maxAge) { return $maxAge; } $expire = isset($this->response['header']['Expires']) ? strtotime($this->response['header']['Expires']) : null; return $expire ? $expire : $default; } public function getBody() { return $this->response['body']; } } class CRemoteImage { private $saveFolder = null; private $useCache = true; private $http; private $status; private $defaultMaxAge = 604800; private $url; private $fileName; private $fileJson; private $cache; public function getStatus() { return $this->status; } public function getDetails() { return $this->cache; } public function setCache($path) { $this->saveFolder = $path; return $this; } public function isCacheWritable() { if (!is_writable($this->saveFolder)) { throw new Exception("Cache folder is not writable for downloaded files."); } return $this; } public function useCache($use = true) { $this->useCache = $use; return $this; } public function setHeaderFields() { $this->http->setHeader("User-Agent", "CImage/0.7.2 (PHP/". phpversion() . " cURL)"); $this->http->setHeader("Accept", "image/jpeg,image/png,image/gif"); if ($this->useCache) { $this->http->setHeader("Cache-Control", "max-age=0"); } else { $this->http->setHeader("Cache-Control", "no-cache"); $this->http->setHeader("Pragma", "no-cache"); } } public function save() { $this->cache = array(); $date = $this->http->getDate(time()); $maxAge = $this->http->getMaxAge($this->defaultMaxAge); $lastModified = $this->http->getLastModified(); $type = $this->http->getContentType(); $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); $this->cache['Max-Age'] = $maxAge; $this->cache['Content-Type'] = $type; $this->cache['Url'] = $this->url; if ($lastModified) { $this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified); } $body = $this->http->getBody(); $img = imagecreatefromstring($body); if ($img !== false) { file_put_contents($this->fileName, $body); file_put_contents($this->fileJson, json_encode($this->cache)); return $this->fileName; } return false; } public function updateCacheDetails() { $date = $this->http->getDate(time()); $maxAge = $this->http->getMaxAge($this->defaultMaxAge); $lastModified = $this->http->getLastModified(); $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); $this->cache['Max-Age'] = $maxAge; if ($lastModified) { $this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified); } file_put_contents($this->fileJson, json_encode($this->cache)); return $this->fileName; } public function download($url) { $this->http = new CHttpGet(); $this->url = $url; $this->loadCacheDetails(); if ($this->useCache) { $src = $this->getCachedSource(); if ($src) { $this->status = 1; return $src; } } $this->setHeaderFields(); $this->http->setUrl($this->url); $this->http->doGet(); $this->status = $this->http->getStatus(); if ($this->status === 200) { $this->isCacheWritable(); return $this->save(); } elseif ($this->status === 304) { $this->isCacheWritable(); return $this->updateCacheDetails(); } throw new Exception("Unknown statuscode when downloading remote image: " . $this->status); } public function loadCacheDetails() { $cacheFile = md5($this->url); $this->fileName = $this->saveFolder . $cacheFile; $this->fileJson = $this->fileName . ".json"; if (is_readable($this->fileJson)) { $this->cache = json_decode(file_get_contents($this->fileJson), true); } } public function getCachedSource() { $imageExists = is_readable($this->fileName); $date = strtotime($this->cache['Date']); $maxAge = $this->cache['Max-Age']; $now = time(); if ($imageExists && $date + $maxAge > $now) { return $this->fileName; } if ($imageExists && isset($this->cache['Last-Modified'])) { $this->http->setHeader("If-Modified-Since", $this->cache['Last-Modified']); } return false; } } class CWhitelist { private $whitelist = array(); public function set($whitelist = array()) { if (!is_array($whitelist)) { throw new Exception("Whitelist is not of a supported format."); } $this->whitelist = $whitelist; return $this; } public function check($item, $whitelist = null) { if ($whitelist !== null) { $this->set($whitelist); } if (empty($item) or empty($this->whitelist)) { return false; } foreach ($this->whitelist as $regexp) { if (preg_match("#$regexp#", $item)) { return true; } } return false; } } class CAsciiArt { private $characterSet = array( 'one' => "#0XT|:,.' ", 'two' => "@%#*+=-:. ", 'three' => "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. " ); private $characters = null; private $charCount = null; private $scale = null; private $luminanceStrategy = null; public function __construct() { $this->setOptions(); } public function addCharacterSet($key, $value) { $this->characterSet[$key] = $value; return $this; } public function setOptions($options = array()) { $default = array( "characterSet" => 'two', "scale" => 14, "luminanceStrategy" => 3, "customCharacterSet" => null, ); $default = array_merge($default, $options); if (!is_null($default['customCharacterSet'])) { $this->addCharacterSet('custom', $default['customCharacterSet']); $default['characterSet'] = 'custom'; } $this->scale = $default['scale']; $this->characters = $this->characterSet[$default['characterSet']]; $this->charCount = strlen($this->characters); $this->luminanceStrategy = $default['luminanceStrategy']; return $this; } public function createFromFile($filename) { $img = imagecreatefromstring(file_get_contents($filename)); list($width, $height) = getimagesize($filename); $ascii = null; $incY = $this->scale; $incX = $this->scale / 2; for ($y = 0; $y < $height - 1; $y += $incY) { for ($x = 0; $x < $width - 1; $x += $incX) { $toX = min($x + $this->scale / 2, $width - 1); $toY = min($y + $this->scale, $height - 1); $luminance = $this->luminanceAreaAverage($img, $x, $y, $toX, $toY); $ascii .= $this->luminance2character($luminance); } $ascii .= PHP_EOL; } return $ascii; } public function luminanceAreaAverage($img, $x1, $y1, $x2, $y2) { $numPixels = ($x2 - $x1 + 1) * ($y2 - $y1 + 1); $luminance = 0; for ($x = $x1; $x <= $x2; $x++) { for ($y = $y1; $y <= $y2; $y++) { $rgb = imagecolorat($img, $x, $y); $red = (($rgb >> 16) & 0xFF); $green = (($rgb >> 8) & 0xFF); $blue = ($rgb & 0xFF); $luminance += $this->getLuminance($red, $green, $blue); } } return $luminance / $numPixels; } public function getLuminance($red, $green, $blue) { switch($this->luminanceStrategy) { case 1: $luminance = ($red * 0.2126 + $green * 0.7152 + $blue * 0.0722) / 255; break; case 2: $luminance = ($red * 0.299 + $green * 0.587 + $blue * 0.114) / 255; break; case 3: $luminance = sqrt(0.299 * pow($red, 2) + 0.587 * pow($green, 2) + 0.114 * pow($blue, 2)) / 255; break; case 0: default: $luminance = ($red + $green + $blue) / (255 * 3); } return $luminance; } public function luminance2character($luminance) { $position = (int) round($luminance * ($this->charCount - 1)); $char = $this->characters[$position]; return $char; } } class CImage { const PNG_GREYSCALE = 0; const PNG_RGB = 2; const PNG_RGB_PALETTE = 3; const PNG_GREYSCALE_ALPHA = 4; const PNG_RGB_ALPHA = 6; const JPEG_QUALITY_DEFAULT = 60; private $quality; private $useQuality = false; const PNG_COMPRESSION_DEFAULT = -1; private $compress; private $useCompress = false; private $HTTPHeader = array(); private $bgColorDefault = array( 'red' => 0, 'green' => 0, 'blue' => 0, 'alpha' => null, ); private $bgColor; private $saveFolder; private $image; private $imageSrc; private $pathToImage; private $fileType; private $extension; private $outputFormat = null; private $verbose = false; private $log = array(); private $palette; private $cacheFileName; private $saveAs; private $pngFilter; private $pngFilterCmd; private $pngDeflate; private $pngDeflateCmd; private $jpegOptimize; private $jpegOptimizeCmd; private $width; private $height; private $newWidth; private $newWidthOrig; private $newHeight; private $newHeightOrig; private $dpr = 1; const UPSCALE_DEFAULT = true; private $upscale = self::UPSCALE_DEFAULT; public $crop; public $cropOrig; private $convolve; private $convolves = array( 'lighten' => '0,0,0, 0,12,0, 0,0,0, 9, 0', 'darken' => '0,0,0, 0,6,0, 0,0,0, 9, 0', 'sharpen' => '-1,-1,-1, -1,16,-1, -1,-1,-1, 8, 0', 'sharpen-alt' => '0,-1,0, -1,5,-1, 0,-1,0, 1, 0', 'emboss' => '1,1,-1, 1,3,-1, 1,-1,-1, 3, 0', 'emboss-alt' => '-2,-1,0, -1,1,1, 0,1,2, 1, 0', 'blur' => '1,1,1, 1,15,1, 1,1,1, 23, 0', 'gblur' => '1,2,1, 2,4,2, 1,2,1, 16, 0', 'edge' => '-1,-1,-1, -1,8,-1, -1,-1,-1, 9, 0', 'edge-alt' => '0,1,0, 1,-4,1, 0,1,0, 1, 0', 'draw' => '0,-1,0, -1,5,-1, 0,-1,0, 0, 0', 'mean' => '1,1,1, 1,1,1, 1,1,1, 9, 0', 'motion' => '1,0,0, 0,1,0, 0,0,1, 3, 0', ); private $fillToFit; private $scale; private $rotateBefore; private $rotateAfter; private $autoRotate; private $sharpen; private $emboss; private $blur; private $offset; private $fillWidth; private $fillHeight; private $allowRemote = false; private $remotePattern = '#^https?://#'; private $useCache = true; private $remoteHostWhitelist = null; private $verboseFileName = null; private $asciiOptions = array(); const RESIZE = 1; const RESAMPLE = 2; private $copyStrategy = NULL; public $keepRatio; public $cropToFit; private $cropWidth; private $cropHeight; public $crop_x; public $crop_y; public $filters; private $attr; public function __construct($imageSrc = null, $imageFolder = null, $saveFolder = null, $saveName = null) { $this->setSource($imageSrc, $imageFolder); $this->setTarget($saveFolder, $saveName); } public function setVerbose($mode = true) { $this->verbose = $mode; return $this; } public function setSaveFolder($path) { $this->saveFolder = $path; return $this; } public function useCache($use = true) { $this->useCache = $use; return $this; } public function createDummyImage($width = null, $height = null) { $this->newWidth = $this->newWidth ?: $width ?: 100; $this->newHeight = $this->newHeight ?: $height ?: 100; $this->image = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); return $this; } public function setRemoteDownload($allow, $pattern = null) { $this->allowRemote = $allow; $this->remotePattern = is_null($pattern) ? $this->remotePattern : $pattern; $this->log( "Set remote download to: " . ($this->allowRemote ? "true" : "false") . " using pattern " . $this->remotePattern ); return $this; } public function isRemoteSource($src) { $remote = preg_match($this->remotePattern, $src); $this->log("Detected remote image: " . ($remote ? "true" : "false")); return !!$remote; } public function setRemoteHostWhitelist($whitelist = null) { $this->remoteHostWhitelist = $whitelist; $this->log( "Setting remote host whitelist to: " . (is_null($whitelist) ? "null" : print_r($whitelist, 1)) ); return $this; } public function isRemoteSourceOnWhitelist($src) { if (is_null($this->remoteHostWhitelist)) { $this->log("Remote host on whitelist not configured - allowing."); return true; } $whitelist = new CWhitelist(); $hostname = parse_url($src, PHP_URL_HOST); $allow = $whitelist->check($hostname, $this->remoteHostWhitelist); $this->log( "Remote host is on whitelist: " . ($allow ? "true" : "false") ); return $allow; } private function checkFileExtension($extension) { $valid = array('jpg', 'jpeg', 'png', 'gif'); in_array(strtolower($extension), $valid) or $this->raiseError('Not a valid file extension.'); return $this; } private function normalizeFileExtension($extension = null) { $extension = strtolower($extension ? $extension : $this->extension); if ($extension == 'jpeg') { $extension = 'jpg'; } return $extension; } public function downloadRemoteSource($src) { if (!$this->isRemoteSourceOnWhitelist($src)) { throw new Exception("Hostname is not on whitelist for remote sources."); } $remote = new CRemoteImage(); $cache = $this->saveFolder . "/remote/"; if (!is_dir($cache)) { if (!is_writable($this->saveFolder)) { throw new Exception("Can not create remote cache, cachefolder not writable."); } mkdir($cache); $this->log("The remote cache does not exists, creating it."); } if (!is_writable($cache)) { $this->log("The remote cache is not writable."); } $remote->setCache($cache); $remote->useCache($this->useCache); $src = $remote->download($src); $this->log("Remote HTTP status: " . $remote->getStatus()); $this->log("Remote item is in local cache: $src"); $this->log("Remote details on cache:" . print_r($remote->getDetails(), true)); return $src; } public function setSource($src, $dir = null) { if (!isset($src)) { $this->imageSrc = null; $this->pathToImage = null; return $this; } if ($this->allowRemote && $this->isRemoteSource($src)) { $src = $this->downloadRemoteSource($src); $dir = null; } if (!isset($dir)) { $dir = dirname($src); $src = basename($src); } $this->imageSrc = ltrim($src, '/'); $imageFolder = rtrim($dir, '/'); $this->pathToImage = $imageFolder . '/' . $this->imageSrc; return $this; } public function setTarget($src = null, $dir = null) { if (!isset($src)) { $this->cacheFileName = null; return $this; } if (isset($dir)) { $this->saveFolder = rtrim($dir, '/'); } $this->cacheFileName = $this->saveFolder . '/' . $src; $this->cacheFileName = preg_replace('/^a-zA-Z0-9\.-_/', '', $this->cacheFileName); $this->log("The cache file name is: " . $this->cacheFileName); return $this; } public function getTarget() { return $this->cacheFileName; } public function setOptions($args) { $this->log("Set new options for processing image."); $defaults = array( 'newWidth' => null, 'newHeight' => null, 'aspectRatio' => null, 'keepRatio' => true, 'cropToFit' => false, 'fillToFit' => null, 'crop' => null, 'area' => null, 'upscale' => self::UPSCALE_DEFAULT, 'useCache' => true, 'useOriginal' => true, 'scale' => null, 'rotateBefore' => null, 'autoRotate' => false, 'bgColor' => null, 'palette' => null, 'filters' => null, 'sharpen' => null, 'emboss' => null, 'blur' => null, 'convolve' => null, 'rotateAfter' => null, 'outputFormat' => null, 'dpr' => 1, ); if (isset($args['crop']) && !is_array($args['crop'])) { $pices = explode(',', $args['crop']); $args['crop'] = array( 'width' => $pices[0], 'height' => $pices[1], 'start_x' => $pices[2], 'start_y' => $pices[3], ); } if (isset($args['area']) && !is_array($args['area'])) { $pices = explode(',', $args['area']); $args['area'] = array( 'top' => $pices[0], 'right' => $pices[1], 'bottom' => $pices[2], 'left' => $pices[3], ); } if (isset($args['filters']) && is_array($args['filters'])) { foreach ($args['filters'] as $key => $filterStr) { $parts = explode(',', $filterStr); $filter = $this->mapFilter($parts[0]); $filter['str'] = $filterStr; for ($i=1; $i<=$filter['argc']; $i++) { if (isset($parts[$i])) { $filter["arg{$i}"] = $parts[$i]; } else { throw new Exception( 'Missing arg to filter, review how many arguments are needed at - http://php.net/manual/en/function.imagefilter.php' ); } } $args['filters'][$key] = $filter; } } $args = array_merge($defaults, $args); foreach ($defaults as $key => $val) { $this->{$key} = $args[$key]; } if ($this->bgColor) { $this->setDefaultBackgroundColor($this->bgColor); } $this->newWidthOrig = $this->newWidth; $this->newHeightOrig = $this->newHeight; $this->cropOrig = $this->crop; return $this; } private function mapFilter($name) { $map = array( 'negate' => array('id'=>0, 'argc'=>0, 'type'=>IMG_FILTER_NEGATE), 'grayscale' => array('id'=>1, 'argc'=>0, 'type'=>IMG_FILTER_GRAYSCALE), 'brightness' => array('id'=>2, 'argc'=>1, 'type'=>IMG_FILTER_BRIGHTNESS), 'contrast' => array('id'=>3, 'argc'=>1, 'type'=>IMG_FILTER_CONTRAST), 'colorize' => array('id'=>4, 'argc'=>4, 'type'=>IMG_FILTER_COLORIZE), 'edgedetect' => array('id'=>5, 'argc'=>0, 'type'=>IMG_FILTER_EDGEDETECT), 'emboss' => array('id'=>6, 'argc'=>0, 'type'=>IMG_FILTER_EMBOSS), 'gaussian_blur' => array('id'=>7, 'argc'=>0, 'type'=>IMG_FILTER_GAUSSIAN_BLUR), 'selective_blur' => array('id'=>8, 'argc'=>0, 'type'=>IMG_FILTER_SELECTIVE_BLUR), 'mean_removal' => array('id'=>9, 'argc'=>0, 'type'=>IMG_FILTER_MEAN_REMOVAL), 'smooth' => array('id'=>10, 'argc'=>1, 'type'=>IMG_FILTER_SMOOTH), 'pixelate' => array('id'=>11, 'argc'=>2, 'type'=>IMG_FILTER_PIXELATE), ); if (isset($map[$name])) { return $map[$name]; } else { throw new Exception('No such filter.'); } } public function loadImageDetails($file = null) { $file = $file ? $file : $this->pathToImage; is_readable($file) or $this->raiseError('Image file does not exist.'); $info = list($this->width, $this->height, $this->fileType, $this->attr) = getimagesize($file); if (empty($info)) { throw new Exception("The file doesn't seem to be a valid image."); } if ($this->verbose) { $this->log("Loading image details for: {$file}"); $this->log(" Image width x height (type): {$this->width} x {$this->height} ({$this->fileType})."); $this->log(" Image filesize: " . filesize($file) . " bytes."); $this->log(" Image mimetype: " . image_type_to_mime_type($this->fileType)); } return $this; } public function initDimensions() { $this->log("Init dimension (before) newWidth x newHeight is {$this->newWidth} x {$this->newHeight}."); if ($this->newWidth[strlen($this->newWidth)-1] == '%') { $this->newWidth = $this->width * substr($this->newWidth, 0, -1) / 100; $this->log("Setting new width based on % to {$this->newWidth}"); } if ($this->newHeight[strlen($this->newHeight)-1] == '%') { $this->newHeight = $this->height * substr($this->newHeight, 0, -1) / 100; $this->log("Setting new height based on % to {$this->newHeight}"); } is_null($this->aspectRatio) or is_numeric($this->aspectRatio) or $this->raiseError('Aspect ratio out of range'); if ($this->aspectRatio && is_null($this->newWidth) && is_null($this->newHeight)) { if ($this->aspectRatio >= 1) { $this->newWidth = $this->width; $this->newHeight = $this->width / $this->aspectRatio; $this->log("Setting new width & height based on width & aspect ratio (>=1) to (w x h) {$this->newWidth} x {$this->newHeight}"); } else { $this->newHeight = $this->height; $this->newWidth = $this->height * $this->aspectRatio; $this->log("Setting new width & height based on width & aspect ratio (<1) to (w x h) {$this->newWidth} x {$this->newHeight}"); } } elseif ($this->aspectRatio && is_null($this->newWidth)) { $this->newWidth = $this->newHeight * $this->aspectRatio; $this->log("Setting new width based on aspect ratio to {$this->newWidth}"); } elseif ($this->aspectRatio && is_null($this->newHeight)) { $this->newHeight = $this->newWidth / $this->aspectRatio; $this->log("Setting new height based on aspect ratio to {$this->newHeight}"); } if ($this->dpr != 1) { if (!is_null($this->newWidth)) { $this->newWidth = round($this->newWidth * $this->dpr); $this->log("Setting new width based on dpr={$this->dpr} - w={$this->newWidth}"); } if (!is_null($this->newHeight)) { $this->newHeight = round($this->newHeight * $this->dpr); $this->log("Setting new height based on dpr={$this->dpr} - h={$this->newHeight}"); } } is_null($this->newWidth) or is_numeric($this->newWidth) or $this->raiseError('Width not numeric'); is_null($this->newHeight) or is_numeric($this->newHeight) or $this->raiseError('Height not numeric'); $this->log("Init dimension (after) newWidth x newHeight is {$this->newWidth} x {$this->newHeight}."); return $this; } public function calculateNewWidthAndHeight() { $this->log("Calculate new width and height."); $this->log("Original width x height is {$this->width} x {$this->height}."); $this->log("Target dimension (before calculating) newWidth x newHeight is {$this->newWidth} x {$this->newHeight}."); if (isset($this->area)) { $this->offset['top'] = round($this->area['top'] / 100 * $this->height); $this->offset['right'] = round($this->area['right'] / 100 * $this->width); $this->offset['bottom'] = round($this->area['bottom'] / 100 * $this->height); $this->offset['left'] = round($this->area['left'] / 100 * $this->width); $this->offset['width'] = $this->width - $this->offset['left'] - $this->offset['right']; $this->offset['height'] = $this->height - $this->offset['top'] - $this->offset['bottom']; $this->width = $this->offset['width']; $this->height = $this->offset['height']; $this->log("The offset for the area to use is top {$this->area['top']}%, right {$this->area['right']}%, bottom {$this->area['bottom']}%, left {$this->area['left']}%."); $this->log("The offset for the area to use is top {$this->offset['top']}px, right {$this->offset['right']}px, bottom {$this->offset['bottom']}px, left {$this->offset['left']}px, width {$this->offset['width']}px, height {$this->offset['height']}px."); } $width = $this->width; $height = $this->height; if ($this->crop) { $width = $this->crop['width'] = $this->crop['width'] <= 0 ? $this->width + $this->crop['width'] : $this->crop['width']; $height = $this->crop['height'] = $this->crop['height'] <= 0 ? $this->height + $this->crop['height'] : $this->crop['height']; if ($this->crop['start_x'] == 'left') { $this->crop['start_x'] = 0; } elseif ($this->crop['start_x'] == 'right') { $this->crop['start_x'] = $this->width - $width; } elseif ($this->crop['start_x'] == 'center') { $this->crop['start_x'] = round($this->width / 2) - round($width / 2); } if ($this->crop['start_y'] == 'top') { $this->crop['start_y'] = 0; } elseif ($this->crop['start_y'] == 'bottom') { $this->crop['start_y'] = $this->height - $height; } elseif ($this->crop['start_y'] == 'center') { $this->crop['start_y'] = round($this->height / 2) - round($height / 2); } $this->log("Crop area is width {$width}px, height {$height}px, start_x {$this->crop['start_x']}px, start_y {$this->crop['start_y']}px."); } if ($this->keepRatio) { $this->log("Keep aspect ratio."); if (($this->cropToFit || $this->fillToFit) && isset($this->newWidth) && isset($this->newHeight)) { $this->log("Use newWidth and newHeigh as width/height, image should fit in box."); } elseif (isset($this->newWidth) && isset($this->newHeight)) { $ratioWidth = $width / $this->newWidth; $ratioHeight = $height / $this->newHeight; $ratio = ($ratioWidth > $ratioHeight) ? $ratioWidth : $ratioHeight; $this->newWidth = round($width / $ratio); $this->newHeight = round($height / $ratio); $this->log("New width and height was set."); } elseif (isset($this->newWidth)) { $factor = (float)$this->newWidth / (float)$width; $this->newHeight = round($factor * $height); $this->log("New width was set."); } elseif (isset($this->newHeight)) { $factor = (float)$this->newHeight / (float)$height; $this->newWidth = round($factor * $width); $this->log("New height was set."); } if ($this->cropToFit || $this->fillToFit) { $ratioWidth = $width / $this->newWidth; $ratioHeight = $height / $this->newHeight; if ($this->cropToFit) { $this->log("Crop to fit."); $ratio = ($ratioWidth < $ratioHeight) ? $ratioWidth : $ratioHeight; $this->cropWidth = round($width / $ratio); $this->cropHeight = round($height / $ratio); $this->log("Crop width, height, ratio: $this->cropWidth x $this->cropHeight ($ratio)."); } elseif ($this->fillToFit) { $this->log("Fill to fit."); $ratio = ($ratioWidth < $ratioHeight) ? $ratioHeight : $ratioWidth; $this->fillWidth = round($width / $ratio); $this->fillHeight = round($height / $ratio); $this->log("Fill width, height, ratio: $this->fillWidth x $this->fillHeight ($ratio)."); } } } if ($this->crop) { $this->log("Crop."); $this->newWidth = round(isset($this->newWidth) ? $this->newWidth : $this->crop['width']); $this->newHeight = round(isset($this->newHeight) ? $this->newHeight : $this->crop['height']); } $this->newWidth = round(isset($this->newWidth) ? $this->newWidth : $this->width); $this->newHeight = round(isset($this->newHeight) ? $this->newHeight : $this->height); $this->log("Calculated new width x height as {$this->newWidth} x {$this->newHeight}."); return $this; } public function reCalculateDimensions() { $this->log("Re-calculate image dimensions, newWidth x newHeigh was: " . $this->newWidth . " x " . $this->newHeight); $this->newWidth = $this->newWidthOrig; $this->newHeight = $this->newHeightOrig; $this->crop = $this->cropOrig; $this->initDimensions() ->calculateNewWidthAndHeight(); return $this; } public function setSaveAsExtension($saveAs = null) { if (isset($saveAs)) { $saveAs = strtolower($saveAs); $this->checkFileExtension($saveAs); $this->saveAs = $saveAs; $this->extension = $saveAs; } $this->log("Prepare to save image as: " . $this->extension); return $this; } public function setJpegQuality($quality = null) { if ($quality) { $this->useQuality = true; } $this->quality = isset($quality) ? $quality : self::JPEG_QUALITY_DEFAULT; (is_numeric($this->quality) and $this->quality > 0 and $this->quality <= 100) or $this->raiseError('Quality not in range.'); $this->log("Setting JPEG quality to {$this->quality}."); return $this; } public function setPngCompression($compress = null) { if ($compress) { $this->useCompress = true; } $this->compress = isset($compress) ? $compress : self::PNG_COMPRESSION_DEFAULT; (is_numeric($this->compress) and $this->compress >= -1 and $this->compress <= 9) or $this->raiseError('Quality not in range.'); $this->log("Setting PNG compression level to {$this->compress}."); return $this; } public function useOriginalIfPossible($useOrig = true) { if ($useOrig && ($this->newWidth == $this->width) && ($this->newHeight == $this->height) && !$this->area && !$this->crop && !$this->cropToFit && !$this->fillToFit && !$this->filters && !$this->sharpen && !$this->emboss && !$this->blur && !$this->convolve && !$this->palette && !$this->useQuality && !$this->useCompress && !$this->saveAs && !$this->rotateBefore && !$this->rotateAfter && !$this->autoRotate && !$this->bgColor && ($this->upscale === self::UPSCALE_DEFAULT) ) { $this->log("Using original image."); $this->output($this->pathToImage); } return $this; } public function generateFilename($base = null, $useSubdir = true, $prefix = null) { $filename = basename($this->pathToImage); $cropToFit = $this->cropToFit ? '_cf' : null; $fillToFit = $this->fillToFit ? '_ff' : null; $crop_x = $this->crop_x ? "_x{$this->crop_x}" : null; $crop_y = $this->crop_y ? "_y{$this->crop_y}" : null; $scale = $this->scale ? "_s{$this->scale}" : null; $bgColor = $this->bgColor ? "_bgc{$this->bgColor}" : null; $quality = $this->quality ? "_q{$this->quality}" : null; $compress = $this->compress ? "_co{$this->compress}" : null; $rotateBefore = $this->rotateBefore ? "_rb{$this->rotateBefore}" : null; $rotateAfter = $this->rotateAfter ? "_ra{$this->rotateAfter}" : null; $saveAs = $this->normalizeFileExtension(); $saveAs = $saveAs ? "_$saveAs" : null; $copyStrat = null; if ($this->copyStrategy === self::RESIZE) { $copyStrat = "_rs"; } $width = $this->newWidth ? '_' . $this->newWidth : null; $height = $this->newHeight ? '_' . $this->newHeight : null; $offset = isset($this->offset) ? '_o' . $this->offset['top'] . '-' . $this->offset['right'] . '-' . $this->offset['bottom'] . '-' . $this->offset['left'] : null; $crop = $this->crop ? '_c' . $this->crop['width'] . '-' . $this->crop['height'] . '-' . $this->crop['start_x'] . '-' . $this->crop['start_y'] : null; $filters = null; if (isset($this->filters)) { foreach ($this->filters as $filter) { if (is_array($filter)) { $filters .= "_f{$filter['id']}"; for ($i=1; $i<=$filter['argc']; $i++) { $filters .= "-".$filter["arg{$i}"]; } } } } $sharpen = $this->sharpen ? 's' : null; $emboss = $this->emboss ? 'e' : null; $blur = $this->blur ? 'b' : null; $palette = $this->palette ? 'p' : null; $autoRotate = $this->autoRotate ? 'ar' : null; $optimize = $this->jpegOptimize ? 'o' : null; $optimize .= $this->pngFilter ? 'f' : null; $optimize .= $this->pngDeflate ? 'd' : null; $convolve = null; if ($this->convolve) { $convolve = '_conv' . preg_replace('/[^a-zA-Z0-9]/', '', $this->convolve); } $upscale = null; if ($this->upscale !== self::UPSCALE_DEFAULT) { $upscale = '_nu'; } $subdir = null; if ($useSubdir === true) { $subdir = str_replace('/', '-', dirname($this->imageSrc)); $subdir = ($subdir == '.') ? '_.' : $subdir; $subdir .= '_'; } $file = $prefix . $subdir . $filename . $width . $height . $offset . $crop . $cropToFit . $fillToFit . $crop_x . $crop_y . $upscale . $quality . $filters . $sharpen . $emboss . $blur . $palette . $optimize . $compress . $scale . $rotateBefore . $rotateAfter . $autoRotate . $bgColor . $convolve . $copyStrat . $saveAs; return $this->setTarget($file, $base); } public function useCacheIfPossible($useCache = true) { if ($useCache && is_readable($this->cacheFileName)) { $fileTime = filemtime($this->pathToImage); $cacheTime = filemtime($this->cacheFileName); if ($fileTime <= $cacheTime) { if ($this->useCache) { if ($this->verbose) { $this->log("Use cached file."); $this->log("Cached image filesize: " . filesize($this->cacheFileName) . " bytes."); } $this->output($this->cacheFileName, $this->outputFormat); } else { $this->log("Cache is valid but ignoring it by intention."); } } else { $this->log("Original file is modified, ignoring cache."); } } else { $this->log("Cachefile does not exists or ignoring it."); } return $this; } public function load($src = null, $dir = null) { if (isset($src)) { $this->setSource($src, $dir); } $this->loadImageDetails($this->pathToImage); $this->image = imagecreatefromstring(file_get_contents($this->pathToImage)); if ($this->image === false) { throw new Exception("Could not load image."); } if ($this->verbose) { $this->log("### Image successfully loaded from file."); $this->log(" imageistruecolor() : " . (imageistruecolor($this->image) ? 'true' : 'false')); $this->log(" imagecolorstotal() : " . imagecolorstotal($this->image)); $this->log(" Number of colors in image = " . $this->colorsTotal($this->image)); $index = imagecolortransparent($this->image); $this->log(" Detected transparent color = " . ($index >= 0 ? implode(", ", imagecolorsforindex($this->image, $index)) : "NONE") . " at index = $index"); } return $this; } public function getPngType($filename = null) { $filename = $filename ? $filename : $this->pathToImage; $pngType = ord(file_get_contents($filename, false, null, 25, 1)); if ($this->verbose) { $this->log("Checking png type of: " . $filename); $this->log($this->getPngTypeAsString($pngType)); } return $pngType; } private function getPngTypeAsString($pngType = null, $filename = null) { if ($filename || !$pngType) { $pngType = $this->getPngType($filename); } $index = imagecolortransparent($this->image); $transparent = null; if ($index != -1) { $transparent = " (transparent)"; } switch ($pngType) { case self::PNG_GREYSCALE: $text = "PNG is type 0, Greyscale$transparent"; break; case self::PNG_RGB: $text = "PNG is type 2, RGB$transparent"; break; case self::PNG_RGB_PALETTE: $text = "PNG is type 3, RGB with palette$transparent"; break; case self::PNG_GREYSCALE_ALPHA: $text = "PNG is type 4, Greyscale with alpha channel"; break; case self::PNG_RGB_ALPHA: $text = "PNG is type 6, RGB with alpha channel (PNG 32-bit)"; break; default: $text = "PNG is UNKNOWN type, is it really a PNG image?"; } return $text; } private function colorsTotal($im) { if (imageistruecolor($im)) { $this->log("Colors as true color."); $h = imagesy($im); $w = imagesx($im); $c = array(); for ($x=0; $x < $w; $x++) { for ($y=0; $y < $h; $y++) { @$c['c'.imagecolorat($im, $x, $y)]++; } } return count($c); } else { $this->log("Colors as palette."); return imagecolorstotal($im); } } public function preResize() { $this->log("### Pre-process before resizing"); if ($this->rotateBefore) { $this->log("Rotating image."); $this->rotate($this->rotateBefore, $this->bgColor) ->reCalculateDimensions(); } if ($this->autoRotate) { $this->log("Auto rotating image."); $this->rotateExif() ->reCalculateDimensions(); } if (isset($this->scale)) { $this->log("Scale by {$this->scale}%"); $newWidth = $this->width * $this->scale / 100; $newHeight = $this->height * $this->scale / 100; $img = $this->CreateImageKeepTransparency($newWidth, $newHeight); imagecopyresampled($img, $this->image, 0, 0, 0, 0, $newWidth, $newHeight, $this->width, $this->height); $this->image = $img; $this->width = $newWidth; $this->height = $newHeight; } return $this; } public function setCopyResizeStrategy($strategy) { $this->copyStrategy = $strategy; return $this; } public function imageCopyResampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { if($this->copyStrategy == self::RESIZE) { $this->log("Copy by resize"); imagecopyresized($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } else { $this->log("Copy by resample"); imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } } public function resize() { $this->log("### Starting to Resize()"); $this->log("Upscale = '$this->upscale'"); if (isset($this->offset)) { $this->log("Offset for area to use, cropping it width={$this->offset['width']}, height={$this->offset['height']}, start_x={$this->offset['left']}, start_y={$this->offset['top']}"); $img = $this->CreateImageKeepTransparency($this->offset['width'], $this->offset['height']); imagecopy($img, $this->image, 0, 0, $this->offset['left'], $this->offset['top'], $this->offset['width'], $this->offset['height']); $this->image = $img; $this->width = $this->offset['width']; $this->height = $this->offset['height']; } if ($this->crop) { $this->log("Cropping area width={$this->crop['width']}, height={$this->crop['height']}, start_x={$this->crop['start_x']}, start_y={$this->crop['start_y']}"); $img = $this->CreateImageKeepTransparency($this->crop['width'], $this->crop['height']); imagecopy($img, $this->image, 0, 0, $this->crop['start_x'], $this->crop['start_y'], $this->crop['width'], $this->crop['height']); $this->image = $img; $this->width = $this->crop['width']; $this->height = $this->crop['height']; } if (!$this->upscale) { } if ($this->cropToFit) { $this->log("Resizing using strategy - Crop to fit"); if (!$this->upscale && ($this->width < $this->newWidth || $this->height < $this->newHeight)) { $this->log("Resizing - smaller image, do not upscale."); $cropX = round(($this->cropWidth/2) - ($this->newWidth/2)); $cropY = round(($this->cropHeight/2) - ($this->newHeight/2)); $posX = 0; $posY = 0; if ($this->newWidth > $this->width) { $posX = round(($this->newWidth - $this->width) / 2); } if ($this->newHeight > $this->height) { $posY = round(($this->newHeight - $this->height) / 2); } $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); imagecopy($imageResized, $this->image, $posX, $posY, $cropX, $cropY, $this->newWidth, $this->newHeight); } else { $cropX = round(($this->cropWidth/2) - ($this->newWidth/2)); $cropY = round(($this->cropHeight/2) - ($this->newHeight/2)); $imgPreCrop = $this->CreateImageKeepTransparency($this->cropWidth, $this->cropHeight); $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); $this->imageCopyResampled($imgPreCrop, $this->image, 0, 0, 0, 0, $this->cropWidth, $this->cropHeight, $this->width, $this->height); imagecopy($imageResized, $imgPreCrop, 0, 0, $cropX, $cropY, $this->newWidth, $this->newHeight); } $this->image = $imageResized; $this->width = $this->newWidth; $this->height = $this->newHeight; } elseif ($this->fillToFit) { $this->log("Resizing using strategy - Fill to fit"); $posX = 0; $posY = 0; $ratioOrig = $this->width / $this->height; $ratioNew = $this->newWidth / $this->newHeight; if ($ratioOrig < $ratioNew) { $posX = round(($this->newWidth - $this->fillWidth) / 2); } else { $posY = round(($this->newHeight - $this->fillHeight) / 2); } if (!$this->upscale && ($this->width < $this->newWidth || $this->height < $this->newHeight) ) { $this->log("Resizing - smaller image, do not upscale."); $posX = round(($this->fillWidth - $this->width) / 2); $posY = round(($this->fillHeight - $this->height) / 2); $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); imagecopy($imageResized, $this->image, $posX, $posY, 0, 0, $this->fillWidth, $this->fillHeight); } else { $imgPreFill = $this->CreateImageKeepTransparency($this->fillWidth, $this->fillHeight); $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); $this->imageCopyResampled($imgPreFill, $this->image, 0, 0, 0, 0, $this->fillWidth, $this->fillHeight, $this->width, $this->height); imagecopy($imageResized, $imgPreFill, $posX, $posY, 0, 0, $this->fillWidth, $this->fillHeight); } $this->image = $imageResized; $this->width = $this->newWidth; $this->height = $this->newHeight; } elseif (!($this->newWidth == $this->width && $this->newHeight == $this->height)) { $this->log("Resizing, new height and/or width"); if (!$this->upscale && ($this->width < $this->newWidth || $this->height < $this->newHeight) ) { $this->log("Resizing - smaller image, do not upscale."); if (!$this->keepRatio) { $this->log("Resizing - stretch to fit selected."); $posX = 0; $posY = 0; $cropX = 0; $cropY = 0; if ($this->newWidth > $this->width && $this->newHeight > $this->height) { $posX = round(($this->newWidth - $this->width) / 2); $posY = round(($this->newHeight - $this->height) / 2); } elseif ($this->newWidth > $this->width) { $posX = round(($this->newWidth - $this->width) / 2); $cropY = round(($this->height - $this->newHeight) / 2); } elseif ($this->newHeight > $this->height) { $posY = round(($this->newHeight - $this->height) / 2); $cropX = round(($this->width - $this->newWidth) / 2); } $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); imagecopy($imageResized, $this->image, $posX, $posY, $cropX, $cropY, $this->newWidth, $this->newHeight); $this->image = $imageResized; $this->width = $this->newWidth; $this->height = $this->newHeight; } } else { $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); $this->imageCopyResampled($imageResized, $this->image, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height); $this->image = $imageResized; $this->width = $this->newWidth; $this->height = $this->newHeight; } } return $this; } public function postResize() { $this->log("### Post-process after resizing"); if ($this->rotateAfter) { $this->log("Rotating image."); $this->rotate($this->rotateAfter, $this->bgColor); } if (isset($this->filters) && is_array($this->filters)) { foreach ($this->filters as $filter) { $this->log("Applying filter {$filter['type']}."); switch ($filter['argc']) { case 0: imagefilter($this->image, $filter['type']); break; case 1: imagefilter($this->image, $filter['type'], $filter['arg1']); break; case 2: imagefilter($this->image, $filter['type'], $filter['arg1'], $filter['arg2']); break; case 3: imagefilter($this->image, $filter['type'], $filter['arg1'], $filter['arg2'], $filter['arg3']); break; case 4: imagefilter($this->image, $filter['type'], $filter['arg1'], $filter['arg2'], $filter['arg3'], $filter['arg4']); break; } } } if ($this->palette) { $this->log("Converting to palette image."); $this->trueColorToPalette(); } if ($this->blur) { $this->log("Blur."); $this->blurImage(); } if ($this->emboss) { $this->log("Emboss."); $this->embossImage(); } if ($this->sharpen) { $this->log("Sharpen."); $this->sharpenImage(); } if ($this->convolve) { $this->imageConvolution(); } return $this; } public function rotate($angle, $bgColor) { $this->log("Rotate image " . $angle . " degrees with filler color."); $color = $this->getBackgroundColor(); $this->image = imagerotate($this->image, $angle, $color); $this->width = imagesx($this->image); $this->height = imagesy($this->image); $this->log("New image dimension width x height: " . $this->width . " x " . $this->height); return $this; } public function rotateExif() { if (!in_array($this->fileType, array(IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM))) { $this->log("Autorotate ignored, EXIF not supported by this filetype."); return $this; } $exif = exif_read_data($this->pathToImage); if (!empty($exif['Orientation'])) { switch ($exif['Orientation']) { case 3: $this->log("Autorotate 180."); $this->rotate(180, $this->bgColor); break; case 6: $this->log("Autorotate -90."); $this->rotate(-90, $this->bgColor); break; case 8: $this->log("Autorotate 90."); $this->rotate(90, $this->bgColor); break; default: $this->log("Autorotate ignored, unknown value as orientation."); } } else { $this->log("Autorotate ignored, no orientation in EXIF."); } return $this; } public function trueColorToPalette() { $img = imagecreatetruecolor($this->width, $this->height); $bga = imagecolorallocatealpha($img, 0, 0, 0, 127); imagecolortransparent($img, $bga); imagefill($img, 0, 0, $bga); imagecopy($img, $this->image, 0, 0, 0, 0, $this->width, $this->height); imagetruecolortopalette($img, false, 255); imagesavealpha($img, true); if (imageistruecolor($this->image)) { $this->log("Matching colors with true color image."); imagecolormatch($this->image, $img); } $this->image = $img; } public function sharpenImage() { $this->imageConvolution('sharpen'); return $this; } public function embossImage() { $this->imageConvolution('emboss'); return $this; } public function blurImage() { $this->imageConvolution('blur'); return $this; } public function createConvolveArguments($expression) { if (isset($this->convolves[$expression])) { $expression = $this->convolves[$expression]; } $part = explode(',', $expression); $this->log("Creating convolution expressen: $expression"); if (count($part) != 11) { throw new Exception( "Missmatch in argument convolve. Expected comma-separated string with + $config = array( 'mode' => 'strict', ); class CHttpGet { private $request = array(); private $response = array(); public function __construct() { $this->request['header'] = array(); } public function buildUrl($baseUrl, $merge) { $parts = parse_url($baseUrl); $parts = array_merge($parts, $merge); $url = $parts['scheme']; $url .= "://"; $url .= $parts['host']; $url .= isset($parts['port']) ? ":" . $parts['port'] : "" ; $url .= $parts['path']; return $url; } public function setUrl($url) { $parts = parse_url($url); $path = ""; if (isset($parts['path'])) { $pathParts = explode('/', $parts['path']); unset($pathParts[0]); foreach ($pathParts as $value) { $path .= "/" . rawurlencode($value); } } $url = $this->buildUrl($url, array("path" => $path)); $this->request['url'] = $url; return $this; } public function setHeader($field, $value) { $this->request['header'][] = "$field: $value"; return $this; } public function parseHeader() { $rawHeaders = rtrim($this->response['headerRaw'], "\r\n"); $headerGroups = explode("\r\n\r\n", $rawHeaders); $header = explode("\r\n", end($headerGroups)); $output = array(); if ('HTTP' === substr($header[0], 0, 4)) { list($output['version'], $output['status']) = explode(' ', $header[0]); unset($header[0]); } foreach ($header as $entry) { $pos = strpos($entry, ':'); $output[trim(substr($entry, 0, $pos))] = trim(substr($entry, $pos + 1)); } $this->response['header'] = $output; return $this; } public function doGet($debug = false) { $options = array( CURLOPT_URL => $this->request['url'], CURLOPT_HEADER => 1, CURLOPT_HTTPHEADER => $this->request['header'], CURLOPT_AUTOREFERER => true, CURLOPT_RETURNTRANSFER => true, CURLINFO_HEADER_OUT => $debug, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 5, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 2, ); $ch = curl_init(); curl_setopt_array($ch, $options); $response = curl_exec($ch); if (!$response) { throw new Exception("Failed retrieving url, details follows: " . curl_error($ch)); } $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $this->response['headerRaw'] = substr($response, 0, $headerSize); $this->response['body'] = substr($response, $headerSize); $this->parseHeader(); if ($debug) { $info = curl_getinfo($ch); echo "Request header
", var_dump($info['request_header']), "
"; echo "Response header (raw)
", var_dump($this->response['headerRaw']), "
"; echo "Response header (parsed)
", var_dump($this->response['header']), "
"; } curl_close($ch); return true; } public function getStatus() { return isset($this->response['header']['status']) ? (int) $this->response['header']['status'] : null; } public function getLastModified() { return isset($this->response['header']['Last-Modified']) ? strtotime($this->response['header']['Last-Modified']) : null; } public function getContentType() { $type = isset($this->response['header']['Content-Type']) ? $this->response['header']['Content-Type'] : null; return preg_match('#[a-z]+/[a-z]+#', $type) ? $type : null; } public function getDate($default = false) { return isset($this->response['header']['Date']) ? strtotime($this->response['header']['Date']) : $default; } public function getMaxAge($default = false) { $cacheControl = isset($this->response['header']['Cache-Control']) ? $this->response['header']['Cache-Control'] : null; $maxAge = null; if ($cacheControl) { $part = explode('=', $cacheControl); $maxAge = ($part[0] == "max-age") ? (int) $part[1] : null; } if ($maxAge) { return $maxAge; } $expire = isset($this->response['header']['Expires']) ? strtotime($this->response['header']['Expires']) : null; return $expire ? $expire : $default; } public function getBody() { return $this->response['body']; } } class CRemoteImage { private $saveFolder = null; private $useCache = true; private $http; private $status; private $defaultMaxAge = 604800; private $url; private $fileName; private $fileJson; private $cache; public function getStatus() { return $this->status; } public function getDetails() { return $this->cache; } public function setCache($path) { $this->saveFolder = $path; return $this; } public function isCacheWritable() { if (!is_writable($this->saveFolder)) { throw new Exception("Cache folder is not writable for downloaded files."); } return $this; } public function useCache($use = true) { $this->useCache = $use; return $this; } public function setHeaderFields() { $cimageVersion = "CImage"; if (defined("CIMAGE_USER_AGENT")) { $cimageVersion = CIMAGE_USER_AGENT; } $this->http->setHeader("User-Agent", "$cimageVersion (PHP/". phpversion() . " cURL)"); $this->http->setHeader("Accept", "image/jpeg,image/png,image/gif"); if ($this->useCache) { $this->http->setHeader("Cache-Control", "max-age=0"); } else { $this->http->setHeader("Cache-Control", "no-cache"); $this->http->setHeader("Pragma", "no-cache"); } } public function save() { $this->cache = array(); $date = $this->http->getDate(time()); $maxAge = $this->http->getMaxAge($this->defaultMaxAge); $lastModified = $this->http->getLastModified(); $type = $this->http->getContentType(); $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); $this->cache['Max-Age'] = $maxAge; $this->cache['Content-Type'] = $type; $this->cache['Url'] = $this->url; if ($lastModified) { $this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified); } $body = $this->http->getBody(); $img = imagecreatefromstring($body); if ($img !== false) { file_put_contents($this->fileName, $body); file_put_contents($this->fileJson, json_encode($this->cache)); return $this->fileName; } return false; } public function updateCacheDetails() { $date = $this->http->getDate(time()); $maxAge = $this->http->getMaxAge($this->defaultMaxAge); $lastModified = $this->http->getLastModified(); $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); $this->cache['Max-Age'] = $maxAge; if ($lastModified) { $this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified); } file_put_contents($this->fileJson, json_encode($this->cache)); return $this->fileName; } public function download($url) { $this->http = new CHttpGet(); $this->url = $url; $this->loadCacheDetails(); if ($this->useCache) { $src = $this->getCachedSource(); if ($src) { $this->status = 1; return $src; } } $this->setHeaderFields(); $this->http->setUrl($this->url); $this->http->doGet(); $this->status = $this->http->getStatus(); if ($this->status === 200) { $this->isCacheWritable(); return $this->save(); } elseif ($this->status === 304) { $this->isCacheWritable(); return $this->updateCacheDetails(); } throw new Exception("Unknown statuscode when downloading remote image: " . $this->status); } public function loadCacheDetails() { $cacheFile = md5($this->url); $this->fileName = $this->saveFolder . $cacheFile; $this->fileJson = $this->fileName . ".json"; if (is_readable($this->fileJson)) { $this->cache = json_decode(file_get_contents($this->fileJson), true); } } public function getCachedSource() { $imageExists = is_readable($this->fileName); $date = strtotime($this->cache['Date']); $maxAge = $this->cache['Max-Age']; $now = time(); if ($imageExists && $date + $maxAge > $now) { return $this->fileName; } if ($imageExists && isset($this->cache['Last-Modified'])) { $this->http->setHeader("If-Modified-Since", $this->cache['Last-Modified']); } return false; } } class CWhitelist { private $whitelist = array(); public function set($whitelist = array()) { if (!is_array($whitelist)) { throw new Exception("Whitelist is not of a supported format."); } $this->whitelist = $whitelist; return $this; } public function check($item, $whitelist = null) { if ($whitelist !== null) { $this->set($whitelist); } if (empty($item) or empty($this->whitelist)) { return false; } foreach ($this->whitelist as $regexp) { if (preg_match("#$regexp#", $item)) { return true; } } return false; } } class CAsciiArt { private $characterSet = array( 'one' => "#0XT|:,.' ", 'two' => "@%#*+=-:. ", 'three' => "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. " ); private $characters = null; private $charCount = null; private $scale = null; private $luminanceStrategy = null; public function __construct() { $this->setOptions(); } public function addCharacterSet($key, $value) { $this->characterSet[$key] = $value; return $this; } public function setOptions($options = array()) { $default = array( "characterSet" => 'two', "scale" => 14, "luminanceStrategy" => 3, "customCharacterSet" => null, ); $default = array_merge($default, $options); if (!is_null($default['customCharacterSet'])) { $this->addCharacterSet('custom', $default['customCharacterSet']); $default['characterSet'] = 'custom'; } $this->scale = $default['scale']; $this->characters = $this->characterSet[$default['characterSet']]; $this->charCount = strlen($this->characters); $this->luminanceStrategy = $default['luminanceStrategy']; return $this; } public function createFromFile($filename) { $img = imagecreatefromstring(file_get_contents($filename)); list($width, $height) = getimagesize($filename); $ascii = null; $incY = $this->scale; $incX = $this->scale / 2; for ($y = 0; $y < $height - 1; $y += $incY) { for ($x = 0; $x < $width - 1; $x += $incX) { $toX = min($x + $this->scale / 2, $width - 1); $toY = min($y + $this->scale, $height - 1); $luminance = $this->luminanceAreaAverage($img, $x, $y, $toX, $toY); $ascii .= $this->luminance2character($luminance); } $ascii .= PHP_EOL; } return $ascii; } public function luminanceAreaAverage($img, $x1, $y1, $x2, $y2) { $numPixels = ($x2 - $x1 + 1) * ($y2 - $y1 + 1); $luminance = 0; for ($x = $x1; $x <= $x2; $x++) { for ($y = $y1; $y <= $y2; $y++) { $rgb = imagecolorat($img, $x, $y); $red = (($rgb >> 16) & 0xFF); $green = (($rgb >> 8) & 0xFF); $blue = ($rgb & 0xFF); $luminance += $this->getLuminance($red, $green, $blue); } } return $luminance / $numPixels; } public function getLuminance($red, $green, $blue) { switch ($this->luminanceStrategy) { case 1: $luminance = ($red * 0.2126 + $green * 0.7152 + $blue * 0.0722) / 255; break; case 2: $luminance = ($red * 0.299 + $green * 0.587 + $blue * 0.114) / 255; break; case 3: $luminance = sqrt(0.299 * pow($red, 2) + 0.587 * pow($green, 2) + 0.114 * pow($blue, 2)) / 255; break; case 0: default: $luminance = ($red + $green + $blue) / (255 * 3); } return $luminance; } public function luminance2character($luminance) { $position = (int) round($luminance * ($this->charCount - 1)); $char = $this->characters[$position]; return $char; } } class CImage { const PNG_GREYSCALE = 0; const PNG_RGB = 2; const PNG_RGB_PALETTE = 3; const PNG_GREYSCALE_ALPHA = 4; const PNG_RGB_ALPHA = 6; const JPEG_QUALITY_DEFAULT = 60; private $quality; private $useQuality = false; const PNG_COMPRESSION_DEFAULT = -1; private $compress; private $useCompress = false; private $HTTPHeader = array(); private $bgColorDefault = array( 'red' => 0, 'green' => 0, 'blue' => 0, 'alpha' => null, ); private $bgColor; private $saveFolder; private $image; private $imageSrc; private $pathToImage; private $fileType; private $extension; private $outputFormat = null; private $verbose = false; private $log = array(); private $palette; private $cacheFileName; private $saveAs; private $pngFilter; private $pngFilterCmd; private $pngDeflate; private $pngDeflateCmd; private $jpegOptimize; private $jpegOptimizeCmd; private $width; private $height; private $newWidth; private $newWidthOrig; private $newHeight; private $newHeightOrig; private $dpr = 1; const UPSCALE_DEFAULT = true; private $upscale = self::UPSCALE_DEFAULT; public $crop; public $cropOrig; private $convolve; private $convolves = array( 'lighten' => '0,0,0, 0,12,0, 0,0,0, 9, 0', 'darken' => '0,0,0, 0,6,0, 0,0,0, 9, 0', 'sharpen' => '-1,-1,-1, -1,16,-1, -1,-1,-1, 8, 0', 'sharpen-alt' => '0,-1,0, -1,5,-1, 0,-1,0, 1, 0', 'emboss' => '1,1,-1, 1,3,-1, 1,-1,-1, 3, 0', 'emboss-alt' => '-2,-1,0, -1,1,1, 0,1,2, 1, 0', 'blur' => '1,1,1, 1,15,1, 1,1,1, 23, 0', 'gblur' => '1,2,1, 2,4,2, 1,2,1, 16, 0', 'edge' => '-1,-1,-1, -1,8,-1, -1,-1,-1, 9, 0', 'edge-alt' => '0,1,0, 1,-4,1, 0,1,0, 1, 0', 'draw' => '0,-1,0, -1,5,-1, 0,-1,0, 0, 0', 'mean' => '1,1,1, 1,1,1, 1,1,1, 9, 0', 'motion' => '1,0,0, 0,1,0, 0,0,1, 3, 0', ); private $fillToFit; private $scale; private $rotateBefore; private $rotateAfter; private $autoRotate; private $sharpen; private $emboss; private $blur; private $offset; private $fillWidth; private $fillHeight; private $allowRemote = false; private $remotePattern = '#^https?://#'; private $useCache = true; private $remoteHostWhitelist = null; private $verboseFileName = null; private $asciiOptions = array(); const RESIZE = 1; const RESAMPLE = 2; private $copyStrategy = null; public $keepRatio; public $cropToFit; private $cropWidth; private $cropHeight; public $crop_x; public $crop_y; public $filters; private $attr; public function __construct($imageSrc = null, $imageFolder = null, $saveFolder = null, $saveName = null) { $this->setSource($imageSrc, $imageFolder); $this->setTarget($saveFolder, $saveName); } public function setVerbose($mode = true) { $this->verbose = $mode; return $this; } public function setSaveFolder($path) { $this->saveFolder = $path; return $this; } public function useCache($use = true) { $this->useCache = $use; return $this; } public function createDummyImage($width = null, $height = null) { $this->newWidth = $this->newWidth ?: $width ?: 100; $this->newHeight = $this->newHeight ?: $height ?: 100; $this->image = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); return $this; } public function setRemoteDownload($allow, $cache, $pattern = null) { $this->allowRemote = $allow; $this->remoteCache = $cache; $this->remotePattern = is_null($pattern) ? $this->remotePattern : $pattern; $this->log( "Set remote download to: " . ($this->allowRemote ? "true" : "false") . " using pattern " . $this->remotePattern ); return $this; } public function isRemoteSource($src) { $remote = preg_match($this->remotePattern, $src); $this->log("Detected remote image: " . ($remote ? "true" : "false")); return !!$remote; } public function setRemoteHostWhitelist($whitelist = null) { $this->remoteHostWhitelist = $whitelist; $this->log( "Setting remote host whitelist to: " . (is_null($whitelist) ? "null" : print_r($whitelist, 1)) ); return $this; } public function isRemoteSourceOnWhitelist($src) { if (is_null($this->remoteHostWhitelist)) { $this->log("Remote host on whitelist not configured - allowing."); return true; } $whitelist = new CWhitelist(); $hostname = parse_url($src, PHP_URL_HOST); $allow = $whitelist->check($hostname, $this->remoteHostWhitelist); $this->log( "Remote host is on whitelist: " . ($allow ? "true" : "false") ); return $allow; } private function checkFileExtension($extension) { $valid = array('jpg', 'jpeg', 'png', 'gif'); in_array(strtolower($extension), $valid) or $this->raiseError('Not a valid file extension.'); return $this; } private function normalizeFileExtension($extension = null) { $extension = strtolower($extension ? $extension : $this->extension); if ($extension == 'jpeg') { $extension = 'jpg'; } return $extension; } public function downloadRemoteSource($src) { if (!$this->isRemoteSourceOnWhitelist($src)) { throw new Exception("Hostname is not on whitelist for remote sources."); } $remote = new CRemoteImage(); if (!is_writable($this->remoteCache)) { $this->log("The remote cache is not writable."); } $remote->setCache($this->remoteCache); $remote->useCache($this->useCache); $src = $remote->download($src); $this->log("Remote HTTP status: " . $remote->getStatus()); $this->log("Remote item is in local cache: $src"); $this->log("Remote details on cache:" . print_r($remote->getDetails(), true)); return $src; } public function setSource($src, $dir = null) { if (!isset($src)) { $this->imageSrc = null; $this->pathToImage = null; return $this; } if ($this->allowRemote && $this->isRemoteSource($src)) { $src = $this->downloadRemoteSource($src); $dir = null; } if (!isset($dir)) { $dir = dirname($src); $src = basename($src); } $this->imageSrc = ltrim($src, '/'); $imageFolder = rtrim($dir, '/'); $this->pathToImage = $imageFolder . '/' . $this->imageSrc; return $this; } public function setTarget($src = null, $dir = null) { if (!isset($src)) { $this->cacheFileName = null; return $this; } if (isset($dir)) { $this->saveFolder = rtrim($dir, '/'); } $this->cacheFileName = $this->saveFolder . '/' . $src; $this->cacheFileName = preg_replace('/^a-zA-Z0-9\.-_/', '', $this->cacheFileName); $this->log("The cache file name is: " . $this->cacheFileName); return $this; } public function getTarget() { return $this->cacheFileName; } public function setOptions($args) { $this->log("Set new options for processing image."); $defaults = array( 'newWidth' => null, 'newHeight' => null, 'aspectRatio' => null, 'keepRatio' => true, 'cropToFit' => false, 'fillToFit' => null, 'crop' => null, 'area' => null, 'upscale' => self::UPSCALE_DEFAULT, 'useCache' => true, 'useOriginal' => true, 'scale' => null, 'rotateBefore' => null, 'autoRotate' => false, 'bgColor' => null, 'palette' => null, 'filters' => null, 'sharpen' => null, 'emboss' => null, 'blur' => null, 'convolve' => null, 'rotateAfter' => null, 'outputFormat' => null, 'dpr' => 1, ); if (isset($args['crop']) && !is_array($args['crop'])) { $pices = explode(',', $args['crop']); $args['crop'] = array( 'width' => $pices[0], 'height' => $pices[1], 'start_x' => $pices[2], 'start_y' => $pices[3], ); } if (isset($args['area']) && !is_array($args['area'])) { $pices = explode(',', $args['area']); $args['area'] = array( 'top' => $pices[0], 'right' => $pices[1], 'bottom' => $pices[2], 'left' => $pices[3], ); } if (isset($args['filters']) && is_array($args['filters'])) { foreach ($args['filters'] as $key => $filterStr) { $parts = explode(',', $filterStr); $filter = $this->mapFilter($parts[0]); $filter['str'] = $filterStr; for ($i=1; $i<=$filter['argc']; $i++) { if (isset($parts[$i])) { $filter["arg{$i}"] = $parts[$i]; } else { throw new Exception( 'Missing arg to filter, review how many arguments are needed at + http://php.net/manual/en/function.imagefilter.php' ); } } $args['filters'][$key] = $filter; } } $args = array_merge($defaults, $args); foreach ($defaults as $key => $val) { $this->{$key} = $args[$key]; } if ($this->bgColor) { $this->setDefaultBackgroundColor($this->bgColor); } $this->newWidthOrig = $this->newWidth; $this->newHeightOrig = $this->newHeight; $this->cropOrig = $this->crop; return $this; } private function mapFilter($name) { $map = array( 'negate' => array('id'=>0, 'argc'=>0, 'type'=>IMG_FILTER_NEGATE), 'grayscale' => array('id'=>1, 'argc'=>0, 'type'=>IMG_FILTER_GRAYSCALE), 'brightness' => array('id'=>2, 'argc'=>1, 'type'=>IMG_FILTER_BRIGHTNESS), 'contrast' => array('id'=>3, 'argc'=>1, 'type'=>IMG_FILTER_CONTRAST), 'colorize' => array('id'=>4, 'argc'=>4, 'type'=>IMG_FILTER_COLORIZE), 'edgedetect' => array('id'=>5, 'argc'=>0, 'type'=>IMG_FILTER_EDGEDETECT), 'emboss' => array('id'=>6, 'argc'=>0, 'type'=>IMG_FILTER_EMBOSS), 'gaussian_blur' => array('id'=>7, 'argc'=>0, 'type'=>IMG_FILTER_GAUSSIAN_BLUR), 'selective_blur' => array('id'=>8, 'argc'=>0, 'type'=>IMG_FILTER_SELECTIVE_BLUR), 'mean_removal' => array('id'=>9, 'argc'=>0, 'type'=>IMG_FILTER_MEAN_REMOVAL), 'smooth' => array('id'=>10, 'argc'=>1, 'type'=>IMG_FILTER_SMOOTH), 'pixelate' => array('id'=>11, 'argc'=>2, 'type'=>IMG_FILTER_PIXELATE), ); if (isset($map[$name])) { return $map[$name]; } else { throw new Exception('No such filter.'); } } public function loadImageDetails($file = null) { $file = $file ? $file : $this->pathToImage; is_readable($file) or $this->raiseError('Image file does not exist.'); $info = list($this->width, $this->height, $this->fileType, $this->attr) = getimagesize($file); if (empty($info)) { throw new Exception("The file doesn't seem to be a valid image."); } if ($this->verbose) { $this->log("Loading image details for: {$file}"); $this->log(" Image width x height (type): {$this->width} x {$this->height} ({$this->fileType})."); $this->log(" Image filesize: " . filesize($file) . " bytes."); $this->log(" Image mimetype: " . image_type_to_mime_type($this->fileType)); } return $this; } public function initDimensions() { $this->log("Init dimension (before) newWidth x newHeight is {$this->newWidth} x {$this->newHeight}."); if ($this->newWidth[strlen($this->newWidth)-1] == '%') { $this->newWidth = $this->width * substr($this->newWidth, 0, -1) / 100; $this->log("Setting new width based on % to {$this->newWidth}"); } if ($this->newHeight[strlen($this->newHeight)-1] == '%') { $this->newHeight = $this->height * substr($this->newHeight, 0, -1) / 100; $this->log("Setting new height based on % to {$this->newHeight}"); } is_null($this->aspectRatio) or is_numeric($this->aspectRatio) or $this->raiseError('Aspect ratio out of range'); if ($this->aspectRatio && is_null($this->newWidth) && is_null($this->newHeight)) { if ($this->aspectRatio >= 1) { $this->newWidth = $this->width; $this->newHeight = $this->width / $this->aspectRatio; $this->log("Setting new width & height based on width & aspect ratio (>=1) to (w x h) {$this->newWidth} x {$this->newHeight}"); } else { $this->newHeight = $this->height; $this->newWidth = $this->height * $this->aspectRatio; $this->log("Setting new width & height based on width & aspect ratio (<1) to (w x h) {$this->newWidth} x {$this->newHeight}"); } } elseif ($this->aspectRatio && is_null($this->newWidth)) { $this->newWidth = $this->newHeight * $this->aspectRatio; $this->log("Setting new width based on aspect ratio to {$this->newWidth}"); } elseif ($this->aspectRatio && is_null($this->newHeight)) { $this->newHeight = $this->newWidth / $this->aspectRatio; $this->log("Setting new height based on aspect ratio to {$this->newHeight}"); } if ($this->dpr != 1) { if (!is_null($this->newWidth)) { $this->newWidth = round($this->newWidth * $this->dpr); $this->log("Setting new width based on dpr={$this->dpr} - w={$this->newWidth}"); } if (!is_null($this->newHeight)) { $this->newHeight = round($this->newHeight * $this->dpr); $this->log("Setting new height based on dpr={$this->dpr} - h={$this->newHeight}"); } } is_null($this->newWidth) or is_numeric($this->newWidth) or $this->raiseError('Width not numeric'); is_null($this->newHeight) or is_numeric($this->newHeight) or $this->raiseError('Height not numeric'); $this->log("Init dimension (after) newWidth x newHeight is {$this->newWidth} x {$this->newHeight}."); return $this; } public function calculateNewWidthAndHeight() { $this->log("Calculate new width and height."); $this->log("Original width x height is {$this->width} x {$this->height}."); $this->log("Target dimension (before calculating) newWidth x newHeight is {$this->newWidth} x {$this->newHeight}."); if (isset($this->area)) { $this->offset['top'] = round($this->area['top'] / 100 * $this->height); $this->offset['right'] = round($this->area['right'] / 100 * $this->width); $this->offset['bottom'] = round($this->area['bottom'] / 100 * $this->height); $this->offset['left'] = round($this->area['left'] / 100 * $this->width); $this->offset['width'] = $this->width - $this->offset['left'] - $this->offset['right']; $this->offset['height'] = $this->height - $this->offset['top'] - $this->offset['bottom']; $this->width = $this->offset['width']; $this->height = $this->offset['height']; $this->log("The offset for the area to use is top {$this->area['top']}%, right {$this->area['right']}%, bottom {$this->area['bottom']}%, left {$this->area['left']}%."); $this->log("The offset for the area to use is top {$this->offset['top']}px, right {$this->offset['right']}px, bottom {$this->offset['bottom']}px, left {$this->offset['left']}px, width {$this->offset['width']}px, height {$this->offset['height']}px."); } $width = $this->width; $height = $this->height; if ($this->crop) { $width = $this->crop['width'] = $this->crop['width'] <= 0 ? $this->width + $this->crop['width'] : $this->crop['width']; $height = $this->crop['height'] = $this->crop['height'] <= 0 ? $this->height + $this->crop['height'] : $this->crop['height']; if ($this->crop['start_x'] == 'left') { $this->crop['start_x'] = 0; } elseif ($this->crop['start_x'] == 'right') { $this->crop['start_x'] = $this->width - $width; } elseif ($this->crop['start_x'] == 'center') { $this->crop['start_x'] = round($this->width / 2) - round($width / 2); } if ($this->crop['start_y'] == 'top') { $this->crop['start_y'] = 0; } elseif ($this->crop['start_y'] == 'bottom') { $this->crop['start_y'] = $this->height - $height; } elseif ($this->crop['start_y'] == 'center') { $this->crop['start_y'] = round($this->height / 2) - round($height / 2); } $this->log("Crop area is width {$width}px, height {$height}px, start_x {$this->crop['start_x']}px, start_y {$this->crop['start_y']}px."); } if ($this->keepRatio) { $this->log("Keep aspect ratio."); if (($this->cropToFit || $this->fillToFit) && isset($this->newWidth) && isset($this->newHeight)) { $this->log("Use newWidth and newHeigh as width/height, image should fit in box."); } elseif (isset($this->newWidth) && isset($this->newHeight)) { $ratioWidth = $width / $this->newWidth; $ratioHeight = $height / $this->newHeight; $ratio = ($ratioWidth > $ratioHeight) ? $ratioWidth : $ratioHeight; $this->newWidth = round($width / $ratio); $this->newHeight = round($height / $ratio); $this->log("New width and height was set."); } elseif (isset($this->newWidth)) { $factor = (float)$this->newWidth / (float)$width; $this->newHeight = round($factor * $height); $this->log("New width was set."); } elseif (isset($this->newHeight)) { $factor = (float)$this->newHeight / (float)$height; $this->newWidth = round($factor * $width); $this->log("New height was set."); } if ($this->cropToFit || $this->fillToFit) { $ratioWidth = $width / $this->newWidth; $ratioHeight = $height / $this->newHeight; if ($this->cropToFit) { $this->log("Crop to fit."); $ratio = ($ratioWidth < $ratioHeight) ? $ratioWidth : $ratioHeight; $this->cropWidth = round($width / $ratio); $this->cropHeight = round($height / $ratio); $this->log("Crop width, height, ratio: $this->cropWidth x $this->cropHeight ($ratio)."); } elseif ($this->fillToFit) { $this->log("Fill to fit."); $ratio = ($ratioWidth < $ratioHeight) ? $ratioHeight : $ratioWidth; $this->fillWidth = round($width / $ratio); $this->fillHeight = round($height / $ratio); $this->log("Fill width, height, ratio: $this->fillWidth x $this->fillHeight ($ratio)."); } } } if ($this->crop) { $this->log("Crop."); $this->newWidth = round(isset($this->newWidth) ? $this->newWidth : $this->crop['width']); $this->newHeight = round(isset($this->newHeight) ? $this->newHeight : $this->crop['height']); } $this->newWidth = round(isset($this->newWidth) ? $this->newWidth : $this->width); $this->newHeight = round(isset($this->newHeight) ? $this->newHeight : $this->height); $this->log("Calculated new width x height as {$this->newWidth} x {$this->newHeight}."); return $this; } public function reCalculateDimensions() { $this->log("Re-calculate image dimensions, newWidth x newHeigh was: " . $this->newWidth . " x " . $this->newHeight); $this->newWidth = $this->newWidthOrig; $this->newHeight = $this->newHeightOrig; $this->crop = $this->cropOrig; $this->initDimensions() ->calculateNewWidthAndHeight(); return $this; } public function setSaveAsExtension($saveAs = null) { if (isset($saveAs)) { $saveAs = strtolower($saveAs); $this->checkFileExtension($saveAs); $this->saveAs = $saveAs; $this->extension = $saveAs; } $this->log("Prepare to save image as: " . $this->extension); return $this; } public function setJpegQuality($quality = null) { if ($quality) { $this->useQuality = true; } $this->quality = isset($quality) ? $quality : self::JPEG_QUALITY_DEFAULT; (is_numeric($this->quality) and $this->quality > 0 and $this->quality <= 100) or $this->raiseError('Quality not in range.'); $this->log("Setting JPEG quality to {$this->quality}."); return $this; } public function setPngCompression($compress = null) { if ($compress) { $this->useCompress = true; } $this->compress = isset($compress) ? $compress : self::PNG_COMPRESSION_DEFAULT; (is_numeric($this->compress) and $this->compress >= -1 and $this->compress <= 9) or $this->raiseError('Quality not in range.'); $this->log("Setting PNG compression level to {$this->compress}."); return $this; } public function useOriginalIfPossible($useOrig = true) { if ($useOrig && ($this->newWidth == $this->width) && ($this->newHeight == $this->height) && !$this->area && !$this->crop && !$this->cropToFit && !$this->fillToFit && !$this->filters && !$this->sharpen && !$this->emboss && !$this->blur && !$this->convolve && !$this->palette && !$this->useQuality && !$this->useCompress && !$this->saveAs && !$this->rotateBefore && !$this->rotateAfter && !$this->autoRotate && !$this->bgColor && ($this->upscale === self::UPSCALE_DEFAULT) ) { $this->log("Using original image."); $this->output($this->pathToImage); } return $this; } public function generateFilename($base = null, $useSubdir = true, $prefix = null) { $filename = basename($this->pathToImage); $cropToFit = $this->cropToFit ? '_cf' : null; $fillToFit = $this->fillToFit ? '_ff' : null; $crop_x = $this->crop_x ? "_x{$this->crop_x}" : null; $crop_y = $this->crop_y ? "_y{$this->crop_y}" : null; $scale = $this->scale ? "_s{$this->scale}" : null; $bgColor = $this->bgColor ? "_bgc{$this->bgColor}" : null; $quality = $this->quality ? "_q{$this->quality}" : null; $compress = $this->compress ? "_co{$this->compress}" : null; $rotateBefore = $this->rotateBefore ? "_rb{$this->rotateBefore}" : null; $rotateAfter = $this->rotateAfter ? "_ra{$this->rotateAfter}" : null; $saveAs = $this->normalizeFileExtension(); $saveAs = $saveAs ? "_$saveAs" : null; $copyStrat = null; if ($this->copyStrategy === self::RESIZE) { $copyStrat = "_rs"; } $width = $this->newWidth ? '_' . $this->newWidth : null; $height = $this->newHeight ? '_' . $this->newHeight : null; $offset = isset($this->offset) ? '_o' . $this->offset['top'] . '-' . $this->offset['right'] . '-' . $this->offset['bottom'] . '-' . $this->offset['left'] : null; $crop = $this->crop ? '_c' . $this->crop['width'] . '-' . $this->crop['height'] . '-' . $this->crop['start_x'] . '-' . $this->crop['start_y'] : null; $filters = null; if (isset($this->filters)) { foreach ($this->filters as $filter) { if (is_array($filter)) { $filters .= "_f{$filter['id']}"; for ($i=1; $i<=$filter['argc']; $i++) { $filters .= "-".$filter["arg{$i}"]; } } } } $sharpen = $this->sharpen ? 's' : null; $emboss = $this->emboss ? 'e' : null; $blur = $this->blur ? 'b' : null; $palette = $this->palette ? 'p' : null; $autoRotate = $this->autoRotate ? 'ar' : null; $optimize = $this->jpegOptimize ? 'o' : null; $optimize .= $this->pngFilter ? 'f' : null; $optimize .= $this->pngDeflate ? 'd' : null; $convolve = null; if ($this->convolve) { $convolve = '_conv' . preg_replace('/[^a-zA-Z0-9]/', '', $this->convolve); } $upscale = null; if ($this->upscale !== self::UPSCALE_DEFAULT) { $upscale = '_nu'; } $subdir = null; if ($useSubdir === true) { $subdir = str_replace('/', '-', dirname($this->imageSrc)); $subdir = ($subdir == '.') ? '_.' : $subdir; $subdir .= '_'; } $file = $prefix . $subdir . $filename . $width . $height . $offset . $crop . $cropToFit . $fillToFit . $crop_x . $crop_y . $upscale . $quality . $filters . $sharpen . $emboss . $blur . $palette . $optimize . $compress . $scale . $rotateBefore . $rotateAfter . $autoRotate . $bgColor . $convolve . $copyStrat . $saveAs; return $this->setTarget($file, $base); } public function useCacheIfPossible($useCache = true) { if ($useCache && is_readable($this->cacheFileName)) { $fileTime = filemtime($this->pathToImage); $cacheTime = filemtime($this->cacheFileName); if ($fileTime <= $cacheTime) { if ($this->useCache) { if ($this->verbose) { $this->log("Use cached file."); $this->log("Cached image filesize: " . filesize($this->cacheFileName) . " bytes."); } $this->output($this->cacheFileName, $this->outputFormat); } else { $this->log("Cache is valid but ignoring it by intention."); } } else { $this->log("Original file is modified, ignoring cache."); } } else { $this->log("Cachefile does not exists or ignoring it."); } return $this; } public function load($src = null, $dir = null) { if (isset($src)) { $this->setSource($src, $dir); } $this->loadImageDetails($this->pathToImage); $this->image = imagecreatefromstring(file_get_contents($this->pathToImage)); if ($this->image === false) { throw new Exception("Could not load image."); } if ($this->verbose) { $this->log("### Image successfully loaded from file."); $this->log(" imageistruecolor() : " . (imageistruecolor($this->image) ? 'true' : 'false')); $this->log(" imagecolorstotal() : " . imagecolorstotal($this->image)); $this->log(" Number of colors in image = " . $this->colorsTotal($this->image)); $index = imagecolortransparent($this->image); $this->log(" Detected transparent color = " . ($index >= 0 ? implode(", ", imagecolorsforindex($this->image, $index)) : "NONE") . " at index = $index"); } return $this; } public function getPngType($filename = null) { $filename = $filename ? $filename : $this->pathToImage; $pngType = ord(file_get_contents($filename, false, null, 25, 1)); if ($this->verbose) { $this->log("Checking png type of: " . $filename); $this->log($this->getPngTypeAsString($pngType)); } return $pngType; } private function getPngTypeAsString($pngType = null, $filename = null) { if ($filename || !$pngType) { $pngType = $this->getPngType($filename); } $index = imagecolortransparent($this->image); $transparent = null; if ($index != -1) { $transparent = " (transparent)"; } switch ($pngType) { case self::PNG_GREYSCALE: $text = "PNG is type 0, Greyscale$transparent"; break; case self::PNG_RGB: $text = "PNG is type 2, RGB$transparent"; break; case self::PNG_RGB_PALETTE: $text = "PNG is type 3, RGB with palette$transparent"; break; case self::PNG_GREYSCALE_ALPHA: $text = "PNG is type 4, Greyscale with alpha channel"; break; case self::PNG_RGB_ALPHA: $text = "PNG is type 6, RGB with alpha channel (PNG 32-bit)"; break; default: $text = "PNG is UNKNOWN type, is it really a PNG image?"; } return $text; } private function colorsTotal($im) { if (imageistruecolor($im)) { $this->log("Colors as true color."); $h = imagesy($im); $w = imagesx($im); $c = array(); for ($x=0; $x < $w; $x++) { for ($y=0; $y < $h; $y++) { @$c['c'.imagecolorat($im, $x, $y)]++; } } return count($c); } else { $this->log("Colors as palette."); return imagecolorstotal($im); } } public function preResize() { $this->log("### Pre-process before resizing"); if ($this->rotateBefore) { $this->log("Rotating image."); $this->rotate($this->rotateBefore, $this->bgColor) ->reCalculateDimensions(); } if ($this->autoRotate) { $this->log("Auto rotating image."); $this->rotateExif() ->reCalculateDimensions(); } if (isset($this->scale)) { $this->log("Scale by {$this->scale}%"); $newWidth = $this->width * $this->scale / 100; $newHeight = $this->height * $this->scale / 100; $img = $this->CreateImageKeepTransparency($newWidth, $newHeight); imagecopyresampled($img, $this->image, 0, 0, 0, 0, $newWidth, $newHeight, $this->width, $this->height); $this->image = $img; $this->width = $newWidth; $this->height = $newHeight; } return $this; } public function setCopyResizeStrategy($strategy) { $this->copyStrategy = $strategy; return $this; } public function imageCopyResampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { if ($this->copyStrategy == self::RESIZE) { $this->log("Copy by resize"); imagecopyresized($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } else { $this->log("Copy by resample"); imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } } public function resize() { $this->log("### Starting to Resize()"); $this->log("Upscale = '$this->upscale'"); if (isset($this->offset)) { $this->log("Offset for area to use, cropping it width={$this->offset['width']}, height={$this->offset['height']}, start_x={$this->offset['left']}, start_y={$this->offset['top']}"); $img = $this->CreateImageKeepTransparency($this->offset['width'], $this->offset['height']); imagecopy($img, $this->image, 0, 0, $this->offset['left'], $this->offset['top'], $this->offset['width'], $this->offset['height']); $this->image = $img; $this->width = $this->offset['width']; $this->height = $this->offset['height']; } if ($this->crop) { $this->log("Cropping area width={$this->crop['width']}, height={$this->crop['height']}, start_x={$this->crop['start_x']}, start_y={$this->crop['start_y']}"); $img = $this->CreateImageKeepTransparency($this->crop['width'], $this->crop['height']); imagecopy($img, $this->image, 0, 0, $this->crop['start_x'], $this->crop['start_y'], $this->crop['width'], $this->crop['height']); $this->image = $img; $this->width = $this->crop['width']; $this->height = $this->crop['height']; } if (!$this->upscale) { } if ($this->cropToFit) { $this->log("Resizing using strategy - Crop to fit"); if (!$this->upscale && ($this->width < $this->newWidth || $this->height < $this->newHeight)) { $this->log("Resizing - smaller image, do not upscale."); $cropX = round(($this->cropWidth/2) - ($this->newWidth/2)); $cropY = round(($this->cropHeight/2) - ($this->newHeight/2)); $posX = 0; $posY = 0; if ($this->newWidth > $this->width) { $posX = round(($this->newWidth - $this->width) / 2); } if ($this->newHeight > $this->height) { $posY = round(($this->newHeight - $this->height) / 2); } $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); imagecopy($imageResized, $this->image, $posX, $posY, $cropX, $cropY, $this->newWidth, $this->newHeight); } else { $cropX = round(($this->cropWidth/2) - ($this->newWidth/2)); $cropY = round(($this->cropHeight/2) - ($this->newHeight/2)); $imgPreCrop = $this->CreateImageKeepTransparency($this->cropWidth, $this->cropHeight); $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); $this->imageCopyResampled($imgPreCrop, $this->image, 0, 0, 0, 0, $this->cropWidth, $this->cropHeight, $this->width, $this->height); imagecopy($imageResized, $imgPreCrop, 0, 0, $cropX, $cropY, $this->newWidth, $this->newHeight); } $this->image = $imageResized; $this->width = $this->newWidth; $this->height = $this->newHeight; } elseif ($this->fillToFit) { $this->log("Resizing using strategy - Fill to fit"); $posX = 0; $posY = 0; $ratioOrig = $this->width / $this->height; $ratioNew = $this->newWidth / $this->newHeight; if ($ratioOrig < $ratioNew) { $posX = round(($this->newWidth - $this->fillWidth) / 2); } else { $posY = round(($this->newHeight - $this->fillHeight) / 2); } if (!$this->upscale && ($this->width < $this->newWidth || $this->height < $this->newHeight) ) { $this->log("Resizing - smaller image, do not upscale."); $posX = round(($this->fillWidth - $this->width) / 2); $posY = round(($this->fillHeight - $this->height) / 2); $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); imagecopy($imageResized, $this->image, $posX, $posY, 0, 0, $this->fillWidth, $this->fillHeight); } else { $imgPreFill = $this->CreateImageKeepTransparency($this->fillWidth, $this->fillHeight); $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); $this->imageCopyResampled($imgPreFill, $this->image, 0, 0, 0, 0, $this->fillWidth, $this->fillHeight, $this->width, $this->height); imagecopy($imageResized, $imgPreFill, $posX, $posY, 0, 0, $this->fillWidth, $this->fillHeight); } $this->image = $imageResized; $this->width = $this->newWidth; $this->height = $this->newHeight; } elseif (!($this->newWidth == $this->width && $this->newHeight == $this->height)) { $this->log("Resizing, new height and/or width"); if (!$this->upscale && ($this->width < $this->newWidth || $this->height < $this->newHeight) ) { $this->log("Resizing - smaller image, do not upscale."); if (!$this->keepRatio) { $this->log("Resizing - stretch to fit selected."); $posX = 0; $posY = 0; $cropX = 0; $cropY = 0; if ($this->newWidth > $this->width && $this->newHeight > $this->height) { $posX = round(($this->newWidth - $this->width) / 2); $posY = round(($this->newHeight - $this->height) / 2); } elseif ($this->newWidth > $this->width) { $posX = round(($this->newWidth - $this->width) / 2); $cropY = round(($this->height - $this->newHeight) / 2); } elseif ($this->newHeight > $this->height) { $posY = round(($this->newHeight - $this->height) / 2); $cropX = round(($this->width - $this->newWidth) / 2); } $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); imagecopy($imageResized, $this->image, $posX, $posY, $cropX, $cropY, $this->newWidth, $this->newHeight); $this->image = $imageResized; $this->width = $this->newWidth; $this->height = $this->newHeight; } } else { $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); $this->imageCopyResampled($imageResized, $this->image, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height); $this->image = $imageResized; $this->width = $this->newWidth; $this->height = $this->newHeight; } } return $this; } public function postResize() { $this->log("### Post-process after resizing"); if ($this->rotateAfter) { $this->log("Rotating image."); $this->rotate($this->rotateAfter, $this->bgColor); } if (isset($this->filters) && is_array($this->filters)) { foreach ($this->filters as $filter) { $this->log("Applying filter {$filter['type']}."); switch ($filter['argc']) { case 0: imagefilter($this->image, $filter['type']); break; case 1: imagefilter($this->image, $filter['type'], $filter['arg1']); break; case 2: imagefilter($this->image, $filter['type'], $filter['arg1'], $filter['arg2']); break; case 3: imagefilter($this->image, $filter['type'], $filter['arg1'], $filter['arg2'], $filter['arg3']); break; case 4: imagefilter($this->image, $filter['type'], $filter['arg1'], $filter['arg2'], $filter['arg3'], $filter['arg4']); break; } } } if ($this->palette) { $this->log("Converting to palette image."); $this->trueColorToPalette(); } if ($this->blur) { $this->log("Blur."); $this->blurImage(); } if ($this->emboss) { $this->log("Emboss."); $this->embossImage(); } if ($this->sharpen) { $this->log("Sharpen."); $this->sharpenImage(); } if ($this->convolve) { $this->imageConvolution(); } return $this; } public function rotate($angle, $bgColor) { $this->log("Rotate image " . $angle . " degrees with filler color."); $color = $this->getBackgroundColor(); $this->image = imagerotate($this->image, $angle, $color); $this->width = imagesx($this->image); $this->height = imagesy($this->image); $this->log("New image dimension width x height: " . $this->width . " x " . $this->height); return $this; } public function rotateExif() { if (!in_array($this->fileType, array(IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM))) { $this->log("Autorotate ignored, EXIF not supported by this filetype."); return $this; } $exif = exif_read_data($this->pathToImage); if (!empty($exif['Orientation'])) { switch ($exif['Orientation']) { case 3: $this->log("Autorotate 180."); $this->rotate(180, $this->bgColor); break; case 6: $this->log("Autorotate -90."); $this->rotate(-90, $this->bgColor); break; case 8: $this->log("Autorotate 90."); $this->rotate(90, $this->bgColor); break; default: $this->log("Autorotate ignored, unknown value as orientation."); } } else { $this->log("Autorotate ignored, no orientation in EXIF."); } return $this; } public function trueColorToPalette() { $img = imagecreatetruecolor($this->width, $this->height); $bga = imagecolorallocatealpha($img, 0, 0, 0, 127); imagecolortransparent($img, $bga); imagefill($img, 0, 0, $bga); imagecopy($img, $this->image, 0, 0, 0, 0, $this->width, $this->height); imagetruecolortopalette($img, false, 255); imagesavealpha($img, true); if (imageistruecolor($this->image)) { $this->log("Matching colors with true color image."); imagecolormatch($this->image, $img); } $this->image = $img; } public function sharpenImage() { $this->imageConvolution('sharpen'); return $this; } public function embossImage() { $this->imageConvolution('emboss'); return $this; } public function blurImage() { $this->imageConvolution('blur'); return $this; } public function createConvolveArguments($expression) { if (isset($this->convolves[$expression])) { $expression = $this->convolves[$expression]; } $part = explode(',', $expression); $this->log("Creating convolution expressen: $expression"); if (count($part) != 11) { throw new Exception( "Missmatch in argument convolve. Expected comma-separated string with 11 float values. Got $expression." ); } array_walk($part, function ($item, $key) { if (!is_numeric($item)) { throw new Exception("Argument to convolve expression should be float but is not."); } }); return array( array( array($part[0], $part[1], $part[2]), array($part[3], $part[4], $part[5]), array($part[6], $part[7], $part[8]), ), $part[9], $part[10], ); } public function addConvolveExpressions($options) { $this->convolves = array_merge($this->convolves, $options); return $this; } public function imageConvolution($options = null) { $options = $options ? $options : $this->convolve; $this->log("Convolution with '$options'"); $options = explode(":", $options); foreach ($options as $option) { list($matrix, $divisor, $offset) = $this->createConvolveArguments($option); imageconvolution($this->image, $matrix, $divisor, $offset); } return $this; } public function setDefaultBackgroundColor($color) { $this->log("Setting default background color to '$color'."); if (!(strlen($color) == 6 || strlen($color) == 8)) { throw new Exception( "Background color needs a hex value of 6 or 8 digits. 000000-FFFFFF or 00000000-FFFFFF7F. Current value was: '$color'." ); } $red = hexdec(substr($color, 0, 2)); $green = hexdec(substr($color, 2, 2)); $blue = hexdec(substr($color, 4, 2)); $alpha = (strlen($color) == 8) ? hexdec(substr($color, 6, 2)) : null; if (($red < 0 || $red > 255) || ($green < 0 || $green > 255) || ($blue < 0 || $blue > 255) || ($alpha < 0 || $alpha > 127) ) { throw new Exception( "Background color out of range. Red, green blue should be 00-FF and alpha should be 00-7F. - Current value was: '$color'." ); } $this->bgColor = strtolower($color); $this->bgColorDefault = array( 'red' => $red, 'green' => $green, 'blue' => $blue, 'alpha' => $alpha ); return $this; } private function getBackgroundColor($img = null) { $img = isset($img) ? $img : $this->image; if ($this->bgColorDefault) { $red = $this->bgColorDefault['red']; $green = $this->bgColorDefault['green']; $blue = $this->bgColorDefault['blue']; $alpha = $this->bgColorDefault['alpha']; if ($alpha) { $color = imagecolorallocatealpha($img, $red, $green, $blue, $alpha); } else { $color = imagecolorallocate($img, $red, $green, $blue); } return $color; } else { return 0; } } private function createImageKeepTransparency($width, $height) { $this->log("Creating a new working image width={$width}px, height={$height}px."); $img = imagecreatetruecolor($width, $height); imagealphablending($img, false); imagesavealpha($img, true); $index = $this->image ? imagecolortransparent($this->image) : -1; if ($index != -1) { imagealphablending($img, true); $transparent = imagecolorsforindex($this->image, $index); $color = imagecolorallocatealpha($img, $transparent['red'], $transparent['green'], $transparent['blue'], $transparent['alpha']); imagefill($img, 0, 0, $color); $index = imagecolortransparent($img, $color); $this->Log("Detected transparent color = " . implode(", ", $transparent) . " at index = $index"); } elseif ($this->bgColorDefault) { $color = $this->getBackgroundColor($img); imagefill($img, 0, 0, $color); $this->Log("Filling image with background color."); } return $img; } public function setPostProcessingOptions($options) { if (isset($options['jpeg_optimize']) && $options['jpeg_optimize']) { $this->jpegOptimizeCmd = $options['jpeg_optimize_cmd']; } else { $this->jpegOptimizeCmd = null; } if (isset($options['png_filter']) && $options['png_filter']) { $this->pngFilterCmd = $options['png_filter_cmd']; } else { $this->pngFilterCmd = null; } if (isset($options['png_deflate']) && $options['png_deflate']) { $this->pngDeflateCmd = $options['png_deflate_cmd']; } else { $this->pngDeflateCmd = null; } return $this; } protected function getTargetImageExtension() { if (isset($this->extension)) { return strtolower($this->extension); } else { return substr(image_type_to_extension($this->fileType), 1); } } public function save($src = null, $base = null, $overwrite = true) { if (isset($src)) { $this->setTarget($src, $base); } if ($overwrite === false && is_file($this->cacheFileName)) { $this->Log("Not overwriting file since its already exists and \$overwrite if false."); return; } is_writable($this->saveFolder) or $this->raiseError('Target directory is not writable.'); $type = $this->getTargetImageExtension(); $this->Log("Saving image as " . $type); switch($type) { case 'jpeg': case 'jpg': $this->Log("Saving image as JPEG to cache using quality = {$this->quality}."); imagejpeg($this->image, $this->cacheFileName, $this->quality); if ($this->jpegOptimizeCmd) { if ($this->verbose) { clearstatcache(); $this->log("Filesize before optimize: " . filesize($this->cacheFileName) . " bytes."); } $res = array(); $cmd = $this->jpegOptimizeCmd . " -outfile $this->cacheFileName $this->cacheFileName"; exec($cmd, $res); $this->log($cmd); $this->log($res); } break; case 'gif': $this->Log("Saving image as GIF to cache."); imagegif($this->image, $this->cacheFileName); break; case 'png': default: $this->Log("Saving image as PNG to cache using compression = {$this->compress}."); imagealphablending($this->image, false); imagesavealpha($this->image, true); imagepng($this->image, $this->cacheFileName, $this->compress); if ($this->pngFilterCmd) { if ($this->verbose) { clearstatcache(); $this->Log("Filesize before filter optimize: " . filesize($this->cacheFileName) . " bytes."); } $res = array(); $cmd = $this->pngFilterCmd . " $this->cacheFileName"; exec($cmd, $res); $this->Log($cmd); $this->Log($res); } if ($this->pngDeflateCmd) { if ($this->verbose) { clearstatcache(); $this->Log("Filesize before deflate optimize: " . filesize($this->cacheFileName) . " bytes."); } $res = array(); $cmd = $this->pngDeflateCmd . " $this->cacheFileName"; exec($cmd, $res); $this->Log($cmd); $this->Log($res); } break; } if ($this->verbose) { clearstatcache(); $this->log("Saved image to cache."); $this->log(" Cached image filesize: " . filesize($this->cacheFileName) . " bytes."); $this->log(" imageistruecolor() : " . (imageistruecolor($this->image) ? 'true' : 'false')); $this->log(" imagecolorstotal() : " . imagecolorstotal($this->image)); $this->log(" Number of colors in image = " . $this->ColorsTotal($this->image)); $index = imagecolortransparent($this->image); $this->log(" Detected transparent color = " . ($index > 0 ? implode(", ", imagecolorsforindex($this->image, $index)) : "NONE") . " at index = $index"); } return $this; } public function convert2sRGBColorSpace($src, $dir, $cache, $iccFile, $useCache = true) { if ($this->verbose) { $this->log("# Converting image to sRGB colorspace."); } if (!class_exists("Imagick")) { $this->log(" Ignoring since Imagemagick is not installed."); return false; } $this->setSaveFolder($cache) ->setSource($src, $dir) ->generateFilename(null, false, 'srgb_'); if ($useCache && is_readable($this->cacheFileName)) { $fileTime = filemtime($this->pathToImage); $cacheTime = filemtime($this->cacheFileName); if ($fileTime <= $cacheTime) { $this->log(" Using cached version: " . $this->cacheFileName); return $this->cacheFileName; } } if (is_writable($this->saveFolder)) { $image = new Imagick($this->pathToImage); $colorspace = $image->getImageColorspace(); $this->log(" Current colorspace: " . $colorspace); $profiles = $image->getImageProfiles('*', false); $hasICCProfile = (array_search('icc', $profiles) !== false); $this->log(" Has ICC color profile: " . ($hasICCProfile ? "YES" : "NO")); if ($colorspace != Imagick::COLORSPACE_SRGB || $hasICCProfile) { $this->log(" Converting to sRGB."); $sRGBicc = file_get_contents($iccFile); $image->profileImage('icc', $sRGBicc); $image->transformImageColorspace(Imagick::COLORSPACE_SRGB); $image->writeImage($this->cacheFileName); return $this->cacheFileName; } } return false; } public function linkToCacheFile($alias) { if ($alias === null) { $this->log("Ignore creating alias."); return $this; } if (is_readable($alias)) { unlink($alias); } $res = link($this->cacheFileName, $alias); if ($res) { $this->log("Created an alias as: $alias"); } else { $this->log("Failed to create the alias: $alias"); } return $this; } public function addHTTPHeader($type, $value) { $this->HTTPHeader[$type] = $value; } public function output($file = null, $format = null) { if (is_null($file)) { $file = $this->cacheFileName; } if (is_null($format)) { $format = $this->outputFormat; } $this->log("Output format is: $format"); if (!$this->verbose && $format == 'json') { header('Content-type: application/json'); echo $this->json($file); exit; } elseif ($format == 'ascii') { header('Content-type: text/plain'); echo $this->ascii($file); exit; } $this->log("Outputting image: $file"); clearstatcache(); $lastModified = filemtime($file); $gmdate = gmdate("D, d M Y H:i:s", $lastModified); if (!$this->verbose) { header('Last-Modified: ' . $gmdate . " GMT"); } foreach($this->HTTPHeader as $key => $val) { header("$key: $val"); } if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified) { if ($this->verbose) { $this->log("304 not modified"); $this->verboseOutput(); exit; } header("HTTP/1.0 304 Not Modified"); } else { $info = getimagesize($file); !empty($info) or $this->raiseError("The file doesn't seem to be an image."); $mime = $info['mime']; $size = filesize($file); if ($this->verbose) { $this->log("Last-Modified: " . $gmdate . " GMT"); $this->log("Content-type: " . $mime); $this->log("Content-length: " . $size); $this->verboseOutput(); if (is_null($this->verboseFileName)) { exit; } } header("Content-type: $mime"); header("Content-length: $size"); readfile($file); } exit; } public function json($file = null) { $file = $file ? $file : $this->cacheFileName; $details = array(); clearstatcache(); $details['src'] = $this->imageSrc; $lastModified = filemtime($this->pathToImage); $details['srcGmdate'] = gmdate("D, d M Y H:i:s", $lastModified); $details['cache'] = basename($this->cacheFileName); $lastModified = filemtime($this->cacheFileName); $details['cacheGmdate'] = gmdate("D, d M Y H:i:s", $lastModified); $this->load($file); $details['filename'] = basename($file); $details['mimeType'] = image_type_to_mime_type($this->fileType); $details['width'] = $this->width; $details['height'] = $this->height; $details['aspectRatio'] = round($this->width / $this->height, 3); $details['size'] = filesize($file); $details['colors'] = $this->colorsTotal($this->image); $details['includedFiles'] = count(get_included_files()); $details['memoryPeek'] = round(memory_get_peak_usage()/1024/1024, 3) . " MB" ; $details['memoryCurrent'] = round(memory_get_usage()/1024/1024, 3) . " MB"; $details['memoryLimit'] = ini_get('memory_limit'); if (isset($_SERVER['REQUEST_TIME_FLOAT'])) { $details['loadTime'] = (string) round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 3) . "s"; } if ($details['mimeType'] == 'image/png') { $details['pngType'] = $this->getPngTypeAsString(null, $file); } $options = null; if (defined("JSON_PRETTY_PRINT") && defined("JSON_UNESCAPED_SLASHES")) { $options = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES; } return json_encode($details, $options); } public function setAsciiOptions($options = array()) { $this->asciiOptions = $options; } public function ascii($file = null) { $file = $file ? $file : $this->cacheFileName; $asciiArt = new CAsciiArt(); $asciiArt->setOptions($this->asciiOptions); return $asciiArt->createFromFile($file); } public function log($message) { if ($this->verbose) { $this->log[] = $message; } return $this; } public function setVerboseToFile($fileName) { $this->log("Setting verbose output to file."); $this->verboseFileName = $fileName; } private function verboseOutput() { $log = null; $this->log("As JSON: \n" . $this->json()); $this->log("Memory peak: " . round(memory_get_peak_usage() /1024/1024) . "M"); $this->log("Memory limit: " . ini_get('memory_limit')); $included = get_included_files(); $this->log("Included files: " . count($included)); foreach ($this->log as $val) { if (is_array($val)) { foreach ($val as $val1) { $log .= htmlentities($val1) . '
'; } } else { $log .= htmlentities($val) . '
'; } } if (!is_null($this->verboseFileName)) { file_put_contents( $this->verboseFileName, str_replace("
", "\n", $log) ); } else { echo <<bgColor = strtolower($color); $this->bgColorDefault = array( 'red' => $red, 'green' => $green, 'blue' => $blue, 'alpha' => $alpha ); return $this; } private function getBackgroundColor($img = null) { $img = isset($img) ? $img : $this->image; if ($this->bgColorDefault) { $red = $this->bgColorDefault['red']; $green = $this->bgColorDefault['green']; $blue = $this->bgColorDefault['blue']; $alpha = $this->bgColorDefault['alpha']; if ($alpha) { $color = imagecolorallocatealpha($img, $red, $green, $blue, $alpha); } else { $color = imagecolorallocate($img, $red, $green, $blue); } return $color; } else { return 0; } } private function createImageKeepTransparency($width, $height) { $this->log("Creating a new working image width={$width}px, height={$height}px."); $img = imagecreatetruecolor($width, $height); imagealphablending($img, false); imagesavealpha($img, true); $index = $this->image ? imagecolortransparent($this->image) : -1; if ($index != -1) { imagealphablending($img, true); $transparent = imagecolorsforindex($this->image, $index); $color = imagecolorallocatealpha($img, $transparent['red'], $transparent['green'], $transparent['blue'], $transparent['alpha']); imagefill($img, 0, 0, $color); $index = imagecolortransparent($img, $color); $this->Log("Detected transparent color = " . implode(", ", $transparent) . " at index = $index"); } elseif ($this->bgColorDefault) { $color = $this->getBackgroundColor($img); imagefill($img, 0, 0, $color); $this->Log("Filling image with background color."); } return $img; } public function setPostProcessingOptions($options) { if (isset($options['jpeg_optimize']) && $options['jpeg_optimize']) { $this->jpegOptimizeCmd = $options['jpeg_optimize_cmd']; } else { $this->jpegOptimizeCmd = null; } if (isset($options['png_filter']) && $options['png_filter']) { $this->pngFilterCmd = $options['png_filter_cmd']; } else { $this->pngFilterCmd = null; } if (isset($options['png_deflate']) && $options['png_deflate']) { $this->pngDeflateCmd = $options['png_deflate_cmd']; } else { $this->pngDeflateCmd = null; } return $this; } protected function getTargetImageExtension() { if (isset($this->extension)) { return strtolower($this->extension); } else { return substr(image_type_to_extension($this->fileType), 1); } } public function save($src = null, $base = null, $overwrite = true) { if (isset($src)) { $this->setTarget($src, $base); } if ($overwrite === false && is_file($this->cacheFileName)) { $this->Log("Not overwriting file since its already exists and \$overwrite if false."); return; } is_writable($this->saveFolder) or $this->raiseError('Target directory is not writable.'); $type = $this->getTargetImageExtension(); $this->Log("Saving image as " . $type); switch ($type) { case 'jpeg': case 'jpg': $this->Log("Saving image as JPEG to cache using quality = {$this->quality}."); imagejpeg($this->image, $this->cacheFileName, $this->quality); if ($this->jpegOptimizeCmd) { if ($this->verbose) { clearstatcache(); $this->log("Filesize before optimize: " . filesize($this->cacheFileName) . " bytes."); } $res = array(); $cmd = $this->jpegOptimizeCmd . " -outfile $this->cacheFileName $this->cacheFileName"; exec($cmd, $res); $this->log($cmd); $this->log($res); } break; case 'gif': $this->Log("Saving image as GIF to cache."); imagegif($this->image, $this->cacheFileName); break; case 'png': default: $this->Log("Saving image as PNG to cache using compression = {$this->compress}."); imagealphablending($this->image, false); imagesavealpha($this->image, true); imagepng($this->image, $this->cacheFileName, $this->compress); if ($this->pngFilterCmd) { if ($this->verbose) { clearstatcache(); $this->Log("Filesize before filter optimize: " . filesize($this->cacheFileName) . " bytes."); } $res = array(); $cmd = $this->pngFilterCmd . " $this->cacheFileName"; exec($cmd, $res); $this->Log($cmd); $this->Log($res); } if ($this->pngDeflateCmd) { if ($this->verbose) { clearstatcache(); $this->Log("Filesize before deflate optimize: " . filesize($this->cacheFileName) . " bytes."); } $res = array(); $cmd = $this->pngDeflateCmd . " $this->cacheFileName"; exec($cmd, $res); $this->Log($cmd); $this->Log($res); } break; } if ($this->verbose) { clearstatcache(); $this->log("Saved image to cache."); $this->log(" Cached image filesize: " . filesize($this->cacheFileName) . " bytes."); $this->log(" imageistruecolor() : " . (imageistruecolor($this->image) ? 'true' : 'false')); $this->log(" imagecolorstotal() : " . imagecolorstotal($this->image)); $this->log(" Number of colors in image = " . $this->ColorsTotal($this->image)); $index = imagecolortransparent($this->image); $this->log(" Detected transparent color = " . ($index > 0 ? implode(", ", imagecolorsforindex($this->image, $index)) : "NONE") . " at index = $index"); } return $this; } public function convert2sRGBColorSpace($src, $dir, $cache, $iccFile, $useCache = true) { if ($this->verbose) { $this->log("# Converting image to sRGB colorspace."); } if (!class_exists("Imagick")) { $this->log(" Ignoring since Imagemagick is not installed."); return false; } $this->setSaveFolder($cache) ->setSource($src, $dir) ->generateFilename(null, false, 'srgb_'); if ($useCache && is_readable($this->cacheFileName)) { $fileTime = filemtime($this->pathToImage); $cacheTime = filemtime($this->cacheFileName); if ($fileTime <= $cacheTime) { $this->log(" Using cached version: " . $this->cacheFileName); return $this->cacheFileName; } } if (is_writable($this->saveFolder)) { $image = new Imagick($this->pathToImage); $colorspace = $image->getImageColorspace(); $this->log(" Current colorspace: " . $colorspace); $profiles = $image->getImageProfiles('*', false); $hasICCProfile = (array_search('icc', $profiles) !== false); $this->log(" Has ICC color profile: " . ($hasICCProfile ? "YES" : "NO")); if ($colorspace != Imagick::COLORSPACE_SRGB || $hasICCProfile) { $this->log(" Converting to sRGB."); $sRGBicc = file_get_contents($iccFile); $image->profileImage('icc', $sRGBicc); $image->transformImageColorspace(Imagick::COLORSPACE_SRGB); $image->writeImage($this->cacheFileName); return $this->cacheFileName; } } return false; } public function linkToCacheFile($alias) { if ($alias === null) { $this->log("Ignore creating alias."); return $this; } if (is_readable($alias)) { unlink($alias); } $res = link($this->cacheFileName, $alias); if ($res) { $this->log("Created an alias as: $alias"); } else { $this->log("Failed to create the alias: $alias"); } return $this; } public function addHTTPHeader($type, $value) { $this->HTTPHeader[$type] = $value; } public function output($file = null, $format = null) { if (is_null($file)) { $file = $this->cacheFileName; } if (is_null($format)) { $format = $this->outputFormat; } $this->log("Output format is: $format"); if (!$this->verbose && $format == 'json') { header('Content-type: application/json'); echo $this->json($file); exit; } elseif ($format == 'ascii') { header('Content-type: text/plain'); echo $this->ascii($file); exit; } $this->log("Outputting image: $file"); clearstatcache(); $lastModified = filemtime($file); $gmdate = gmdate("D, d M Y H:i:s", $lastModified); if (!$this->verbose) { header('Last-Modified: ' . $gmdate . " GMT"); } foreach ($this->HTTPHeader as $key => $val) { header("$key: $val"); } if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified) { if ($this->verbose) { $this->log("304 not modified"); $this->verboseOutput(); exit; } header("HTTP/1.0 304 Not Modified"); } else { $info = getimagesize($file); !empty($info) or $this->raiseError("The file doesn't seem to be an image."); $mime = $info['mime']; $size = filesize($file); if ($this->verbose) { $this->log("Last-Modified: " . $gmdate . " GMT"); $this->log("Content-type: " . $mime); $this->log("Content-length: " . $size); $this->verboseOutput(); if (is_null($this->verboseFileName)) { exit; } } header("Content-type: $mime"); header("Content-length: $size"); readfile($file); } exit; } public function json($file = null) { $file = $file ? $file : $this->cacheFileName; $details = array(); clearstatcache(); $details['src'] = $this->imageSrc; $lastModified = filemtime($this->pathToImage); $details['srcGmdate'] = gmdate("D, d M Y H:i:s", $lastModified); $details['cache'] = basename($this->cacheFileName); $lastModified = filemtime($this->cacheFileName); $details['cacheGmdate'] = gmdate("D, d M Y H:i:s", $lastModified); $this->load($file); $details['filename'] = basename($file); $details['mimeType'] = image_type_to_mime_type($this->fileType); $details['width'] = $this->width; $details['height'] = $this->height; $details['aspectRatio'] = round($this->width / $this->height, 3); $details['size'] = filesize($file); $details['colors'] = $this->colorsTotal($this->image); $details['includedFiles'] = count(get_included_files()); $details['memoryPeek'] = round(memory_get_peak_usage()/1024/1024, 3) . " MB" ; $details['memoryCurrent'] = round(memory_get_usage()/1024/1024, 3) . " MB"; $details['memoryLimit'] = ini_get('memory_limit'); if (isset($_SERVER['REQUEST_TIME_FLOAT'])) { $details['loadTime'] = (string) round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 3) . "s"; } if ($details['mimeType'] == 'image/png') { $details['pngType'] = $this->getPngTypeAsString(null, $file); } $options = null; if (defined("JSON_PRETTY_PRINT") && defined("JSON_UNESCAPED_SLASHES")) { $options = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES; } return json_encode($details, $options); } public function setAsciiOptions($options = array()) { $this->asciiOptions = $options; } public function ascii($file = null) { $file = $file ? $file : $this->cacheFileName; $asciiArt = new CAsciiArt(); $asciiArt->setOptions($this->asciiOptions); return $asciiArt->createFromFile($file); } public function log($message) { if ($this->verbose) { $this->log[] = $message; } return $this; } public function setVerboseToFile($fileName) { $this->log("Setting verbose output to file."); $this->verboseFileName = $fileName; } private function verboseOutput() { $log = null; $this->log("As JSON: \n" . $this->json()); $this->log("Memory peak: " . round(memory_get_peak_usage() /1024/1024) . "M"); $this->log("Memory limit: " . ini_get('memory_limit')); $included = get_included_files(); $this->log("Included files: " . count($included)); foreach ($this->log as $val) { if (is_array($val)) { foreach ($val as $val1) { $log .= htmlentities($val1) . '
'; } } else { $log .= htmlentities($val) . '
'; } } if (!is_null($this->verboseFileName)) { file_put_contents( $this->verboseFileName, str_replace("
", "\n", $log) ); } else { echo <<CImage Verbose Output
{$log}
EOD; -} } private function raiseError($message) { throw new Exception($message); } } $version = "v0.7.8 (2015-12-06)"; function errorPage($msg, $type = 500) { global $mode; switch ($type) { case 403: $header = "403 Forbidden"; break; case 404: $header = "404 Not Found"; break; default: $header = "500 Internal Server Error"; } header("HTTP/1.0 $header"); if ($mode == 'development') { die("[img.php] $msg"); } error_log("[img.php] $msg"); die("HTTP/1.0 $header"); } set_exception_handler(function ($exception) { errorPage( "

img.php: Uncaught exception:

" . $exception->getMessage() . "

" . $exception->getTraceAsString() . "
" , 500); }); function get($key, $default = null) { if (is_array($key)) { foreach ($key as $val) { if (isset($_GET[$val])) { return $_GET[$val]; } } } elseif (isset($_GET[$key])) { return $_GET[$key]; } return $default; } function getDefined($key, $defined, $undefined) { return get($key) === null ? $undefined : $defined; } function getConfig($key, $default) { global $config; return isset($config[$key]) ? $config[$key] : $default; } function verbose($msg = null) { global $verbose, $verboseFile; static $log = array(); if (!($verbose || $verboseFile)) { return; } if (is_null($msg)) { return $log; } $log[] = $msg; } $configFile = __DIR__.'/'.basename(__FILE__, '.php').'_config.php'; if (is_file($configFile)) { $config = require $configFile; } elseif (!isset($config)) { $config = array(); } $verbose = getDefined(array('verbose', 'v'), true, false); $verboseFile = getDefined('vf', true, false); verbose("img.php version = $version"); $status = getDefined('status', true, false); $mode = getConfig('mode', 'production'); set_time_limit(20); ini_set('gd.jpeg_ignore_warning', 1); if (!extension_loaded('gd')) { errorPage("Extension gd is not loaded.", 500); } if ($mode == 'strict') { error_reporting(0); ini_set('display_errors', 0); ini_set('log_errors', 1); $verbose = false; $status = false; $verboseFile = false; } elseif ($mode == 'production') { error_reporting(-1); ini_set('display_errors', 0); ini_set('log_errors', 1); $verbose = false; $status = false; $verboseFile = false; } elseif ($mode == 'development') { error_reporting(-1); ini_set('display_errors', 1); ini_set('log_errors', 0); $verboseFile = false; } elseif ($mode == 'test') { error_reporting(-1); ini_set('display_errors', 1); ini_set('log_errors', 0); } else { errorPage("Unknown mode: $mode", 500); } verbose("mode = $mode"); verbose("error log = " . ini_get('error_log')); $defaultTimezone = getConfig('default_timezone', null); if ($defaultTimezone) { date_default_timezone_set($defaultTimezone); } elseif (!ini_get('default_timezone')) { date_default_timezone_set('UTC'); } $pwdConfig = getConfig('password', false); $pwdAlways = getConfig('password_always', false); $pwdType = getConfig('password_type', 'text'); $pwd = get(array('password', 'pwd'), null); $passwordMatch = null; if ($pwd) { switch($pwdType) { case 'md5': $passwordMatch = ($pwdConfig === md5($pwd)); break; case 'hash': $passwordMatch = password_verify($pwd, $pwdConfig); break; case 'text': $passwordMatch = ($pwdConfig === $pwd); break; default: $passwordMatch = false; } } if ($pwdAlways && $passwordMatch !== true) { errorPage("Password required and does not match or exists.", 403); } verbose("password match = $passwordMatch"); $allowHotlinking = getConfig('allow_hotlinking', true); $hotlinkingWhitelist = getConfig('hotlinking_whitelist', array()); $serverName = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : null; $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null; $refererHost = parse_url($referer, PHP_URL_HOST); if (!$allowHotlinking) { if ($passwordMatch) { ; verbose("Hotlinking since passwordmatch"); } elseif ($passwordMatch === false) { errorPage("Hotlinking/leeching not allowed when password missmatch.", 403); } elseif (!$referer) { errorPage("Hotlinking/leeching not allowed and referer is missing.", 403); } elseif (strcmp($serverName, $refererHost) == 0) { ; verbose("Hotlinking disallowed but serverName matches refererHost."); } elseif (!empty($hotlinkingWhitelist)) { $whitelist = new CWhitelist(); $allowedByWhitelist = $whitelist->check($refererHost, $hotlinkingWhitelist); if ($allowedByWhitelist) { verbose("Hotlinking/leeching allowed by whitelist."); } else { errorPage("Hotlinking/leeching not allowed by whitelist. Referer: $referer.", 403); } } else { errorPage("Hotlinking/leeching not allowed.", 403); } } verbose("allow_hotlinking = $allowHotlinking"); verbose("referer = $referer"); verbose("referer host = $refererHost"); $autoloader = getConfig('autoloader', false); $cimageClass = getConfig('cimage_class', false); if ($autoloader) { require $autoloader; } elseif ($cimageClass) { require $cimageClass; } $img = new CImage(); $img->setVerbose($verbose || $verboseFile); $allowRemote = getConfig('remote_allow', false); if ($allowRemote && $passwordMatch !== false) { $pattern = getConfig('remote_pattern', null); $img->setRemoteDownload($allowRemote, $pattern); $whitelist = getConfig('remote_whitelist', null); $img->setRemoteHostWhitelist($whitelist); } $shortcut = get(array('shortcut', 'sc'), null); $shortcutConfig = getConfig('shortcut', array( 'sepia' => "&f=grayscale&f0=brightness,-10&f1=contrast,-20&f2=colorize,120,60,0,0&sharpen", )); verbose("shortcut = $shortcut"); if (isset($shortcut) && isset($shortcutConfig[$shortcut])) { parse_str($shortcutConfig[$shortcut], $get); verbose("shortcut-constant = {$shortcutConfig[$shortcut]}"); $_GET = array_merge($_GET, $get); } $srcImage = urldecode(get('src')) or errorPage('Must set src-attribute.', 404); $imagePath = getConfig('image_path', __DIR__ . '/img/'); $imagePathConstraint = getConfig('image_path_constraint', true); $validFilename = getConfig('valid_filename', '#^[a-z0-9A-Z-/_ \.:]+$#'); $dummyEnabled = getConfig('dummy_enabled', true); $dummyFilename = getConfig('dummy_filename', 'dummy'); $dummyImage = false; preg_match($validFilename, $srcImage) or errorPage('Filename contains invalid characters.', 404); if ($dummyEnabled && $srcImage === $dummyFilename) { $dummyImage = true; } elseif ($allowRemote && $img->isRemoteSource($srcImage)) { } elseif ($imagePathConstraint) { $pathToImage = realpath($imagePath . $srcImage); $imageDir = realpath($imagePath); is_file($pathToImage) or errorPage( 'Source image is not a valid file, check the filename and that a - matching file exists on the filesystem.' , 404); substr_compare($imageDir, $pathToImage, 0, strlen($imageDir)) == 0 or errorPage( 'Security constraint: Source image is not below the directory "image_path" - as specified in the config file img_config.php.' , 404); } verbose("src = $srcImage"); $sizeConstant = getConfig('size_constant', function () { $sizes = array( 'w1' => 613, 'w2' => 630, ); $gridColumnWidth = 30; $gridGutterWidth = 10; $gridColumns = 24; for ($i = 1; $i <= $gridColumns; $i++) { $sizes['c' . $i] = ($gridColumnWidth + $gridGutterWidth) * $i - $gridGutterWidth; } return $sizes; }); $sizes = call_user_func($sizeConstant); $newWidth = get(array('width', 'w')); $maxWidth = getConfig('max_width', 2000); if (isset($sizes[$newWidth])) { $newWidth = $sizes[$newWidth]; } if ($newWidth[strlen($newWidth)-1] == '%') { is_numeric(substr($newWidth, 0, -1)) or errorPage('Width % not numeric.', 404); } else { is_null($newWidth) or ($newWidth > 10 && $newWidth <= $maxWidth) or errorPage('Width out of range.', 404); } verbose("new width = $newWidth"); $newHeight = get(array('height', 'h')); $maxHeight = getConfig('max_height', 2000); if (isset($sizes[$newHeight])) { $newHeight = $sizes[$newHeight]; } if ($newHeight[strlen($newHeight)-1] == '%') { is_numeric(substr($newHeight, 0, -1)) or errorPage('Height % out of range.', 404); } else { is_null($newHeight) or ($newHeight > 10 && $newHeight <= $maxHeight) or errorPage('Height out of range.', 404); } verbose("new height = $newHeight"); $aspectRatio = get(array('aspect-ratio', 'ar')); $aspectRatioConstant = getConfig('aspect_ratio_constant', function () { return array( '3:1' => 3/1, '3:2' => 3/2, '4:3' => 4/3, '8:5' => 8/5, '16:10' => 16/10, '16:9' => 16/9, 'golden' => 1.618, ); }); $aspectRatios = call_user_func($aspectRatioConstant); $negateAspectRatio = ($aspectRatio[0] == '!') ? true : false; $aspectRatio = $negateAspectRatio ? substr($aspectRatio, 1) : $aspectRatio; if (isset($aspectRatios[$aspectRatio])) { $aspectRatio = $aspectRatios[$aspectRatio]; } if ($negateAspectRatio) { $aspectRatio = 1 / $aspectRatio; } is_null($aspectRatio) or is_numeric($aspectRatio) or errorPage('Aspect ratio out of range', 404); verbose("aspect ratio = $aspectRatio"); $cropToFit = getDefined(array('crop-to-fit', 'cf'), true, false); verbose("crop to fit = $cropToFit"); $backgroundColor = getConfig('background_color', null); if ($backgroundColor) { $img->setDefaultBackgroundColor($backgroundColor); verbose("Using default background_color = $backgroundColor"); } $bgColor = get(array('bgColor', 'bg-color', 'bgc'), null); verbose("bgColor = $bgColor"); $resizeStrategy = getDefined(array('no-resample'), true, false); if ($resizeStrategy) { $img->setCopyResizeStrategy($img::RESIZE); verbose("Setting = Resize instead of resample"); } $fillToFit = get(array('fill-to-fit', 'ff'), null); verbose("fill-to-fit = $fillToFit"); if ($fillToFit !== null) { if (!empty($fillToFit)) { $bgColor = $fillToFit; verbose("fillToFit changed bgColor to = $bgColor"); } $fillToFit = true; verbose("fill-to-fit (fixed) = $fillToFit"); } $keepRatio = getDefined(array('no-ratio', 'nr', 'stretch'), false, true); verbose("keep ratio = $keepRatio"); $crop = get(array('crop', 'c')); verbose("crop = $crop"); $area = get(array('area', 'a')); verbose("area = $area"); $useOriginal = getDefined(array('skip-original', 'so'), false, true); $useOriginalDefault = getConfig('skip_original', false); if ($useOriginalDefault === true) { verbose("use original is default ON"); $useOriginal = true; } verbose("use original = $useOriginal"); $useCache = getDefined(array('no-cache', 'nc'), false, true); verbose("use cache = $useCache"); $quality = get(array('quality', 'q')); $qualityDefault = getConfig('jpg_quality', null); is_null($quality) or ($quality > 0 and $quality <= 100) or errorPage('Quality out of range', 404); if (is_null($quality) && !is_null($qualityDefault)) { $quality = $qualityDefault; } verbose("quality = $quality"); $compress = get(array('compress', 'co')); $compressDefault = getConfig('png_compression', null); is_null($compress) or ($compress > 0 and $compress <= 9) or errorPage('Compress out of range', 404); if (is_null($compress) && !is_null($compressDefault)) { $compress = $compressDefault; } verbose("compress = $compress"); $saveAs = get(array('save-as', 'sa')); verbose("save as = $saveAs"); $scale = get(array('scale', 's')); is_null($scale) or ($scale >= 0 and $scale <= 400) or errorPage('Scale out of range', 404); verbose("scale = $scale"); $palette = getDefined(array('palette', 'p'), true, false); verbose("palette = $palette"); $sharpen = getDefined('sharpen', true, null); verbose("sharpen = $sharpen"); $emboss = getDefined('emboss', true, null); verbose("emboss = $emboss"); $blur = getDefined('blur', true, null); verbose("blur = $blur"); $rotateBefore = get(array('rotateBefore', 'rotate-before', 'rb')); is_null($rotateBefore) or ($rotateBefore >= -360 and $rotateBefore <= 360) or errorPage('RotateBefore out of range', 404); verbose("rotateBefore = $rotateBefore"); $rotateAfter = get(array('rotateAfter', 'rotate-after', 'ra', 'rotate', 'r')); is_null($rotateAfter) or ($rotateAfter >= -360 and $rotateAfter <= 360) or errorPage('RotateBefore out of range', 404); verbose("rotateAfter = $rotateAfter"); $autoRotate = getDefined(array('autoRotate', 'auto-rotate', 'aro'), true, false); verbose("autoRotate = $autoRotate"); $filters = array(); $filter = get(array('filter', 'f')); if ($filter) { $filters[] = $filter; } for ($i = 0; $i < 10; $i++) { $filter = get(array("filter{$i}", "f{$i}")); if ($filter) { $filters[] = $filter; } } verbose("filters = " . print_r($filters, 1)); $outputFormat = getDefined('json', 'json', null); $outputFormat = getDefined('ascii', 'ascii', $outputFormat); verbose("outputformat = $outputFormat"); if ($outputFormat == 'ascii') { $defaultOptions = getConfig( 'ascii-options', array( "characterSet" => 'two', "scale" => 14, "luminanceStrategy" => 3, "customCharacterSet" => null, ) ); $options = get('ascii'); $options = explode(',', $options); if (isset($options[0]) && !empty($options[0])) { $defaultOptions['characterSet'] = $options[0]; } if (isset($options[1]) && !empty($options[1])) { $defaultOptions['scale'] = $options[1]; } if (isset($options[2]) && !empty($options[2])) { $defaultOptions['luminanceStrategy'] = $options[2]; } if (count($options) > 3) { unset($options[0]); unset($options[1]); unset($options[2]); $characterString = implode($options); $defaultOptions['customCharacterSet'] = $characterString; } $img->setAsciiOptions($defaultOptions); } $dpr = get(array('ppi', 'dpr', 'device-pixel-ratio'), 1); verbose("dpr = $dpr"); $convolve = get('convolve', null); $convolutionConstant = getConfig('convolution_constant', array()); if ($convolve && isset($convolutionConstant)) { $img->addConvolveExpressions($convolutionConstant); verbose("convolve constant = " . print_r($convolutionConstant, 1)); } verbose("convolve = " . print_r($convolve, 1)); $upscale = getDefined(array('no-upscale', 'nu'), false, true); verbose("upscale = $upscale"); $postProcessing = getConfig('postprocessing', array( 'png_filter' => false, 'png_filter_cmd' => '/usr/local/bin/optipng -q', 'png_deflate' => false, 'png_deflate_cmd' => '/usr/local/bin/pngout -q', 'jpeg_optimize' => false, 'jpeg_optimize_cmd' => '/usr/local/bin/jpegtran -copy none -optimize', )); $alias = get('alias', null); $aliasPath = getConfig('alias_path', null); $validAliasname = getConfig('valid_aliasname', '#^[a-z0-9A-Z-_]+$#'); $aliasTarget = null; if ($alias && $aliasPath && $passwordMatch) { $aliasTarget = $aliasPath . $alias; $useCache = false; is_writable($aliasPath) or errorPage("Directory for alias is not writable.", 403); preg_match($validAliasname, $alias) or errorPage('Filename for alias contains invalid characters. Do not add extension.', 404); } elseif ($alias) { errorPage('Alias is not enabled in the config file or password not matching.', 403); } verbose("alias = $alias"); $cachePath = getConfig('cache_path', __DIR__ . '/../cache/'); $cacheControl = getConfig('cache_control', null); if ($cacheControl) { verbose("cacheControl = $cacheControl"); $img->addHTTPHeader("Cache-Control", $cacheControl); } $dummyDir = getConfig('dummy_dir', $cachePath. "/" . $dummyFilename); if ($dummyImage === true) { is_writable($dummyDir) or verbose("dummy dir not writable = $dummyDir"); $img->setSaveFolder($dummyDir) ->setSource($dummyFilename, $dummyDir) ->setOptions( array( 'newWidth' => $newWidth, 'newHeight' => $newHeight, 'bgColor' => $bgColor, ) ) ->setJpegQuality($quality) ->setPngCompression($compress) ->createDummyImage() ->generateFilename(null, false) ->save(null, null, false); $srcImage = $img->getTarget(); $imagePath = null; verbose("src (updated) = $srcImage"); } $srgbDirName = "/srgb"; $srgbDir = realpath(getConfig('srgb_dir', $cachePath . $srgbDirName)); $srgbDefault = getConfig('srgb_default', false); $srgbColorProfile = getConfig('srgb_colorprofile', __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc'); $srgb = getDefined('srgb', true, null); if ($srgb || $srgbDefault) { if (!is_writable($srgbDir)) { if (is_writable($cachePath)) { mkdir($srgbDir); } } $filename = $img->convert2sRGBColorSpace( $srcImage, $imagePath, $srgbDir, $srgbColorProfile, $useCache ); if ($filename) { $srcImage = $img->getTarget(); $imagePath = null; verbose("srgb conversion and saved to cache = $srcImage"); } else { verbose("srgb not op"); } } if ($status) { $text = "img.php version = $version\n"; $text .= "PHP version = " . PHP_VERSION . "\n"; $text .= "Running on: " . $_SERVER['SERVER_SOFTWARE'] . "\n"; $text .= "Allow remote images = $allowRemote\n"; $text .= "Cache writable = " . is_writable($cachePath) . "\n"; $text .= "Cache dummy writable = " . is_writable($dummyDir) . "\n"; $text .= "Cache srgb writable = " . is_writable($srgbDir) . "\n"; $text .= "Alias path writable = " . is_writable($aliasPath) . "\n"; $no = extension_loaded('exif') ? null : 'NOT'; $text .= "Extension exif is $no loaded.
"; $no = extension_loaded('curl') ? null : 'NOT'; $text .= "Extension curl is $no loaded.
"; $no = extension_loaded('imagick') ? null : 'NOT'; $text .= "Extension imagick is $no loaded.
"; $no = extension_loaded('gd') ? null : 'NOT'; $text .= "Extension gd is $no loaded.
"; if (!$no) { $text .= print_r(gd_info(), 1); } echo <<path = $path; return $this; } public function getPathToSubdir($subdir, $create = true) { $path = realpath($this->path . "/" . $subdir); if (is_dir($path)) { return $path; } if ($create && is_writable($this->path)) { $path = $this->path . "/" . $subdir; if (mkdir($path)) { return realpath($path); } } return false; } public function getStatusOfSubdir($subdir) { $path = realpath($this->path . "/" . $subdir); $exists = is_dir($path); $res = $exists ? "exists" : "does not exist"; if ($exists) { $res .= is_writable($path) ? ", writable" : ", not writable"; } return $res; } public function removeSubdir($subdir) { $path = realpath($this->path . "/" . $subdir); if (is_dir($path)) { return rmdir($path); } return null; } } $version = "v0.7.9 (2015-12-07)"; define("CIMAGE_USER_AGENT", "CImage/$version"); function errorPage($msg, $type = 500) { global $mode; switch ($type) { case 403: $header = "403 Forbidden"; break; case 404: $header = "404 Not Found"; break; default: $header = "500 Internal Server Error"; } header("HTTP/1.0 $header"); if ($mode == "development") { die("[img.php] $msg"); } if ($mode == "strict") { $header = "404 Not Found"; } error_log("[img.php] $msg"); die("HTTP/1.0 $header"); } set_exception_handler(function ($exception) { errorPage( "

img.php: Uncaught exception:

" . $exception->getMessage() . "

" . $exception->getTraceAsString() . "
", 500 ); }); function get($key, $default = null) { if (is_array($key)) { foreach ($key as $val) { if (isset($_GET[$val])) { return $_GET[$val]; } } } elseif (isset($_GET[$key])) { return $_GET[$key]; } return $default; } function getDefined($key, $defined, $undefined) { return get($key) === null ? $undefined : $defined; } function getConfig($key, $default) { global $config; return isset($config[$key]) ? $config[$key] : $default; } function verbose($msg = null) { global $verbose, $verboseFile; static $log = array(); if (!($verbose || $verboseFile)) { return; } if (is_null($msg)) { return $log; } $log[] = $msg; } $configFile = __DIR__.'/'.basename(__FILE__, '.php').'_config.php'; if (is_file($configFile)) { $config = require $configFile; } elseif (!isset($config)) { $config = array(); } $verbose = getDefined(array('verbose', 'v'), true, false); $verboseFile = getDefined('vf', true, false); verbose("img.php version = $version"); $status = getDefined('status', true, false); $mode = getConfig('mode', 'production'); set_time_limit(20); ini_set('gd.jpeg_ignore_warning', 1); if (!extension_loaded('gd')) { errorPage("Extension gd is not loaded.", 500); } if ($mode == 'strict') { error_reporting(0); ini_set('display_errors', 0); ini_set('log_errors', 1); $verbose = false; $status = false; $verboseFile = false; } elseif ($mode == 'production') { error_reporting(-1); ini_set('display_errors', 0); ini_set('log_errors', 1); $verbose = false; $status = false; $verboseFile = false; } elseif ($mode == 'development') { error_reporting(-1); ini_set('display_errors', 1); ini_set('log_errors', 0); $verboseFile = false; } elseif ($mode == 'test') { error_reporting(-1); ini_set('display_errors', 1); ini_set('log_errors', 0); } else { errorPage("Unknown mode: $mode", 500); } verbose("mode = $mode"); verbose("error log = " . ini_get('error_log')); $defaultTimezone = getConfig('default_timezone', null); if ($defaultTimezone) { date_default_timezone_set($defaultTimezone); } elseif (!ini_get('default_timezone')) { date_default_timezone_set('UTC'); } $pwdConfig = getConfig('password', false); $pwdAlways = getConfig('password_always', false); $pwdType = getConfig('password_type', 'text'); $pwd = get(array('password', 'pwd'), null); $passwordMatch = null; if ($pwd) { switch ($pwdType) { case 'md5': $passwordMatch = ($pwdConfig === md5($pwd)); break; case 'hash': $passwordMatch = password_verify($pwd, $pwdConfig); break; case 'text': $passwordMatch = ($pwdConfig === $pwd); break; default: $passwordMatch = false; } } if ($pwdAlways && $passwordMatch !== true) { errorPage("Password required and does not match or exists.", 403); } verbose("password match = $passwordMatch"); $allowHotlinking = getConfig('allow_hotlinking', true); $hotlinkingWhitelist = getConfig('hotlinking_whitelist', array()); $serverName = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : null; $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null; $refererHost = parse_url($referer, PHP_URL_HOST); if (!$allowHotlinking) { if ($passwordMatch) { ; verbose("Hotlinking since passwordmatch"); } elseif ($passwordMatch === false) { errorPage("Hotlinking/leeching not allowed when password missmatch.", 403); } elseif (!$referer) { errorPage("Hotlinking/leeching not allowed and referer is missing.", 403); } elseif (strcmp($serverName, $refererHost) == 0) { ; verbose("Hotlinking disallowed but serverName matches refererHost."); } elseif (!empty($hotlinkingWhitelist)) { $whitelist = new CWhitelist(); $allowedByWhitelist = $whitelist->check($refererHost, $hotlinkingWhitelist); if ($allowedByWhitelist) { verbose("Hotlinking/leeching allowed by whitelist."); } else { errorPage("Hotlinking/leeching not allowed by whitelist. Referer: $referer.", 403); } } else { errorPage("Hotlinking/leeching not allowed.", 403); } } verbose("allow_hotlinking = $allowHotlinking"); verbose("referer = $referer"); verbose("referer host = $refererHost"); $autoloader = getConfig('autoloader', false); $cimageClass = getConfig('cimage_class', false); if ($autoloader) { require $autoloader; } elseif ($cimageClass) { require $cimageClass; } $img = new CImage(); $img->setVerbose($verbose || $verboseFile); $cachePath = getConfig('cache_path', __DIR__ . '/../cache/'); $cache = new CCache(); $cache->setDir($cachePath); $allowRemote = getConfig('remote_allow', false); if ($allowRemote && $passwordMatch !== false) { $cacheRemote = $cache->getPathToSubdir("remote"); $pattern = getConfig('remote_pattern', null); $img->setRemoteDownload($allowRemote, $cacheRemote, $pattern); $whitelist = getConfig('remote_whitelist', null); $img->setRemoteHostWhitelist($whitelist); } $shortcut = get(array('shortcut', 'sc'), null); $shortcutConfig = getConfig('shortcut', array( 'sepia' => "&f=grayscale&f0=brightness,-10&f1=contrast,-20&f2=colorize,120,60,0,0&sharpen", )); verbose("shortcut = $shortcut"); if (isset($shortcut) && isset($shortcutConfig[$shortcut])) { parse_str($shortcutConfig[$shortcut], $get); verbose("shortcut-constant = {$shortcutConfig[$shortcut]}"); $_GET = array_merge($_GET, $get); } $srcImage = urldecode(get('src')) or errorPage('Must set src-attribute.', 404); $imagePath = getConfig('image_path', __DIR__ . '/img/'); $imagePathConstraint = getConfig('image_path_constraint', true); $validFilename = getConfig('valid_filename', '#^[a-z0-9A-Z-/_ \.:]+$#'); $dummyEnabled = getConfig('dummy_enabled', true); $dummyFilename = getConfig('dummy_filename', 'dummy'); $dummyImage = false; preg_match($validFilename, $srcImage) or errorPage('Filename contains invalid characters.', 404); if ($dummyEnabled && $srcImage === $dummyFilename) { $dummyImage = true; } elseif ($allowRemote && $img->isRemoteSource($srcImage)) { } elseif ($imagePathConstraint) { $pathToImage = realpath($imagePath . $srcImage); $imageDir = realpath($imagePath); is_file($pathToImage) or errorPage( 'Source image is not a valid file, check the filename and that a + matching file exists on the filesystem.', 404 ); substr_compare($imageDir, $pathToImage, 0, strlen($imageDir)) == 0 or errorPage( 'Security constraint: Source image is not below the directory "image_path" + as specified in the config file img_config.php.', 404 ); } verbose("src = $srcImage"); $sizeConstant = getConfig('size_constant', function () { $sizes = array( 'w1' => 613, 'w2' => 630, ); $gridColumnWidth = 30; $gridGutterWidth = 10; $gridColumns = 24; for ($i = 1; $i <= $gridColumns; $i++) { $sizes['c' . $i] = ($gridColumnWidth + $gridGutterWidth) * $i - $gridGutterWidth; } return $sizes; }); $sizes = call_user_func($sizeConstant); $newWidth = get(array('width', 'w')); $maxWidth = getConfig('max_width', 2000); if (isset($sizes[$newWidth])) { $newWidth = $sizes[$newWidth]; } if ($newWidth[strlen($newWidth)-1] == '%') { is_numeric(substr($newWidth, 0, -1)) or errorPage('Width % not numeric.', 404); } else { is_null($newWidth) or ($newWidth > 10 && $newWidth <= $maxWidth) or errorPage('Width out of range.', 404); } verbose("new width = $newWidth"); $newHeight = get(array('height', 'h')); $maxHeight = getConfig('max_height', 2000); if (isset($sizes[$newHeight])) { $newHeight = $sizes[$newHeight]; } if ($newHeight[strlen($newHeight)-1] == '%') { is_numeric(substr($newHeight, 0, -1)) or errorPage('Height % out of range.', 404); } else { is_null($newHeight) or ($newHeight > 10 && $newHeight <= $maxHeight) or errorPage('Height out of range.', 404); } verbose("new height = $newHeight"); $aspectRatio = get(array('aspect-ratio', 'ar')); $aspectRatioConstant = getConfig('aspect_ratio_constant', function () { return array( '3:1' => 3/1, '3:2' => 3/2, '4:3' => 4/3, '8:5' => 8/5, '16:10' => 16/10, '16:9' => 16/9, 'golden' => 1.618, ); }); $aspectRatios = call_user_func($aspectRatioConstant); $negateAspectRatio = ($aspectRatio[0] == '!') ? true : false; $aspectRatio = $negateAspectRatio ? substr($aspectRatio, 1) : $aspectRatio; if (isset($aspectRatios[$aspectRatio])) { $aspectRatio = $aspectRatios[$aspectRatio]; } if ($negateAspectRatio) { $aspectRatio = 1 / $aspectRatio; } is_null($aspectRatio) or is_numeric($aspectRatio) or errorPage('Aspect ratio out of range', 404); verbose("aspect ratio = $aspectRatio"); $cropToFit = getDefined(array('crop-to-fit', 'cf'), true, false); verbose("crop to fit = $cropToFit"); $backgroundColor = getConfig('background_color', null); if ($backgroundColor) { $img->setDefaultBackgroundColor($backgroundColor); verbose("Using default background_color = $backgroundColor"); } $bgColor = get(array('bgColor', 'bg-color', 'bgc'), null); verbose("bgColor = $bgColor"); $resizeStrategy = getDefined(array('no-resample'), true, false); if ($resizeStrategy) { $img->setCopyResizeStrategy($img::RESIZE); verbose("Setting = Resize instead of resample"); } $fillToFit = get(array('fill-to-fit', 'ff'), null); verbose("fill-to-fit = $fillToFit"); if ($fillToFit !== null) { if (!empty($fillToFit)) { $bgColor = $fillToFit; verbose("fillToFit changed bgColor to = $bgColor"); } $fillToFit = true; verbose("fill-to-fit (fixed) = $fillToFit"); } $keepRatio = getDefined(array('no-ratio', 'nr', 'stretch'), false, true); verbose("keep ratio = $keepRatio"); $crop = get(array('crop', 'c')); verbose("crop = $crop"); $area = get(array('area', 'a')); verbose("area = $area"); $useOriginal = getDefined(array('skip-original', 'so'), false, true); $useOriginalDefault = getConfig('skip_original', false); if ($useOriginalDefault === true) { verbose("use original is default ON"); $useOriginal = true; } verbose("use original = $useOriginal"); $useCache = getDefined(array('no-cache', 'nc'), false, true); verbose("use cache = $useCache"); $quality = get(array('quality', 'q')); $qualityDefault = getConfig('jpg_quality', null); is_null($quality) or ($quality > 0 and $quality <= 100) or errorPage('Quality out of range', 404); if (is_null($quality) && !is_null($qualityDefault)) { $quality = $qualityDefault; } verbose("quality = $quality"); $compress = get(array('compress', 'co')); $compressDefault = getConfig('png_compression', null); is_null($compress) or ($compress > 0 and $compress <= 9) or errorPage('Compress out of range', 404); if (is_null($compress) && !is_null($compressDefault)) { $compress = $compressDefault; } verbose("compress = $compress"); $saveAs = get(array('save-as', 'sa')); verbose("save as = $saveAs"); $scale = get(array('scale', 's')); is_null($scale) or ($scale >= 0 and $scale <= 400) or errorPage('Scale out of range', 404); verbose("scale = $scale"); $palette = getDefined(array('palette', 'p'), true, false); verbose("palette = $palette"); $sharpen = getDefined('sharpen', true, null); verbose("sharpen = $sharpen"); $emboss = getDefined('emboss', true, null); verbose("emboss = $emboss"); $blur = getDefined('blur', true, null); verbose("blur = $blur"); $rotateBefore = get(array('rotateBefore', 'rotate-before', 'rb')); is_null($rotateBefore) or ($rotateBefore >= -360 and $rotateBefore <= 360) or errorPage('RotateBefore out of range', 404); verbose("rotateBefore = $rotateBefore"); $rotateAfter = get(array('rotateAfter', 'rotate-after', 'ra', 'rotate', 'r')); is_null($rotateAfter) or ($rotateAfter >= -360 and $rotateAfter <= 360) or errorPage('RotateBefore out of range', 404); verbose("rotateAfter = $rotateAfter"); $autoRotate = getDefined(array('autoRotate', 'auto-rotate', 'aro'), true, false); verbose("autoRotate = $autoRotate"); $filters = array(); $filter = get(array('filter', 'f')); if ($filter) { $filters[] = $filter; } for ($i = 0; $i < 10; $i++) { $filter = get(array("filter{$i}", "f{$i}")); if ($filter) { $filters[] = $filter; } } verbose("filters = " . print_r($filters, 1)); $outputFormat = getDefined('json', 'json', null); $outputFormat = getDefined('ascii', 'ascii', $outputFormat); verbose("outputformat = $outputFormat"); if ($outputFormat == 'ascii') { $defaultOptions = getConfig( 'ascii-options', array( "characterSet" => 'two', "scale" => 14, "luminanceStrategy" => 3, "customCharacterSet" => null, ) ); $options = get('ascii'); $options = explode(',', $options); if (isset($options[0]) && !empty($options[0])) { $defaultOptions['characterSet'] = $options[0]; } if (isset($options[1]) && !empty($options[1])) { $defaultOptions['scale'] = $options[1]; } if (isset($options[2]) && !empty($options[2])) { $defaultOptions['luminanceStrategy'] = $options[2]; } if (count($options) > 3) { unset($options[0]); unset($options[1]); unset($options[2]); $characterString = implode($options); $defaultOptions['customCharacterSet'] = $characterString; } $img->setAsciiOptions($defaultOptions); } $dpr = get(array('ppi', 'dpr', 'device-pixel-ratio'), 1); verbose("dpr = $dpr"); $convolve = get('convolve', null); $convolutionConstant = getConfig('convolution_constant', array()); if ($convolve && isset($convolutionConstant)) { $img->addConvolveExpressions($convolutionConstant); verbose("convolve constant = " . print_r($convolutionConstant, 1)); } verbose("convolve = " . print_r($convolve, 1)); $upscale = getDefined(array('no-upscale', 'nu'), false, true); verbose("upscale = $upscale"); $postProcessing = getConfig('postprocessing', array( 'png_filter' => false, 'png_filter_cmd' => '/usr/local/bin/optipng -q', 'png_deflate' => false, 'png_deflate_cmd' => '/usr/local/bin/pngout -q', 'jpeg_optimize' => false, 'jpeg_optimize_cmd' => '/usr/local/bin/jpegtran -copy none -optimize', )); $alias = get('alias', null); $aliasPath = getConfig('alias_path', null); $validAliasname = getConfig('valid_aliasname', '#^[a-z0-9A-Z-_]+$#'); $aliasTarget = null; if ($alias && $aliasPath && $passwordMatch) { $aliasTarget = $aliasPath . $alias; $useCache = false; is_writable($aliasPath) or errorPage("Directory for alias is not writable.", 403); preg_match($validAliasname, $alias) or errorPage('Filename for alias contains invalid characters. Do not add extension.', 404); } elseif ($alias) { errorPage('Alias is not enabled in the config file or password not matching.', 403); } verbose("alias = $alias"); $cacheControl = getConfig('cache_control', null); if ($cacheControl) { verbose("cacheControl = $cacheControl"); $img->addHTTPHeader("Cache-Control", $cacheControl); } if ($dummyImage === true) { $dummyDir = $cache->getPathToSubdir("dummy"); $img->setSaveFolder($dummyDir) ->setSource($dummyFilename, $dummyDir) ->setOptions( array( 'newWidth' => $newWidth, 'newHeight' => $newHeight, 'bgColor' => $bgColor, ) ) ->setJpegQuality($quality) ->setPngCompression($compress) ->createDummyImage() ->generateFilename(null, false) ->save(null, null, false); $srcImage = $img->getTarget(); $imagePath = null; verbose("src (updated) = $srcImage"); } $srgbDefault = getConfig('srgb_default', false); $srgbColorProfile = getConfig('srgb_colorprofile', __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc'); $srgb = getDefined('srgb', true, null); if ($srgb || $srgbDefault) { $filename = $img->convert2sRGBColorSpace( $srcImage, $imagePath, $cache->getPathToSubdir("srgb"), $srgbColorProfile, $useCache ); if ($filename) { $srcImage = $img->getTarget(); $imagePath = null; verbose("srgb conversion and saved to cache = $srcImage"); } else { verbose("srgb not op"); } } if ($status) { $text = "img.php version = $version\n"; $text .= "PHP version = " . PHP_VERSION . "\n"; $text .= "Running on: " . $_SERVER['SERVER_SOFTWARE'] . "\n"; $text .= "Allow remote images = $allowRemote\n"; $res = $cache->getStatusOfSubdir(""); $text .= "Cache $res\n"; $res = $cache->getStatusOfSubdir("remote"); $text .= "Cache remote $res\n"; $res = $cache->getStatusOfSubdir("dummy"); $text .= "Cache dummy $res\n"; $res = $cache->getStatusOfSubdir("srgb"); $text .= "Cache srgb $res\n"; $text .= "Alias path writable = " . is_writable($aliasPath) . "\n"; $no = extension_loaded('exif') ? null : 'NOT'; $text .= "Extension exif is $no loaded.
"; $no = extension_loaded('curl') ? null : 'NOT'; $text .= "Extension curl is $no loaded.
"; $no = extension_loaded('imagick') ? null : 'NOT'; $text .= "Extension imagick is $no loaded.
"; $no = extension_loaded('gd') ? null : 'NOT'; $text .= "Extension gd is $no loaded.
"; if (!$no) { $text .= print_r(gd_info(), 1); } echo << From 05c11ca9fca14782fcbe51f139c9281d7ed3cc4e Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Mon, 7 Dec 2015 16:01:47 +0100 Subject: [PATCH 19/20] refixing new cache management for remote images --- CImage.php | 39 +++++++++++++++++++++++---------------- CRemoteImage.php | 2 +- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/CImage.php b/CImage.php index 88a7c3f..604bc76 100644 --- a/CImage.php +++ b/CImage.php @@ -208,8 +208,8 @@ class CImage /** * Path to command to optimize jpeg images, for example jpegtran or null. */ - private $jpegOptimize; - private $jpegOptimizeCmd; + private $jpegOptimize; + private $jpegOptimizeCmd; @@ -344,20 +344,27 @@ class CImage /** - * Calculate target dimension for image when using fill-to-fit resize strategy. - */ + * Calculate target dimension for image when using fill-to-fit resize strategy. + */ private $fillWidth; private $fillHeight; /** - * Allow remote file download, default is to disallow remote file download. - */ + * Allow remote file download, default is to disallow remote file download. + */ private $allowRemote = false; + /** + * Path to cache for remote download. + */ + private $remoteCache; + + + /** * Pattern to recognize a remote file. */ @@ -400,7 +407,7 @@ class CImage */ const RESIZE = 1; const RESAMPLE = 2; - private $copyStrategy = null; + private $copyStrategy = NULL; @@ -631,7 +638,7 @@ class CImage if ($extension == 'jpeg') { $extension = 'jpg'; - } + } return $extension; } @@ -1619,11 +1626,11 @@ class CImage * * @return $this */ - public function setCopyResizeStrategy($strategy) - { - $this->copyStrategy = $strategy; - return $this; - } + public function setCopyResizeStrategy($strategy) + { + $this->copyStrategy = $strategy; + return $this; + } @@ -1634,7 +1641,7 @@ class CImage */ public function imageCopyResampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { - if ($this->copyStrategy == self::RESIZE) { + if($this->copyStrategy == self::RESIZE) { $this->log("Copy by resize"); imagecopyresized($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } else { @@ -2312,7 +2319,7 @@ class CImage $type = $this->getTargetImageExtension(); $this->Log("Saving image as " . $type); - switch ($type) { + switch($type) { case 'jpeg': case 'jpg': @@ -2549,7 +2556,7 @@ class CImage header('Last-Modified: ' . $gmdate . " GMT"); } - foreach ($this->HTTPHeader as $key => $val) { + foreach($this->HTTPHeader as $key => $val) { header("$key: $val"); } diff --git a/CRemoteImage.php b/CRemoteImage.php index 4eb418e..ec070fb 100644 --- a/CRemoteImage.php +++ b/CRemoteImage.php @@ -101,7 +101,7 @@ class CRemoteImage */ public function setCache($path) { - $this->saveFolder = $path; + $this->saveFolder = rtrim($path, "/") . "/"; return $this; } From f250f7dff9123cfb84c6b60d6ee2820d9e8debe1 Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Mon, 7 Dec 2015 16:02:17 +0100 Subject: [PATCH 20/20] recreating bundles --- webroot/imgd.php | 41 ++++++++++++++++++++++++----------------- webroot/imgp.php | 41 ++++++++++++++++++++++++----------------- webroot/imgs.php | 6 +++--- 3 files changed, 51 insertions(+), 37 deletions(-) diff --git a/webroot/imgd.php b/webroot/imgd.php index 65a42b5..ee1cc1d 100755 --- a/webroot/imgd.php +++ b/webroot/imgd.php @@ -430,7 +430,7 @@ class CRemoteImage */ public function setCache($path) { - $this->saveFolder = $path; + $this->saveFolder = rtrim($path, "/") . "/"; return $this; } @@ -1132,8 +1132,8 @@ class CImage /** * Path to command to optimize jpeg images, for example jpegtran or null. */ - private $jpegOptimize; - private $jpegOptimizeCmd; + private $jpegOptimize; + private $jpegOptimizeCmd; @@ -1268,20 +1268,27 @@ class CImage /** - * Calculate target dimension for image when using fill-to-fit resize strategy. - */ + * Calculate target dimension for image when using fill-to-fit resize strategy. + */ private $fillWidth; private $fillHeight; /** - * Allow remote file download, default is to disallow remote file download. - */ + * Allow remote file download, default is to disallow remote file download. + */ private $allowRemote = false; + /** + * Path to cache for remote download. + */ + private $remoteCache; + + + /** * Pattern to recognize a remote file. */ @@ -1324,7 +1331,7 @@ class CImage */ const RESIZE = 1; const RESAMPLE = 2; - private $copyStrategy = null; + private $copyStrategy = NULL; @@ -1555,7 +1562,7 @@ class CImage if ($extension == 'jpeg') { $extension = 'jpg'; - } + } return $extension; } @@ -2543,11 +2550,11 @@ class CImage * * @return $this */ - public function setCopyResizeStrategy($strategy) - { - $this->copyStrategy = $strategy; - return $this; - } + public function setCopyResizeStrategy($strategy) + { + $this->copyStrategy = $strategy; + return $this; + } @@ -2558,7 +2565,7 @@ class CImage */ public function imageCopyResampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { - if ($this->copyStrategy == self::RESIZE) { + if($this->copyStrategy == self::RESIZE) { $this->log("Copy by resize"); imagecopyresized($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } else { @@ -3236,7 +3243,7 @@ class CImage $type = $this->getTargetImageExtension(); $this->Log("Saving image as " . $type); - switch ($type) { + switch($type) { case 'jpeg': case 'jpg': @@ -3473,7 +3480,7 @@ class CImage header('Last-Modified: ' . $gmdate . " GMT"); } - foreach ($this->HTTPHeader as $key => $val) { + foreach($this->HTTPHeader as $key => $val) { header("$key: $val"); } diff --git a/webroot/imgp.php b/webroot/imgp.php index 27358de..2d60bdf 100755 --- a/webroot/imgp.php +++ b/webroot/imgp.php @@ -430,7 +430,7 @@ class CRemoteImage */ public function setCache($path) { - $this->saveFolder = $path; + $this->saveFolder = rtrim($path, "/") . "/"; return $this; } @@ -1132,8 +1132,8 @@ class CImage /** * Path to command to optimize jpeg images, for example jpegtran or null. */ - private $jpegOptimize; - private $jpegOptimizeCmd; + private $jpegOptimize; + private $jpegOptimizeCmd; @@ -1268,20 +1268,27 @@ class CImage /** - * Calculate target dimension for image when using fill-to-fit resize strategy. - */ + * Calculate target dimension for image when using fill-to-fit resize strategy. + */ private $fillWidth; private $fillHeight; /** - * Allow remote file download, default is to disallow remote file download. - */ + * Allow remote file download, default is to disallow remote file download. + */ private $allowRemote = false; + /** + * Path to cache for remote download. + */ + private $remoteCache; + + + /** * Pattern to recognize a remote file. */ @@ -1324,7 +1331,7 @@ class CImage */ const RESIZE = 1; const RESAMPLE = 2; - private $copyStrategy = null; + private $copyStrategy = NULL; @@ -1555,7 +1562,7 @@ class CImage if ($extension == 'jpeg') { $extension = 'jpg'; - } + } return $extension; } @@ -2543,11 +2550,11 @@ class CImage * * @return $this */ - public function setCopyResizeStrategy($strategy) - { - $this->copyStrategy = $strategy; - return $this; - } + public function setCopyResizeStrategy($strategy) + { + $this->copyStrategy = $strategy; + return $this; + } @@ -2558,7 +2565,7 @@ class CImage */ public function imageCopyResampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { - if ($this->copyStrategy == self::RESIZE) { + if($this->copyStrategy == self::RESIZE) { $this->log("Copy by resize"); imagecopyresized($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } else { @@ -3236,7 +3243,7 @@ class CImage $type = $this->getTargetImageExtension(); $this->Log("Saving image as " . $type); - switch ($type) { + switch($type) { case 'jpeg': case 'jpg': @@ -3473,7 +3480,7 @@ class CImage header('Last-Modified: ' . $gmdate . " GMT"); } - foreach ($this->HTTPHeader as $key => $val) { + foreach($this->HTTPHeader as $key => $val) { header("$key: $val"); } diff --git a/webroot/imgs.php b/webroot/imgs.php index 3d12226..09d915a 100644 --- a/webroot/imgs.php +++ b/webroot/imgs.php @@ -1,11 +1,11 @@ 'strict', ); class CHttpGet { private $request = array(); private $response = array(); public function __construct() { $this->request['header'] = array(); } public function buildUrl($baseUrl, $merge) { $parts = parse_url($baseUrl); $parts = array_merge($parts, $merge); $url = $parts['scheme']; $url .= "://"; $url .= $parts['host']; $url .= isset($parts['port']) ? ":" . $parts['port'] : "" ; $url .= $parts['path']; return $url; } public function setUrl($url) { $parts = parse_url($url); $path = ""; if (isset($parts['path'])) { $pathParts = explode('/', $parts['path']); unset($pathParts[0]); foreach ($pathParts as $value) { $path .= "/" . rawurlencode($value); } } $url = $this->buildUrl($url, array("path" => $path)); $this->request['url'] = $url; return $this; } public function setHeader($field, $value) { $this->request['header'][] = "$field: $value"; return $this; } public function parseHeader() { $rawHeaders = rtrim($this->response['headerRaw'], "\r\n"); $headerGroups = explode("\r\n\r\n", $rawHeaders); $header = explode("\r\n", end($headerGroups)); $output = array(); if ('HTTP' === substr($header[0], 0, 4)) { list($output['version'], $output['status']) = explode(' ', $header[0]); unset($header[0]); } foreach ($header as $entry) { $pos = strpos($entry, ':'); $output[trim(substr($entry, 0, $pos))] = trim(substr($entry, $pos + 1)); } $this->response['header'] = $output; return $this; } public function doGet($debug = false) { $options = array( CURLOPT_URL => $this->request['url'], CURLOPT_HEADER => 1, CURLOPT_HTTPHEADER => $this->request['header'], CURLOPT_AUTOREFERER => true, CURLOPT_RETURNTRANSFER => true, CURLINFO_HEADER_OUT => $debug, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 5, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 2, ); $ch = curl_init(); curl_setopt_array($ch, $options); $response = curl_exec($ch); if (!$response) { throw new Exception("Failed retrieving url, details follows: " . curl_error($ch)); } $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $this->response['headerRaw'] = substr($response, 0, $headerSize); $this->response['body'] = substr($response, $headerSize); $this->parseHeader(); if ($debug) { $info = curl_getinfo($ch); echo "Request header
", var_dump($info['request_header']), "
"; echo "Response header (raw)
", var_dump($this->response['headerRaw']), "
"; echo "Response header (parsed)
", var_dump($this->response['header']), "
"; } curl_close($ch); return true; } public function getStatus() { return isset($this->response['header']['status']) ? (int) $this->response['header']['status'] : null; } public function getLastModified() { return isset($this->response['header']['Last-Modified']) ? strtotime($this->response['header']['Last-Modified']) : null; } public function getContentType() { $type = isset($this->response['header']['Content-Type']) ? $this->response['header']['Content-Type'] : null; return preg_match('#[a-z]+/[a-z]+#', $type) ? $type : null; } public function getDate($default = false) { return isset($this->response['header']['Date']) ? strtotime($this->response['header']['Date']) : $default; } public function getMaxAge($default = false) { $cacheControl = isset($this->response['header']['Cache-Control']) ? $this->response['header']['Cache-Control'] : null; $maxAge = null; if ($cacheControl) { $part = explode('=', $cacheControl); $maxAge = ($part[0] == "max-age") ? (int) $part[1] : null; } if ($maxAge) { return $maxAge; } $expire = isset($this->response['header']['Expires']) ? strtotime($this->response['header']['Expires']) : null; return $expire ? $expire : $default; } public function getBody() { return $this->response['body']; } } class CRemoteImage { private $saveFolder = null; private $useCache = true; private $http; private $status; private $defaultMaxAge = 604800; private $url; private $fileName; private $fileJson; private $cache; public function getStatus() { return $this->status; } public function getDetails() { return $this->cache; } public function setCache($path) { $this->saveFolder = $path; return $this; } public function isCacheWritable() { if (!is_writable($this->saveFolder)) { throw new Exception("Cache folder is not writable for downloaded files."); } return $this; } public function useCache($use = true) { $this->useCache = $use; return $this; } public function setHeaderFields() { $cimageVersion = "CImage"; if (defined("CIMAGE_USER_AGENT")) { $cimageVersion = CIMAGE_USER_AGENT; } $this->http->setHeader("User-Agent", "$cimageVersion (PHP/". phpversion() . " cURL)"); $this->http->setHeader("Accept", "image/jpeg,image/png,image/gif"); if ($this->useCache) { $this->http->setHeader("Cache-Control", "max-age=0"); } else { $this->http->setHeader("Cache-Control", "no-cache"); $this->http->setHeader("Pragma", "no-cache"); } } public function save() { $this->cache = array(); $date = $this->http->getDate(time()); $maxAge = $this->http->getMaxAge($this->defaultMaxAge); $lastModified = $this->http->getLastModified(); $type = $this->http->getContentType(); $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); $this->cache['Max-Age'] = $maxAge; $this->cache['Content-Type'] = $type; $this->cache['Url'] = $this->url; if ($lastModified) { $this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified); } $body = $this->http->getBody(); $img = imagecreatefromstring($body); if ($img !== false) { file_put_contents($this->fileName, $body); file_put_contents($this->fileJson, json_encode($this->cache)); return $this->fileName; } return false; } public function updateCacheDetails() { $date = $this->http->getDate(time()); $maxAge = $this->http->getMaxAge($this->defaultMaxAge); $lastModified = $this->http->getLastModified(); $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); $this->cache['Max-Age'] = $maxAge; if ($lastModified) { $this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified); } file_put_contents($this->fileJson, json_encode($this->cache)); return $this->fileName; } public function download($url) { $this->http = new CHttpGet(); $this->url = $url; $this->loadCacheDetails(); if ($this->useCache) { $src = $this->getCachedSource(); if ($src) { $this->status = 1; return $src; } } $this->setHeaderFields(); $this->http->setUrl($this->url); $this->http->doGet(); $this->status = $this->http->getStatus(); if ($this->status === 200) { $this->isCacheWritable(); return $this->save(); } elseif ($this->status === 304) { $this->isCacheWritable(); return $this->updateCacheDetails(); } throw new Exception("Unknown statuscode when downloading remote image: " . $this->status); } public function loadCacheDetails() { $cacheFile = md5($this->url); $this->fileName = $this->saveFolder . $cacheFile; $this->fileJson = $this->fileName . ".json"; if (is_readable($this->fileJson)) { $this->cache = json_decode(file_get_contents($this->fileJson), true); } } public function getCachedSource() { $imageExists = is_readable($this->fileName); $date = strtotime($this->cache['Date']); $maxAge = $this->cache['Max-Age']; $now = time(); if ($imageExists && $date + $maxAge > $now) { return $this->fileName; } if ($imageExists && isset($this->cache['Last-Modified'])) { $this->http->setHeader("If-Modified-Since", $this->cache['Last-Modified']); } return false; } } class CWhitelist { private $whitelist = array(); public function set($whitelist = array()) { if (!is_array($whitelist)) { throw new Exception("Whitelist is not of a supported format."); } $this->whitelist = $whitelist; return $this; } public function check($item, $whitelist = null) { if ($whitelist !== null) { $this->set($whitelist); } if (empty($item) or empty($this->whitelist)) { return false; } foreach ($this->whitelist as $regexp) { if (preg_match("#$regexp#", $item)) { return true; } } return false; } } class CAsciiArt { private $characterSet = array( 'one' => "#0XT|:,.' ", 'two' => "@%#*+=-:. ", 'three' => "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. " ); private $characters = null; private $charCount = null; private $scale = null; private $luminanceStrategy = null; public function __construct() { $this->setOptions(); } public function addCharacterSet($key, $value) { $this->characterSet[$key] = $value; return $this; } public function setOptions($options = array()) { $default = array( "characterSet" => 'two', "scale" => 14, "luminanceStrategy" => 3, "customCharacterSet" => null, ); $default = array_merge($default, $options); if (!is_null($default['customCharacterSet'])) { $this->addCharacterSet('custom', $default['customCharacterSet']); $default['characterSet'] = 'custom'; } $this->scale = $default['scale']; $this->characters = $this->characterSet[$default['characterSet']]; $this->charCount = strlen($this->characters); $this->luminanceStrategy = $default['luminanceStrategy']; return $this; } public function createFromFile($filename) { $img = imagecreatefromstring(file_get_contents($filename)); list($width, $height) = getimagesize($filename); $ascii = null; $incY = $this->scale; $incX = $this->scale / 2; for ($y = 0; $y < $height - 1; $y += $incY) { for ($x = 0; $x < $width - 1; $x += $incX) { $toX = min($x + $this->scale / 2, $width - 1); $toY = min($y + $this->scale, $height - 1); $luminance = $this->luminanceAreaAverage($img, $x, $y, $toX, $toY); $ascii .= $this->luminance2character($luminance); } $ascii .= PHP_EOL; } return $ascii; } public function luminanceAreaAverage($img, $x1, $y1, $x2, $y2) { $numPixels = ($x2 - $x1 + 1) * ($y2 - $y1 + 1); $luminance = 0; for ($x = $x1; $x <= $x2; $x++) { for ($y = $y1; $y <= $y2; $y++) { $rgb = imagecolorat($img, $x, $y); $red = (($rgb >> 16) & 0xFF); $green = (($rgb >> 8) & 0xFF); $blue = ($rgb & 0xFF); $luminance += $this->getLuminance($red, $green, $blue); } } return $luminance / $numPixels; } public function getLuminance($red, $green, $blue) { switch ($this->luminanceStrategy) { case 1: $luminance = ($red * 0.2126 + $green * 0.7152 + $blue * 0.0722) / 255; break; case 2: $luminance = ($red * 0.299 + $green * 0.587 + $blue * 0.114) / 255; break; case 3: $luminance = sqrt(0.299 * pow($red, 2) + 0.587 * pow($green, 2) + 0.114 * pow($blue, 2)) / 255; break; case 0: default: $luminance = ($red + $green + $blue) / (255 * 3); } return $luminance; } public function luminance2character($luminance) { $position = (int) round($luminance * ($this->charCount - 1)); $char = $this->characters[$position]; return $char; } } class CImage { const PNG_GREYSCALE = 0; const PNG_RGB = 2; const PNG_RGB_PALETTE = 3; const PNG_GREYSCALE_ALPHA = 4; const PNG_RGB_ALPHA = 6; const JPEG_QUALITY_DEFAULT = 60; private $quality; private $useQuality = false; const PNG_COMPRESSION_DEFAULT = -1; private $compress; private $useCompress = false; private $HTTPHeader = array(); private $bgColorDefault = array( 'red' => 0, 'green' => 0, 'blue' => 0, 'alpha' => null, ); private $bgColor; private $saveFolder; private $image; private $imageSrc; private $pathToImage; private $fileType; private $extension; private $outputFormat = null; private $verbose = false; private $log = array(); private $palette; private $cacheFileName; private $saveAs; private $pngFilter; private $pngFilterCmd; private $pngDeflate; private $pngDeflateCmd; private $jpegOptimize; private $jpegOptimizeCmd; private $width; private $height; private $newWidth; private $newWidthOrig; private $newHeight; private $newHeightOrig; private $dpr = 1; const UPSCALE_DEFAULT = true; private $upscale = self::UPSCALE_DEFAULT; public $crop; public $cropOrig; private $convolve; private $convolves = array( 'lighten' => '0,0,0, 0,12,0, 0,0,0, 9, 0', 'darken' => '0,0,0, 0,6,0, 0,0,0, 9, 0', 'sharpen' => '-1,-1,-1, -1,16,-1, -1,-1,-1, 8, 0', 'sharpen-alt' => '0,-1,0, -1,5,-1, 0,-1,0, 1, 0', 'emboss' => '1,1,-1, 1,3,-1, 1,-1,-1, 3, 0', 'emboss-alt' => '-2,-1,0, -1,1,1, 0,1,2, 1, 0', 'blur' => '1,1,1, 1,15,1, 1,1,1, 23, 0', 'gblur' => '1,2,1, 2,4,2, 1,2,1, 16, 0', 'edge' => '-1,-1,-1, -1,8,-1, -1,-1,-1, 9, 0', 'edge-alt' => '0,1,0, 1,-4,1, 0,1,0, 1, 0', 'draw' => '0,-1,0, -1,5,-1, 0,-1,0, 0, 0', 'mean' => '1,1,1, 1,1,1, 1,1,1, 9, 0', 'motion' => '1,0,0, 0,1,0, 0,0,1, 3, 0', ); private $fillToFit; private $scale; private $rotateBefore; private $rotateAfter; private $autoRotate; private $sharpen; private $emboss; private $blur; private $offset; private $fillWidth; private $fillHeight; private $allowRemote = false; private $remotePattern = '#^https?://#'; private $useCache = true; private $remoteHostWhitelist = null; private $verboseFileName = null; private $asciiOptions = array(); const RESIZE = 1; const RESAMPLE = 2; private $copyStrategy = null; public $keepRatio; public $cropToFit; private $cropWidth; private $cropHeight; public $crop_x; public $crop_y; public $filters; private $attr; public function __construct($imageSrc = null, $imageFolder = null, $saveFolder = null, $saveName = null) { $this->setSource($imageSrc, $imageFolder); $this->setTarget($saveFolder, $saveName); } public function setVerbose($mode = true) { $this->verbose = $mode; return $this; } public function setSaveFolder($path) { $this->saveFolder = $path; return $this; } public function useCache($use = true) { $this->useCache = $use; return $this; } public function createDummyImage($width = null, $height = null) { $this->newWidth = $this->newWidth ?: $width ?: 100; $this->newHeight = $this->newHeight ?: $height ?: 100; $this->image = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); return $this; } public function setRemoteDownload($allow, $cache, $pattern = null) { $this->allowRemote = $allow; $this->remoteCache = $cache; $this->remotePattern = is_null($pattern) ? $this->remotePattern : $pattern; $this->log( "Set remote download to: " . ($this->allowRemote ? "true" : "false") . " using pattern " . $this->remotePattern ); return $this; } public function isRemoteSource($src) { $remote = preg_match($this->remotePattern, $src); $this->log("Detected remote image: " . ($remote ? "true" : "false")); return !!$remote; } public function setRemoteHostWhitelist($whitelist = null) { $this->remoteHostWhitelist = $whitelist; $this->log( "Setting remote host whitelist to: " . (is_null($whitelist) ? "null" : print_r($whitelist, 1)) ); return $this; } public function isRemoteSourceOnWhitelist($src) { if (is_null($this->remoteHostWhitelist)) { $this->log("Remote host on whitelist not configured - allowing."); return true; } $whitelist = new CWhitelist(); $hostname = parse_url($src, PHP_URL_HOST); $allow = $whitelist->check($hostname, $this->remoteHostWhitelist); $this->log( "Remote host is on whitelist: " . ($allow ? "true" : "false") ); return $allow; } private function checkFileExtension($extension) { $valid = array('jpg', 'jpeg', 'png', 'gif'); in_array(strtolower($extension), $valid) or $this->raiseError('Not a valid file extension.'); return $this; } private function normalizeFileExtension($extension = null) { $extension = strtolower($extension ? $extension : $this->extension); if ($extension == 'jpeg') { $extension = 'jpg'; } return $extension; } public function downloadRemoteSource($src) { if (!$this->isRemoteSourceOnWhitelist($src)) { throw new Exception("Hostname is not on whitelist for remote sources."); } $remote = new CRemoteImage(); if (!is_writable($this->remoteCache)) { $this->log("The remote cache is not writable."); } $remote->setCache($this->remoteCache); $remote->useCache($this->useCache); $src = $remote->download($src); $this->log("Remote HTTP status: " . $remote->getStatus()); $this->log("Remote item is in local cache: $src"); $this->log("Remote details on cache:" . print_r($remote->getDetails(), true)); return $src; } public function setSource($src, $dir = null) { if (!isset($src)) { $this->imageSrc = null; $this->pathToImage = null; return $this; } if ($this->allowRemote && $this->isRemoteSource($src)) { $src = $this->downloadRemoteSource($src); $dir = null; } if (!isset($dir)) { $dir = dirname($src); $src = basename($src); } $this->imageSrc = ltrim($src, '/'); $imageFolder = rtrim($dir, '/'); $this->pathToImage = $imageFolder . '/' . $this->imageSrc; return $this; } public function setTarget($src = null, $dir = null) { if (!isset($src)) { $this->cacheFileName = null; return $this; } if (isset($dir)) { $this->saveFolder = rtrim($dir, '/'); } $this->cacheFileName = $this->saveFolder . '/' . $src; $this->cacheFileName = preg_replace('/^a-zA-Z0-9\.-_/', '', $this->cacheFileName); $this->log("The cache file name is: " . $this->cacheFileName); return $this; } public function getTarget() { return $this->cacheFileName; } public function setOptions($args) { $this->log("Set new options for processing image."); $defaults = array( 'newWidth' => null, 'newHeight' => null, 'aspectRatio' => null, 'keepRatio' => true, 'cropToFit' => false, 'fillToFit' => null, 'crop' => null, 'area' => null, 'upscale' => self::UPSCALE_DEFAULT, 'useCache' => true, 'useOriginal' => true, 'scale' => null, 'rotateBefore' => null, 'autoRotate' => false, 'bgColor' => null, 'palette' => null, 'filters' => null, 'sharpen' => null, 'emboss' => null, 'blur' => null, 'convolve' => null, 'rotateAfter' => null, 'outputFormat' => null, 'dpr' => 1, ); if (isset($args['crop']) && !is_array($args['crop'])) { $pices = explode(',', $args['crop']); $args['crop'] = array( 'width' => $pices[0], 'height' => $pices[1], 'start_x' => $pices[2], 'start_y' => $pices[3], ); } if (isset($args['area']) && !is_array($args['area'])) { $pices = explode(',', $args['area']); $args['area'] = array( 'top' => $pices[0], 'right' => $pices[1], 'bottom' => $pices[2], 'left' => $pices[3], ); } if (isset($args['filters']) && is_array($args['filters'])) { foreach ($args['filters'] as $key => $filterStr) { $parts = explode(',', $filterStr); $filter = $this->mapFilter($parts[0]); $filter['str'] = $filterStr; for ($i=1; $i<=$filter['argc']; $i++) { if (isset($parts[$i])) { $filter["arg{$i}"] = $parts[$i]; } else { throw new Exception( 'Missing arg to filter, review how many arguments are needed at - http://php.net/manual/en/function.imagefilter.php' ); } } $args['filters'][$key] = $filter; } } $args = array_merge($defaults, $args); foreach ($defaults as $key => $val) { $this->{$key} = $args[$key]; } if ($this->bgColor) { $this->setDefaultBackgroundColor($this->bgColor); } $this->newWidthOrig = $this->newWidth; $this->newHeightOrig = $this->newHeight; $this->cropOrig = $this->crop; return $this; } private function mapFilter($name) { $map = array( 'negate' => array('id'=>0, 'argc'=>0, 'type'=>IMG_FILTER_NEGATE), 'grayscale' => array('id'=>1, 'argc'=>0, 'type'=>IMG_FILTER_GRAYSCALE), 'brightness' => array('id'=>2, 'argc'=>1, 'type'=>IMG_FILTER_BRIGHTNESS), 'contrast' => array('id'=>3, 'argc'=>1, 'type'=>IMG_FILTER_CONTRAST), 'colorize' => array('id'=>4, 'argc'=>4, 'type'=>IMG_FILTER_COLORIZE), 'edgedetect' => array('id'=>5, 'argc'=>0, 'type'=>IMG_FILTER_EDGEDETECT), 'emboss' => array('id'=>6, 'argc'=>0, 'type'=>IMG_FILTER_EMBOSS), 'gaussian_blur' => array('id'=>7, 'argc'=>0, 'type'=>IMG_FILTER_GAUSSIAN_BLUR), 'selective_blur' => array('id'=>8, 'argc'=>0, 'type'=>IMG_FILTER_SELECTIVE_BLUR), 'mean_removal' => array('id'=>9, 'argc'=>0, 'type'=>IMG_FILTER_MEAN_REMOVAL), 'smooth' => array('id'=>10, 'argc'=>1, 'type'=>IMG_FILTER_SMOOTH), 'pixelate' => array('id'=>11, 'argc'=>2, 'type'=>IMG_FILTER_PIXELATE), ); if (isset($map[$name])) { return $map[$name]; } else { throw new Exception('No such filter.'); } } public function loadImageDetails($file = null) { $file = $file ? $file : $this->pathToImage; is_readable($file) or $this->raiseError('Image file does not exist.'); $info = list($this->width, $this->height, $this->fileType, $this->attr) = getimagesize($file); if (empty($info)) { throw new Exception("The file doesn't seem to be a valid image."); } if ($this->verbose) { $this->log("Loading image details for: {$file}"); $this->log(" Image width x height (type): {$this->width} x {$this->height} ({$this->fileType})."); $this->log(" Image filesize: " . filesize($file) . " bytes."); $this->log(" Image mimetype: " . image_type_to_mime_type($this->fileType)); } return $this; } public function initDimensions() { $this->log("Init dimension (before) newWidth x newHeight is {$this->newWidth} x {$this->newHeight}."); if ($this->newWidth[strlen($this->newWidth)-1] == '%') { $this->newWidth = $this->width * substr($this->newWidth, 0, -1) / 100; $this->log("Setting new width based on % to {$this->newWidth}"); } if ($this->newHeight[strlen($this->newHeight)-1] == '%') { $this->newHeight = $this->height * substr($this->newHeight, 0, -1) / 100; $this->log("Setting new height based on % to {$this->newHeight}"); } is_null($this->aspectRatio) or is_numeric($this->aspectRatio) or $this->raiseError('Aspect ratio out of range'); if ($this->aspectRatio && is_null($this->newWidth) && is_null($this->newHeight)) { if ($this->aspectRatio >= 1) { $this->newWidth = $this->width; $this->newHeight = $this->width / $this->aspectRatio; $this->log("Setting new width & height based on width & aspect ratio (>=1) to (w x h) {$this->newWidth} x {$this->newHeight}"); } else { $this->newHeight = $this->height; $this->newWidth = $this->height * $this->aspectRatio; $this->log("Setting new width & height based on width & aspect ratio (<1) to (w x h) {$this->newWidth} x {$this->newHeight}"); } } elseif ($this->aspectRatio && is_null($this->newWidth)) { $this->newWidth = $this->newHeight * $this->aspectRatio; $this->log("Setting new width based on aspect ratio to {$this->newWidth}"); } elseif ($this->aspectRatio && is_null($this->newHeight)) { $this->newHeight = $this->newWidth / $this->aspectRatio; $this->log("Setting new height based on aspect ratio to {$this->newHeight}"); } if ($this->dpr != 1) { if (!is_null($this->newWidth)) { $this->newWidth = round($this->newWidth * $this->dpr); $this->log("Setting new width based on dpr={$this->dpr} - w={$this->newWidth}"); } if (!is_null($this->newHeight)) { $this->newHeight = round($this->newHeight * $this->dpr); $this->log("Setting new height based on dpr={$this->dpr} - h={$this->newHeight}"); } } is_null($this->newWidth) or is_numeric($this->newWidth) or $this->raiseError('Width not numeric'); is_null($this->newHeight) or is_numeric($this->newHeight) or $this->raiseError('Height not numeric'); $this->log("Init dimension (after) newWidth x newHeight is {$this->newWidth} x {$this->newHeight}."); return $this; } public function calculateNewWidthAndHeight() { $this->log("Calculate new width and height."); $this->log("Original width x height is {$this->width} x {$this->height}."); $this->log("Target dimension (before calculating) newWidth x newHeight is {$this->newWidth} x {$this->newHeight}."); if (isset($this->area)) { $this->offset['top'] = round($this->area['top'] / 100 * $this->height); $this->offset['right'] = round($this->area['right'] / 100 * $this->width); $this->offset['bottom'] = round($this->area['bottom'] / 100 * $this->height); $this->offset['left'] = round($this->area['left'] / 100 * $this->width); $this->offset['width'] = $this->width - $this->offset['left'] - $this->offset['right']; $this->offset['height'] = $this->height - $this->offset['top'] - $this->offset['bottom']; $this->width = $this->offset['width']; $this->height = $this->offset['height']; $this->log("The offset for the area to use is top {$this->area['top']}%, right {$this->area['right']}%, bottom {$this->area['bottom']}%, left {$this->area['left']}%."); $this->log("The offset for the area to use is top {$this->offset['top']}px, right {$this->offset['right']}px, bottom {$this->offset['bottom']}px, left {$this->offset['left']}px, width {$this->offset['width']}px, height {$this->offset['height']}px."); } $width = $this->width; $height = $this->height; if ($this->crop) { $width = $this->crop['width'] = $this->crop['width'] <= 0 ? $this->width + $this->crop['width'] : $this->crop['width']; $height = $this->crop['height'] = $this->crop['height'] <= 0 ? $this->height + $this->crop['height'] : $this->crop['height']; if ($this->crop['start_x'] == 'left') { $this->crop['start_x'] = 0; } elseif ($this->crop['start_x'] == 'right') { $this->crop['start_x'] = $this->width - $width; } elseif ($this->crop['start_x'] == 'center') { $this->crop['start_x'] = round($this->width / 2) - round($width / 2); } if ($this->crop['start_y'] == 'top') { $this->crop['start_y'] = 0; } elseif ($this->crop['start_y'] == 'bottom') { $this->crop['start_y'] = $this->height - $height; } elseif ($this->crop['start_y'] == 'center') { $this->crop['start_y'] = round($this->height / 2) - round($height / 2); } $this->log("Crop area is width {$width}px, height {$height}px, start_x {$this->crop['start_x']}px, start_y {$this->crop['start_y']}px."); } if ($this->keepRatio) { $this->log("Keep aspect ratio."); if (($this->cropToFit || $this->fillToFit) && isset($this->newWidth) && isset($this->newHeight)) { $this->log("Use newWidth and newHeigh as width/height, image should fit in box."); } elseif (isset($this->newWidth) && isset($this->newHeight)) { $ratioWidth = $width / $this->newWidth; $ratioHeight = $height / $this->newHeight; $ratio = ($ratioWidth > $ratioHeight) ? $ratioWidth : $ratioHeight; $this->newWidth = round($width / $ratio); $this->newHeight = round($height / $ratio); $this->log("New width and height was set."); } elseif (isset($this->newWidth)) { $factor = (float)$this->newWidth / (float)$width; $this->newHeight = round($factor * $height); $this->log("New width was set."); } elseif (isset($this->newHeight)) { $factor = (float)$this->newHeight / (float)$height; $this->newWidth = round($factor * $width); $this->log("New height was set."); } if ($this->cropToFit || $this->fillToFit) { $ratioWidth = $width / $this->newWidth; $ratioHeight = $height / $this->newHeight; if ($this->cropToFit) { $this->log("Crop to fit."); $ratio = ($ratioWidth < $ratioHeight) ? $ratioWidth : $ratioHeight; $this->cropWidth = round($width / $ratio); $this->cropHeight = round($height / $ratio); $this->log("Crop width, height, ratio: $this->cropWidth x $this->cropHeight ($ratio)."); } elseif ($this->fillToFit) { $this->log("Fill to fit."); $ratio = ($ratioWidth < $ratioHeight) ? $ratioHeight : $ratioWidth; $this->fillWidth = round($width / $ratio); $this->fillHeight = round($height / $ratio); $this->log("Fill width, height, ratio: $this->fillWidth x $this->fillHeight ($ratio)."); } } } if ($this->crop) { $this->log("Crop."); $this->newWidth = round(isset($this->newWidth) ? $this->newWidth : $this->crop['width']); $this->newHeight = round(isset($this->newHeight) ? $this->newHeight : $this->crop['height']); } $this->newWidth = round(isset($this->newWidth) ? $this->newWidth : $this->width); $this->newHeight = round(isset($this->newHeight) ? $this->newHeight : $this->height); $this->log("Calculated new width x height as {$this->newWidth} x {$this->newHeight}."); return $this; } public function reCalculateDimensions() { $this->log("Re-calculate image dimensions, newWidth x newHeigh was: " . $this->newWidth . " x " . $this->newHeight); $this->newWidth = $this->newWidthOrig; $this->newHeight = $this->newHeightOrig; $this->crop = $this->cropOrig; $this->initDimensions() ->calculateNewWidthAndHeight(); return $this; } public function setSaveAsExtension($saveAs = null) { if (isset($saveAs)) { $saveAs = strtolower($saveAs); $this->checkFileExtension($saveAs); $this->saveAs = $saveAs; $this->extension = $saveAs; } $this->log("Prepare to save image as: " . $this->extension); return $this; } public function setJpegQuality($quality = null) { if ($quality) { $this->useQuality = true; } $this->quality = isset($quality) ? $quality : self::JPEG_QUALITY_DEFAULT; (is_numeric($this->quality) and $this->quality > 0 and $this->quality <= 100) or $this->raiseError('Quality not in range.'); $this->log("Setting JPEG quality to {$this->quality}."); return $this; } public function setPngCompression($compress = null) { if ($compress) { $this->useCompress = true; } $this->compress = isset($compress) ? $compress : self::PNG_COMPRESSION_DEFAULT; (is_numeric($this->compress) and $this->compress >= -1 and $this->compress <= 9) or $this->raiseError('Quality not in range.'); $this->log("Setting PNG compression level to {$this->compress}."); return $this; } public function useOriginalIfPossible($useOrig = true) { if ($useOrig && ($this->newWidth == $this->width) && ($this->newHeight == $this->height) && !$this->area && !$this->crop && !$this->cropToFit && !$this->fillToFit && !$this->filters && !$this->sharpen && !$this->emboss && !$this->blur && !$this->convolve && !$this->palette && !$this->useQuality && !$this->useCompress && !$this->saveAs && !$this->rotateBefore && !$this->rotateAfter && !$this->autoRotate && !$this->bgColor && ($this->upscale === self::UPSCALE_DEFAULT) ) { $this->log("Using original image."); $this->output($this->pathToImage); } return $this; } public function generateFilename($base = null, $useSubdir = true, $prefix = null) { $filename = basename($this->pathToImage); $cropToFit = $this->cropToFit ? '_cf' : null; $fillToFit = $this->fillToFit ? '_ff' : null; $crop_x = $this->crop_x ? "_x{$this->crop_x}" : null; $crop_y = $this->crop_y ? "_y{$this->crop_y}" : null; $scale = $this->scale ? "_s{$this->scale}" : null; $bgColor = $this->bgColor ? "_bgc{$this->bgColor}" : null; $quality = $this->quality ? "_q{$this->quality}" : null; $compress = $this->compress ? "_co{$this->compress}" : null; $rotateBefore = $this->rotateBefore ? "_rb{$this->rotateBefore}" : null; $rotateAfter = $this->rotateAfter ? "_ra{$this->rotateAfter}" : null; $saveAs = $this->normalizeFileExtension(); $saveAs = $saveAs ? "_$saveAs" : null; $copyStrat = null; if ($this->copyStrategy === self::RESIZE) { $copyStrat = "_rs"; } $width = $this->newWidth ? '_' . $this->newWidth : null; $height = $this->newHeight ? '_' . $this->newHeight : null; $offset = isset($this->offset) ? '_o' . $this->offset['top'] . '-' . $this->offset['right'] . '-' . $this->offset['bottom'] . '-' . $this->offset['left'] : null; $crop = $this->crop ? '_c' . $this->crop['width'] . '-' . $this->crop['height'] . '-' . $this->crop['start_x'] . '-' . $this->crop['start_y'] : null; $filters = null; if (isset($this->filters)) { foreach ($this->filters as $filter) { if (is_array($filter)) { $filters .= "_f{$filter['id']}"; for ($i=1; $i<=$filter['argc']; $i++) { $filters .= "-".$filter["arg{$i}"]; } } } } $sharpen = $this->sharpen ? 's' : null; $emboss = $this->emboss ? 'e' : null; $blur = $this->blur ? 'b' : null; $palette = $this->palette ? 'p' : null; $autoRotate = $this->autoRotate ? 'ar' : null; $optimize = $this->jpegOptimize ? 'o' : null; $optimize .= $this->pngFilter ? 'f' : null; $optimize .= $this->pngDeflate ? 'd' : null; $convolve = null; if ($this->convolve) { $convolve = '_conv' . preg_replace('/[^a-zA-Z0-9]/', '', $this->convolve); } $upscale = null; if ($this->upscale !== self::UPSCALE_DEFAULT) { $upscale = '_nu'; } $subdir = null; if ($useSubdir === true) { $subdir = str_replace('/', '-', dirname($this->imageSrc)); $subdir = ($subdir == '.') ? '_.' : $subdir; $subdir .= '_'; } $file = $prefix . $subdir . $filename . $width . $height . $offset . $crop . $cropToFit . $fillToFit . $crop_x . $crop_y . $upscale . $quality . $filters . $sharpen . $emboss . $blur . $palette . $optimize . $compress . $scale . $rotateBefore . $rotateAfter . $autoRotate . $bgColor . $convolve . $copyStrat . $saveAs; return $this->setTarget($file, $base); } public function useCacheIfPossible($useCache = true) { if ($useCache && is_readable($this->cacheFileName)) { $fileTime = filemtime($this->pathToImage); $cacheTime = filemtime($this->cacheFileName); if ($fileTime <= $cacheTime) { if ($this->useCache) { if ($this->verbose) { $this->log("Use cached file."); $this->log("Cached image filesize: " . filesize($this->cacheFileName) . " bytes."); } $this->output($this->cacheFileName, $this->outputFormat); } else { $this->log("Cache is valid but ignoring it by intention."); } } else { $this->log("Original file is modified, ignoring cache."); } } else { $this->log("Cachefile does not exists or ignoring it."); } return $this; } public function load($src = null, $dir = null) { if (isset($src)) { $this->setSource($src, $dir); } $this->loadImageDetails($this->pathToImage); $this->image = imagecreatefromstring(file_get_contents($this->pathToImage)); if ($this->image === false) { throw new Exception("Could not load image."); } if ($this->verbose) { $this->log("### Image successfully loaded from file."); $this->log(" imageistruecolor() : " . (imageistruecolor($this->image) ? 'true' : 'false')); $this->log(" imagecolorstotal() : " . imagecolorstotal($this->image)); $this->log(" Number of colors in image = " . $this->colorsTotal($this->image)); $index = imagecolortransparent($this->image); $this->log(" Detected transparent color = " . ($index >= 0 ? implode(", ", imagecolorsforindex($this->image, $index)) : "NONE") . " at index = $index"); } return $this; } public function getPngType($filename = null) { $filename = $filename ? $filename : $this->pathToImage; $pngType = ord(file_get_contents($filename, false, null, 25, 1)); if ($this->verbose) { $this->log("Checking png type of: " . $filename); $this->log($this->getPngTypeAsString($pngType)); } return $pngType; } private function getPngTypeAsString($pngType = null, $filename = null) { if ($filename || !$pngType) { $pngType = $this->getPngType($filename); } $index = imagecolortransparent($this->image); $transparent = null; if ($index != -1) { $transparent = " (transparent)"; } switch ($pngType) { case self::PNG_GREYSCALE: $text = "PNG is type 0, Greyscale$transparent"; break; case self::PNG_RGB: $text = "PNG is type 2, RGB$transparent"; break; case self::PNG_RGB_PALETTE: $text = "PNG is type 3, RGB with palette$transparent"; break; case self::PNG_GREYSCALE_ALPHA: $text = "PNG is type 4, Greyscale with alpha channel"; break; case self::PNG_RGB_ALPHA: $text = "PNG is type 6, RGB with alpha channel (PNG 32-bit)"; break; default: $text = "PNG is UNKNOWN type, is it really a PNG image?"; } return $text; } private function colorsTotal($im) { if (imageistruecolor($im)) { $this->log("Colors as true color."); $h = imagesy($im); $w = imagesx($im); $c = array(); for ($x=0; $x < $w; $x++) { for ($y=0; $y < $h; $y++) { @$c['c'.imagecolorat($im, $x, $y)]++; } } return count($c); } else { $this->log("Colors as palette."); return imagecolorstotal($im); } } public function preResize() { $this->log("### Pre-process before resizing"); if ($this->rotateBefore) { $this->log("Rotating image."); $this->rotate($this->rotateBefore, $this->bgColor) ->reCalculateDimensions(); } if ($this->autoRotate) { $this->log("Auto rotating image."); $this->rotateExif() ->reCalculateDimensions(); } if (isset($this->scale)) { $this->log("Scale by {$this->scale}%"); $newWidth = $this->width * $this->scale / 100; $newHeight = $this->height * $this->scale / 100; $img = $this->CreateImageKeepTransparency($newWidth, $newHeight); imagecopyresampled($img, $this->image, 0, 0, 0, 0, $newWidth, $newHeight, $this->width, $this->height); $this->image = $img; $this->width = $newWidth; $this->height = $newHeight; } return $this; } public function setCopyResizeStrategy($strategy) { $this->copyStrategy = $strategy; return $this; } public function imageCopyResampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { if ($this->copyStrategy == self::RESIZE) { $this->log("Copy by resize"); imagecopyresized($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } else { $this->log("Copy by resample"); imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } } public function resize() { $this->log("### Starting to Resize()"); $this->log("Upscale = '$this->upscale'"); if (isset($this->offset)) { $this->log("Offset for area to use, cropping it width={$this->offset['width']}, height={$this->offset['height']}, start_x={$this->offset['left']}, start_y={$this->offset['top']}"); $img = $this->CreateImageKeepTransparency($this->offset['width'], $this->offset['height']); imagecopy($img, $this->image, 0, 0, $this->offset['left'], $this->offset['top'], $this->offset['width'], $this->offset['height']); $this->image = $img; $this->width = $this->offset['width']; $this->height = $this->offset['height']; } if ($this->crop) { $this->log("Cropping area width={$this->crop['width']}, height={$this->crop['height']}, start_x={$this->crop['start_x']}, start_y={$this->crop['start_y']}"); $img = $this->CreateImageKeepTransparency($this->crop['width'], $this->crop['height']); imagecopy($img, $this->image, 0, 0, $this->crop['start_x'], $this->crop['start_y'], $this->crop['width'], $this->crop['height']); $this->image = $img; $this->width = $this->crop['width']; $this->height = $this->crop['height']; } if (!$this->upscale) { } if ($this->cropToFit) { $this->log("Resizing using strategy - Crop to fit"); if (!$this->upscale && ($this->width < $this->newWidth || $this->height < $this->newHeight)) { $this->log("Resizing - smaller image, do not upscale."); $cropX = round(($this->cropWidth/2) - ($this->newWidth/2)); $cropY = round(($this->cropHeight/2) - ($this->newHeight/2)); $posX = 0; $posY = 0; if ($this->newWidth > $this->width) { $posX = round(($this->newWidth - $this->width) / 2); } if ($this->newHeight > $this->height) { $posY = round(($this->newHeight - $this->height) / 2); } $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); imagecopy($imageResized, $this->image, $posX, $posY, $cropX, $cropY, $this->newWidth, $this->newHeight); } else { $cropX = round(($this->cropWidth/2) - ($this->newWidth/2)); $cropY = round(($this->cropHeight/2) - ($this->newHeight/2)); $imgPreCrop = $this->CreateImageKeepTransparency($this->cropWidth, $this->cropHeight); $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); $this->imageCopyResampled($imgPreCrop, $this->image, 0, 0, 0, 0, $this->cropWidth, $this->cropHeight, $this->width, $this->height); imagecopy($imageResized, $imgPreCrop, 0, 0, $cropX, $cropY, $this->newWidth, $this->newHeight); } $this->image = $imageResized; $this->width = $this->newWidth; $this->height = $this->newHeight; } elseif ($this->fillToFit) { $this->log("Resizing using strategy - Fill to fit"); $posX = 0; $posY = 0; $ratioOrig = $this->width / $this->height; $ratioNew = $this->newWidth / $this->newHeight; if ($ratioOrig < $ratioNew) { $posX = round(($this->newWidth - $this->fillWidth) / 2); } else { $posY = round(($this->newHeight - $this->fillHeight) / 2); } if (!$this->upscale && ($this->width < $this->newWidth || $this->height < $this->newHeight) ) { $this->log("Resizing - smaller image, do not upscale."); $posX = round(($this->fillWidth - $this->width) / 2); $posY = round(($this->fillHeight - $this->height) / 2); $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); imagecopy($imageResized, $this->image, $posX, $posY, 0, 0, $this->fillWidth, $this->fillHeight); } else { $imgPreFill = $this->CreateImageKeepTransparency($this->fillWidth, $this->fillHeight); $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); $this->imageCopyResampled($imgPreFill, $this->image, 0, 0, 0, 0, $this->fillWidth, $this->fillHeight, $this->width, $this->height); imagecopy($imageResized, $imgPreFill, $posX, $posY, 0, 0, $this->fillWidth, $this->fillHeight); } $this->image = $imageResized; $this->width = $this->newWidth; $this->height = $this->newHeight; } elseif (!($this->newWidth == $this->width && $this->newHeight == $this->height)) { $this->log("Resizing, new height and/or width"); if (!$this->upscale && ($this->width < $this->newWidth || $this->height < $this->newHeight) ) { $this->log("Resizing - smaller image, do not upscale."); if (!$this->keepRatio) { $this->log("Resizing - stretch to fit selected."); $posX = 0; $posY = 0; $cropX = 0; $cropY = 0; if ($this->newWidth > $this->width && $this->newHeight > $this->height) { $posX = round(($this->newWidth - $this->width) / 2); $posY = round(($this->newHeight - $this->height) / 2); } elseif ($this->newWidth > $this->width) { $posX = round(($this->newWidth - $this->width) / 2); $cropY = round(($this->height - $this->newHeight) / 2); } elseif ($this->newHeight > $this->height) { $posY = round(($this->newHeight - $this->height) / 2); $cropX = round(($this->width - $this->newWidth) / 2); } $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); imagecopy($imageResized, $this->image, $posX, $posY, $cropX, $cropY, $this->newWidth, $this->newHeight); $this->image = $imageResized; $this->width = $this->newWidth; $this->height = $this->newHeight; } } else { $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); $this->imageCopyResampled($imageResized, $this->image, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height); $this->image = $imageResized; $this->width = $this->newWidth; $this->height = $this->newHeight; } } return $this; } public function postResize() { $this->log("### Post-process after resizing"); if ($this->rotateAfter) { $this->log("Rotating image."); $this->rotate($this->rotateAfter, $this->bgColor); } if (isset($this->filters) && is_array($this->filters)) { foreach ($this->filters as $filter) { $this->log("Applying filter {$filter['type']}."); switch ($filter['argc']) { case 0: imagefilter($this->image, $filter['type']); break; case 1: imagefilter($this->image, $filter['type'], $filter['arg1']); break; case 2: imagefilter($this->image, $filter['type'], $filter['arg1'], $filter['arg2']); break; case 3: imagefilter($this->image, $filter['type'], $filter['arg1'], $filter['arg2'], $filter['arg3']); break; case 4: imagefilter($this->image, $filter['type'], $filter['arg1'], $filter['arg2'], $filter['arg3'], $filter['arg4']); break; } } } if ($this->palette) { $this->log("Converting to palette image."); $this->trueColorToPalette(); } if ($this->blur) { $this->log("Blur."); $this->blurImage(); } if ($this->emboss) { $this->log("Emboss."); $this->embossImage(); } if ($this->sharpen) { $this->log("Sharpen."); $this->sharpenImage(); } if ($this->convolve) { $this->imageConvolution(); } return $this; } public function rotate($angle, $bgColor) { $this->log("Rotate image " . $angle . " degrees with filler color."); $color = $this->getBackgroundColor(); $this->image = imagerotate($this->image, $angle, $color); $this->width = imagesx($this->image); $this->height = imagesy($this->image); $this->log("New image dimension width x height: " . $this->width . " x " . $this->height); return $this; } public function rotateExif() { if (!in_array($this->fileType, array(IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM))) { $this->log("Autorotate ignored, EXIF not supported by this filetype."); return $this; } $exif = exif_read_data($this->pathToImage); if (!empty($exif['Orientation'])) { switch ($exif['Orientation']) { case 3: $this->log("Autorotate 180."); $this->rotate(180, $this->bgColor); break; case 6: $this->log("Autorotate -90."); $this->rotate(-90, $this->bgColor); break; case 8: $this->log("Autorotate 90."); $this->rotate(90, $this->bgColor); break; default: $this->log("Autorotate ignored, unknown value as orientation."); } } else { $this->log("Autorotate ignored, no orientation in EXIF."); } return $this; } public function trueColorToPalette() { $img = imagecreatetruecolor($this->width, $this->height); $bga = imagecolorallocatealpha($img, 0, 0, 0, 127); imagecolortransparent($img, $bga); imagefill($img, 0, 0, $bga); imagecopy($img, $this->image, 0, 0, 0, 0, $this->width, $this->height); imagetruecolortopalette($img, false, 255); imagesavealpha($img, true); if (imageistruecolor($this->image)) { $this->log("Matching colors with true color image."); imagecolormatch($this->image, $img); } $this->image = $img; } public function sharpenImage() { $this->imageConvolution('sharpen'); return $this; } public function embossImage() { $this->imageConvolution('emboss'); return $this; } public function blurImage() { $this->imageConvolution('blur'); return $this; } public function createConvolveArguments($expression) { if (isset($this->convolves[$expression])) { $expression = $this->convolves[$expression]; } $part = explode(',', $expression); $this->log("Creating convolution expressen: $expression"); if (count($part) != 11) { throw new Exception( "Missmatch in argument convolve. Expected comma-separated string with + $config = array( 'mode' => 'strict', ); class CHttpGet { private $request = array(); private $response = array(); public function __construct() { $this->request['header'] = array(); } public function buildUrl($baseUrl, $merge) { $parts = parse_url($baseUrl); $parts = array_merge($parts, $merge); $url = $parts['scheme']; $url .= "://"; $url .= $parts['host']; $url .= isset($parts['port']) ? ":" . $parts['port'] : "" ; $url .= $parts['path']; return $url; } public function setUrl($url) { $parts = parse_url($url); $path = ""; if (isset($parts['path'])) { $pathParts = explode('/', $parts['path']); unset($pathParts[0]); foreach ($pathParts as $value) { $path .= "/" . rawurlencode($value); } } $url = $this->buildUrl($url, array("path" => $path)); $this->request['url'] = $url; return $this; } public function setHeader($field, $value) { $this->request['header'][] = "$field: $value"; return $this; } public function parseHeader() { $rawHeaders = rtrim($this->response['headerRaw'], "\r\n"); $headerGroups = explode("\r\n\r\n", $rawHeaders); $header = explode("\r\n", end($headerGroups)); $output = array(); if ('HTTP' === substr($header[0], 0, 4)) { list($output['version'], $output['status']) = explode(' ', $header[0]); unset($header[0]); } foreach ($header as $entry) { $pos = strpos($entry, ':'); $output[trim(substr($entry, 0, $pos))] = trim(substr($entry, $pos + 1)); } $this->response['header'] = $output; return $this; } public function doGet($debug = false) { $options = array( CURLOPT_URL => $this->request['url'], CURLOPT_HEADER => 1, CURLOPT_HTTPHEADER => $this->request['header'], CURLOPT_AUTOREFERER => true, CURLOPT_RETURNTRANSFER => true, CURLINFO_HEADER_OUT => $debug, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 5, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 2, ); $ch = curl_init(); curl_setopt_array($ch, $options); $response = curl_exec($ch); if (!$response) { throw new Exception("Failed retrieving url, details follows: " . curl_error($ch)); } $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $this->response['headerRaw'] = substr($response, 0, $headerSize); $this->response['body'] = substr($response, $headerSize); $this->parseHeader(); if ($debug) { $info = curl_getinfo($ch); echo "Request header
", var_dump($info['request_header']), "
"; echo "Response header (raw)
", var_dump($this->response['headerRaw']), "
"; echo "Response header (parsed)
", var_dump($this->response['header']), "
"; } curl_close($ch); return true; } public function getStatus() { return isset($this->response['header']['status']) ? (int) $this->response['header']['status'] : null; } public function getLastModified() { return isset($this->response['header']['Last-Modified']) ? strtotime($this->response['header']['Last-Modified']) : null; } public function getContentType() { $type = isset($this->response['header']['Content-Type']) ? $this->response['header']['Content-Type'] : null; return preg_match('#[a-z]+/[a-z]+#', $type) ? $type : null; } public function getDate($default = false) { return isset($this->response['header']['Date']) ? strtotime($this->response['header']['Date']) : $default; } public function getMaxAge($default = false) { $cacheControl = isset($this->response['header']['Cache-Control']) ? $this->response['header']['Cache-Control'] : null; $maxAge = null; if ($cacheControl) { $part = explode('=', $cacheControl); $maxAge = ($part[0] == "max-age") ? (int) $part[1] : null; } if ($maxAge) { return $maxAge; } $expire = isset($this->response['header']['Expires']) ? strtotime($this->response['header']['Expires']) : null; return $expire ? $expire : $default; } public function getBody() { return $this->response['body']; } } class CRemoteImage { private $saveFolder = null; private $useCache = true; private $http; private $status; private $defaultMaxAge = 604800; private $url; private $fileName; private $fileJson; private $cache; public function getStatus() { return $this->status; } public function getDetails() { return $this->cache; } public function setCache($path) { $this->saveFolder = rtrim($path, "/") . "/"; return $this; } public function isCacheWritable() { if (!is_writable($this->saveFolder)) { throw new Exception("Cache folder is not writable for downloaded files."); } return $this; } public function useCache($use = true) { $this->useCache = $use; return $this; } public function setHeaderFields() { $cimageVersion = "CImage"; if (defined("CIMAGE_USER_AGENT")) { $cimageVersion = CIMAGE_USER_AGENT; } $this->http->setHeader("User-Agent", "$cimageVersion (PHP/". phpversion() . " cURL)"); $this->http->setHeader("Accept", "image/jpeg,image/png,image/gif"); if ($this->useCache) { $this->http->setHeader("Cache-Control", "max-age=0"); } else { $this->http->setHeader("Cache-Control", "no-cache"); $this->http->setHeader("Pragma", "no-cache"); } } public function save() { $this->cache = array(); $date = $this->http->getDate(time()); $maxAge = $this->http->getMaxAge($this->defaultMaxAge); $lastModified = $this->http->getLastModified(); $type = $this->http->getContentType(); $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); $this->cache['Max-Age'] = $maxAge; $this->cache['Content-Type'] = $type; $this->cache['Url'] = $this->url; if ($lastModified) { $this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified); } $body = $this->http->getBody(); $img = imagecreatefromstring($body); if ($img !== false) { file_put_contents($this->fileName, $body); file_put_contents($this->fileJson, json_encode($this->cache)); return $this->fileName; } return false; } public function updateCacheDetails() { $date = $this->http->getDate(time()); $maxAge = $this->http->getMaxAge($this->defaultMaxAge); $lastModified = $this->http->getLastModified(); $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); $this->cache['Max-Age'] = $maxAge; if ($lastModified) { $this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified); } file_put_contents($this->fileJson, json_encode($this->cache)); return $this->fileName; } public function download($url) { $this->http = new CHttpGet(); $this->url = $url; $this->loadCacheDetails(); if ($this->useCache) { $src = $this->getCachedSource(); if ($src) { $this->status = 1; return $src; } } $this->setHeaderFields(); $this->http->setUrl($this->url); $this->http->doGet(); $this->status = $this->http->getStatus(); if ($this->status === 200) { $this->isCacheWritable(); return $this->save(); } elseif ($this->status === 304) { $this->isCacheWritable(); return $this->updateCacheDetails(); } throw new Exception("Unknown statuscode when downloading remote image: " . $this->status); } public function loadCacheDetails() { $cacheFile = md5($this->url); $this->fileName = $this->saveFolder . $cacheFile; $this->fileJson = $this->fileName . ".json"; if (is_readable($this->fileJson)) { $this->cache = json_decode(file_get_contents($this->fileJson), true); } } public function getCachedSource() { $imageExists = is_readable($this->fileName); $date = strtotime($this->cache['Date']); $maxAge = $this->cache['Max-Age']; $now = time(); if ($imageExists && $date + $maxAge > $now) { return $this->fileName; } if ($imageExists && isset($this->cache['Last-Modified'])) { $this->http->setHeader("If-Modified-Since", $this->cache['Last-Modified']); } return false; } } class CWhitelist { private $whitelist = array(); public function set($whitelist = array()) { if (!is_array($whitelist)) { throw new Exception("Whitelist is not of a supported format."); } $this->whitelist = $whitelist; return $this; } public function check($item, $whitelist = null) { if ($whitelist !== null) { $this->set($whitelist); } if (empty($item) or empty($this->whitelist)) { return false; } foreach ($this->whitelist as $regexp) { if (preg_match("#$regexp#", $item)) { return true; } } return false; } } class CAsciiArt { private $characterSet = array( 'one' => "#0XT|:,.' ", 'two' => "@%#*+=-:. ", 'three' => "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. " ); private $characters = null; private $charCount = null; private $scale = null; private $luminanceStrategy = null; public function __construct() { $this->setOptions(); } public function addCharacterSet($key, $value) { $this->characterSet[$key] = $value; return $this; } public function setOptions($options = array()) { $default = array( "characterSet" => 'two', "scale" => 14, "luminanceStrategy" => 3, "customCharacterSet" => null, ); $default = array_merge($default, $options); if (!is_null($default['customCharacterSet'])) { $this->addCharacterSet('custom', $default['customCharacterSet']); $default['characterSet'] = 'custom'; } $this->scale = $default['scale']; $this->characters = $this->characterSet[$default['characterSet']]; $this->charCount = strlen($this->characters); $this->luminanceStrategy = $default['luminanceStrategy']; return $this; } public function createFromFile($filename) { $img = imagecreatefromstring(file_get_contents($filename)); list($width, $height) = getimagesize($filename); $ascii = null; $incY = $this->scale; $incX = $this->scale / 2; for ($y = 0; $y < $height - 1; $y += $incY) { for ($x = 0; $x < $width - 1; $x += $incX) { $toX = min($x + $this->scale / 2, $width - 1); $toY = min($y + $this->scale, $height - 1); $luminance = $this->luminanceAreaAverage($img, $x, $y, $toX, $toY); $ascii .= $this->luminance2character($luminance); } $ascii .= PHP_EOL; } return $ascii; } public function luminanceAreaAverage($img, $x1, $y1, $x2, $y2) { $numPixels = ($x2 - $x1 + 1) * ($y2 - $y1 + 1); $luminance = 0; for ($x = $x1; $x <= $x2; $x++) { for ($y = $y1; $y <= $y2; $y++) { $rgb = imagecolorat($img, $x, $y); $red = (($rgb >> 16) & 0xFF); $green = (($rgb >> 8) & 0xFF); $blue = ($rgb & 0xFF); $luminance += $this->getLuminance($red, $green, $blue); } } return $luminance / $numPixels; } public function getLuminance($red, $green, $blue) { switch ($this->luminanceStrategy) { case 1: $luminance = ($red * 0.2126 + $green * 0.7152 + $blue * 0.0722) / 255; break; case 2: $luminance = ($red * 0.299 + $green * 0.587 + $blue * 0.114) / 255; break; case 3: $luminance = sqrt(0.299 * pow($red, 2) + 0.587 * pow($green, 2) + 0.114 * pow($blue, 2)) / 255; break; case 0: default: $luminance = ($red + $green + $blue) / (255 * 3); } return $luminance; } public function luminance2character($luminance) { $position = (int) round($luminance * ($this->charCount - 1)); $char = $this->characters[$position]; return $char; } } class CImage { const PNG_GREYSCALE = 0; const PNG_RGB = 2; const PNG_RGB_PALETTE = 3; const PNG_GREYSCALE_ALPHA = 4; const PNG_RGB_ALPHA = 6; const JPEG_QUALITY_DEFAULT = 60; private $quality; private $useQuality = false; const PNG_COMPRESSION_DEFAULT = -1; private $compress; private $useCompress = false; private $HTTPHeader = array(); private $bgColorDefault = array( 'red' => 0, 'green' => 0, 'blue' => 0, 'alpha' => null, ); private $bgColor; private $saveFolder; private $image; private $imageSrc; private $pathToImage; private $fileType; private $extension; private $outputFormat = null; private $verbose = false; private $log = array(); private $palette; private $cacheFileName; private $saveAs; private $pngFilter; private $pngFilterCmd; private $pngDeflate; private $pngDeflateCmd; private $jpegOptimize; private $jpegOptimizeCmd; private $width; private $height; private $newWidth; private $newWidthOrig; private $newHeight; private $newHeightOrig; private $dpr = 1; const UPSCALE_DEFAULT = true; private $upscale = self::UPSCALE_DEFAULT; public $crop; public $cropOrig; private $convolve; private $convolves = array( 'lighten' => '0,0,0, 0,12,0, 0,0,0, 9, 0', 'darken' => '0,0,0, 0,6,0, 0,0,0, 9, 0', 'sharpen' => '-1,-1,-1, -1,16,-1, -1,-1,-1, 8, 0', 'sharpen-alt' => '0,-1,0, -1,5,-1, 0,-1,0, 1, 0', 'emboss' => '1,1,-1, 1,3,-1, 1,-1,-1, 3, 0', 'emboss-alt' => '-2,-1,0, -1,1,1, 0,1,2, 1, 0', 'blur' => '1,1,1, 1,15,1, 1,1,1, 23, 0', 'gblur' => '1,2,1, 2,4,2, 1,2,1, 16, 0', 'edge' => '-1,-1,-1, -1,8,-1, -1,-1,-1, 9, 0', 'edge-alt' => '0,1,0, 1,-4,1, 0,1,0, 1, 0', 'draw' => '0,-1,0, -1,5,-1, 0,-1,0, 0, 0', 'mean' => '1,1,1, 1,1,1, 1,1,1, 9, 0', 'motion' => '1,0,0, 0,1,0, 0,0,1, 3, 0', ); private $fillToFit; private $scale; private $rotateBefore; private $rotateAfter; private $autoRotate; private $sharpen; private $emboss; private $blur; private $offset; private $fillWidth; private $fillHeight; private $allowRemote = false; private $remoteCache; private $remotePattern = '#^https?://#'; private $useCache = true; private $remoteHostWhitelist = null; private $verboseFileName = null; private $asciiOptions = array(); const RESIZE = 1; const RESAMPLE = 2; private $copyStrategy = NULL; public $keepRatio; public $cropToFit; private $cropWidth; private $cropHeight; public $crop_x; public $crop_y; public $filters; private $attr; public function __construct($imageSrc = null, $imageFolder = null, $saveFolder = null, $saveName = null) { $this->setSource($imageSrc, $imageFolder); $this->setTarget($saveFolder, $saveName); } public function setVerbose($mode = true) { $this->verbose = $mode; return $this; } public function setSaveFolder($path) { $this->saveFolder = $path; return $this; } public function useCache($use = true) { $this->useCache = $use; return $this; } public function createDummyImage($width = null, $height = null) { $this->newWidth = $this->newWidth ?: $width ?: 100; $this->newHeight = $this->newHeight ?: $height ?: 100; $this->image = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); return $this; } public function setRemoteDownload($allow, $cache, $pattern = null) { $this->allowRemote = $allow; $this->remoteCache = $cache; $this->remotePattern = is_null($pattern) ? $this->remotePattern : $pattern; $this->log( "Set remote download to: " . ($this->allowRemote ? "true" : "false") . " using pattern " . $this->remotePattern ); return $this; } public function isRemoteSource($src) { $remote = preg_match($this->remotePattern, $src); $this->log("Detected remote image: " . ($remote ? "true" : "false")); return !!$remote; } public function setRemoteHostWhitelist($whitelist = null) { $this->remoteHostWhitelist = $whitelist; $this->log( "Setting remote host whitelist to: " . (is_null($whitelist) ? "null" : print_r($whitelist, 1)) ); return $this; } public function isRemoteSourceOnWhitelist($src) { if (is_null($this->remoteHostWhitelist)) { $this->log("Remote host on whitelist not configured - allowing."); return true; } $whitelist = new CWhitelist(); $hostname = parse_url($src, PHP_URL_HOST); $allow = $whitelist->check($hostname, $this->remoteHostWhitelist); $this->log( "Remote host is on whitelist: " . ($allow ? "true" : "false") ); return $allow; } private function checkFileExtension($extension) { $valid = array('jpg', 'jpeg', 'png', 'gif'); in_array(strtolower($extension), $valid) or $this->raiseError('Not a valid file extension.'); return $this; } private function normalizeFileExtension($extension = null) { $extension = strtolower($extension ? $extension : $this->extension); if ($extension == 'jpeg') { $extension = 'jpg'; } return $extension; } public function downloadRemoteSource($src) { if (!$this->isRemoteSourceOnWhitelist($src)) { throw new Exception("Hostname is not on whitelist for remote sources."); } $remote = new CRemoteImage(); if (!is_writable($this->remoteCache)) { $this->log("The remote cache is not writable."); } $remote->setCache($this->remoteCache); $remote->useCache($this->useCache); $src = $remote->download($src); $this->log("Remote HTTP status: " . $remote->getStatus()); $this->log("Remote item is in local cache: $src"); $this->log("Remote details on cache:" . print_r($remote->getDetails(), true)); return $src; } public function setSource($src, $dir = null) { if (!isset($src)) { $this->imageSrc = null; $this->pathToImage = null; return $this; } if ($this->allowRemote && $this->isRemoteSource($src)) { $src = $this->downloadRemoteSource($src); $dir = null; } if (!isset($dir)) { $dir = dirname($src); $src = basename($src); } $this->imageSrc = ltrim($src, '/'); $imageFolder = rtrim($dir, '/'); $this->pathToImage = $imageFolder . '/' . $this->imageSrc; return $this; } public function setTarget($src = null, $dir = null) { if (!isset($src)) { $this->cacheFileName = null; return $this; } if (isset($dir)) { $this->saveFolder = rtrim($dir, '/'); } $this->cacheFileName = $this->saveFolder . '/' . $src; $this->cacheFileName = preg_replace('/^a-zA-Z0-9\.-_/', '', $this->cacheFileName); $this->log("The cache file name is: " . $this->cacheFileName); return $this; } public function getTarget() { return $this->cacheFileName; } public function setOptions($args) { $this->log("Set new options for processing image."); $defaults = array( 'newWidth' => null, 'newHeight' => null, 'aspectRatio' => null, 'keepRatio' => true, 'cropToFit' => false, 'fillToFit' => null, 'crop' => null, 'area' => null, 'upscale' => self::UPSCALE_DEFAULT, 'useCache' => true, 'useOriginal' => true, 'scale' => null, 'rotateBefore' => null, 'autoRotate' => false, 'bgColor' => null, 'palette' => null, 'filters' => null, 'sharpen' => null, 'emboss' => null, 'blur' => null, 'convolve' => null, 'rotateAfter' => null, 'outputFormat' => null, 'dpr' => 1, ); if (isset($args['crop']) && !is_array($args['crop'])) { $pices = explode(',', $args['crop']); $args['crop'] = array( 'width' => $pices[0], 'height' => $pices[1], 'start_x' => $pices[2], 'start_y' => $pices[3], ); } if (isset($args['area']) && !is_array($args['area'])) { $pices = explode(',', $args['area']); $args['area'] = array( 'top' => $pices[0], 'right' => $pices[1], 'bottom' => $pices[2], 'left' => $pices[3], ); } if (isset($args['filters']) && is_array($args['filters'])) { foreach ($args['filters'] as $key => $filterStr) { $parts = explode(',', $filterStr); $filter = $this->mapFilter($parts[0]); $filter['str'] = $filterStr; for ($i=1; $i<=$filter['argc']; $i++) { if (isset($parts[$i])) { $filter["arg{$i}"] = $parts[$i]; } else { throw new Exception( 'Missing arg to filter, review how many arguments are needed at + http://php.net/manual/en/function.imagefilter.php' ); } } $args['filters'][$key] = $filter; } } $args = array_merge($defaults, $args); foreach ($defaults as $key => $val) { $this->{$key} = $args[$key]; } if ($this->bgColor) { $this->setDefaultBackgroundColor($this->bgColor); } $this->newWidthOrig = $this->newWidth; $this->newHeightOrig = $this->newHeight; $this->cropOrig = $this->crop; return $this; } private function mapFilter($name) { $map = array( 'negate' => array('id'=>0, 'argc'=>0, 'type'=>IMG_FILTER_NEGATE), 'grayscale' => array('id'=>1, 'argc'=>0, 'type'=>IMG_FILTER_GRAYSCALE), 'brightness' => array('id'=>2, 'argc'=>1, 'type'=>IMG_FILTER_BRIGHTNESS), 'contrast' => array('id'=>3, 'argc'=>1, 'type'=>IMG_FILTER_CONTRAST), 'colorize' => array('id'=>4, 'argc'=>4, 'type'=>IMG_FILTER_COLORIZE), 'edgedetect' => array('id'=>5, 'argc'=>0, 'type'=>IMG_FILTER_EDGEDETECT), 'emboss' => array('id'=>6, 'argc'=>0, 'type'=>IMG_FILTER_EMBOSS), 'gaussian_blur' => array('id'=>7, 'argc'=>0, 'type'=>IMG_FILTER_GAUSSIAN_BLUR), 'selective_blur' => array('id'=>8, 'argc'=>0, 'type'=>IMG_FILTER_SELECTIVE_BLUR), 'mean_removal' => array('id'=>9, 'argc'=>0, 'type'=>IMG_FILTER_MEAN_REMOVAL), 'smooth' => array('id'=>10, 'argc'=>1, 'type'=>IMG_FILTER_SMOOTH), 'pixelate' => array('id'=>11, 'argc'=>2, 'type'=>IMG_FILTER_PIXELATE), ); if (isset($map[$name])) { return $map[$name]; } else { throw new Exception('No such filter.'); } } public function loadImageDetails($file = null) { $file = $file ? $file : $this->pathToImage; is_readable($file) or $this->raiseError('Image file does not exist.'); $info = list($this->width, $this->height, $this->fileType, $this->attr) = getimagesize($file); if (empty($info)) { throw new Exception("The file doesn't seem to be a valid image."); } if ($this->verbose) { $this->log("Loading image details for: {$file}"); $this->log(" Image width x height (type): {$this->width} x {$this->height} ({$this->fileType})."); $this->log(" Image filesize: " . filesize($file) . " bytes."); $this->log(" Image mimetype: " . image_type_to_mime_type($this->fileType)); } return $this; } public function initDimensions() { $this->log("Init dimension (before) newWidth x newHeight is {$this->newWidth} x {$this->newHeight}."); if ($this->newWidth[strlen($this->newWidth)-1] == '%') { $this->newWidth = $this->width * substr($this->newWidth, 0, -1) / 100; $this->log("Setting new width based on % to {$this->newWidth}"); } if ($this->newHeight[strlen($this->newHeight)-1] == '%') { $this->newHeight = $this->height * substr($this->newHeight, 0, -1) / 100; $this->log("Setting new height based on % to {$this->newHeight}"); } is_null($this->aspectRatio) or is_numeric($this->aspectRatio) or $this->raiseError('Aspect ratio out of range'); if ($this->aspectRatio && is_null($this->newWidth) && is_null($this->newHeight)) { if ($this->aspectRatio >= 1) { $this->newWidth = $this->width; $this->newHeight = $this->width / $this->aspectRatio; $this->log("Setting new width & height based on width & aspect ratio (>=1) to (w x h) {$this->newWidth} x {$this->newHeight}"); } else { $this->newHeight = $this->height; $this->newWidth = $this->height * $this->aspectRatio; $this->log("Setting new width & height based on width & aspect ratio (<1) to (w x h) {$this->newWidth} x {$this->newHeight}"); } } elseif ($this->aspectRatio && is_null($this->newWidth)) { $this->newWidth = $this->newHeight * $this->aspectRatio; $this->log("Setting new width based on aspect ratio to {$this->newWidth}"); } elseif ($this->aspectRatio && is_null($this->newHeight)) { $this->newHeight = $this->newWidth / $this->aspectRatio; $this->log("Setting new height based on aspect ratio to {$this->newHeight}"); } if ($this->dpr != 1) { if (!is_null($this->newWidth)) { $this->newWidth = round($this->newWidth * $this->dpr); $this->log("Setting new width based on dpr={$this->dpr} - w={$this->newWidth}"); } if (!is_null($this->newHeight)) { $this->newHeight = round($this->newHeight * $this->dpr); $this->log("Setting new height based on dpr={$this->dpr} - h={$this->newHeight}"); } } is_null($this->newWidth) or is_numeric($this->newWidth) or $this->raiseError('Width not numeric'); is_null($this->newHeight) or is_numeric($this->newHeight) or $this->raiseError('Height not numeric'); $this->log("Init dimension (after) newWidth x newHeight is {$this->newWidth} x {$this->newHeight}."); return $this; } public function calculateNewWidthAndHeight() { $this->log("Calculate new width and height."); $this->log("Original width x height is {$this->width} x {$this->height}."); $this->log("Target dimension (before calculating) newWidth x newHeight is {$this->newWidth} x {$this->newHeight}."); if (isset($this->area)) { $this->offset['top'] = round($this->area['top'] / 100 * $this->height); $this->offset['right'] = round($this->area['right'] / 100 * $this->width); $this->offset['bottom'] = round($this->area['bottom'] / 100 * $this->height); $this->offset['left'] = round($this->area['left'] / 100 * $this->width); $this->offset['width'] = $this->width - $this->offset['left'] - $this->offset['right']; $this->offset['height'] = $this->height - $this->offset['top'] - $this->offset['bottom']; $this->width = $this->offset['width']; $this->height = $this->offset['height']; $this->log("The offset for the area to use is top {$this->area['top']}%, right {$this->area['right']}%, bottom {$this->area['bottom']}%, left {$this->area['left']}%."); $this->log("The offset for the area to use is top {$this->offset['top']}px, right {$this->offset['right']}px, bottom {$this->offset['bottom']}px, left {$this->offset['left']}px, width {$this->offset['width']}px, height {$this->offset['height']}px."); } $width = $this->width; $height = $this->height; if ($this->crop) { $width = $this->crop['width'] = $this->crop['width'] <= 0 ? $this->width + $this->crop['width'] : $this->crop['width']; $height = $this->crop['height'] = $this->crop['height'] <= 0 ? $this->height + $this->crop['height'] : $this->crop['height']; if ($this->crop['start_x'] == 'left') { $this->crop['start_x'] = 0; } elseif ($this->crop['start_x'] == 'right') { $this->crop['start_x'] = $this->width - $width; } elseif ($this->crop['start_x'] == 'center') { $this->crop['start_x'] = round($this->width / 2) - round($width / 2); } if ($this->crop['start_y'] == 'top') { $this->crop['start_y'] = 0; } elseif ($this->crop['start_y'] == 'bottom') { $this->crop['start_y'] = $this->height - $height; } elseif ($this->crop['start_y'] == 'center') { $this->crop['start_y'] = round($this->height / 2) - round($height / 2); } $this->log("Crop area is width {$width}px, height {$height}px, start_x {$this->crop['start_x']}px, start_y {$this->crop['start_y']}px."); } if ($this->keepRatio) { $this->log("Keep aspect ratio."); if (($this->cropToFit || $this->fillToFit) && isset($this->newWidth) && isset($this->newHeight)) { $this->log("Use newWidth and newHeigh as width/height, image should fit in box."); } elseif (isset($this->newWidth) && isset($this->newHeight)) { $ratioWidth = $width / $this->newWidth; $ratioHeight = $height / $this->newHeight; $ratio = ($ratioWidth > $ratioHeight) ? $ratioWidth : $ratioHeight; $this->newWidth = round($width / $ratio); $this->newHeight = round($height / $ratio); $this->log("New width and height was set."); } elseif (isset($this->newWidth)) { $factor = (float)$this->newWidth / (float)$width; $this->newHeight = round($factor * $height); $this->log("New width was set."); } elseif (isset($this->newHeight)) { $factor = (float)$this->newHeight / (float)$height; $this->newWidth = round($factor * $width); $this->log("New height was set."); } if ($this->cropToFit || $this->fillToFit) { $ratioWidth = $width / $this->newWidth; $ratioHeight = $height / $this->newHeight; if ($this->cropToFit) { $this->log("Crop to fit."); $ratio = ($ratioWidth < $ratioHeight) ? $ratioWidth : $ratioHeight; $this->cropWidth = round($width / $ratio); $this->cropHeight = round($height / $ratio); $this->log("Crop width, height, ratio: $this->cropWidth x $this->cropHeight ($ratio)."); } elseif ($this->fillToFit) { $this->log("Fill to fit."); $ratio = ($ratioWidth < $ratioHeight) ? $ratioHeight : $ratioWidth; $this->fillWidth = round($width / $ratio); $this->fillHeight = round($height / $ratio); $this->log("Fill width, height, ratio: $this->fillWidth x $this->fillHeight ($ratio)."); } } } if ($this->crop) { $this->log("Crop."); $this->newWidth = round(isset($this->newWidth) ? $this->newWidth : $this->crop['width']); $this->newHeight = round(isset($this->newHeight) ? $this->newHeight : $this->crop['height']); } $this->newWidth = round(isset($this->newWidth) ? $this->newWidth : $this->width); $this->newHeight = round(isset($this->newHeight) ? $this->newHeight : $this->height); $this->log("Calculated new width x height as {$this->newWidth} x {$this->newHeight}."); return $this; } public function reCalculateDimensions() { $this->log("Re-calculate image dimensions, newWidth x newHeigh was: " . $this->newWidth . " x " . $this->newHeight); $this->newWidth = $this->newWidthOrig; $this->newHeight = $this->newHeightOrig; $this->crop = $this->cropOrig; $this->initDimensions() ->calculateNewWidthAndHeight(); return $this; } public function setSaveAsExtension($saveAs = null) { if (isset($saveAs)) { $saveAs = strtolower($saveAs); $this->checkFileExtension($saveAs); $this->saveAs = $saveAs; $this->extension = $saveAs; } $this->log("Prepare to save image as: " . $this->extension); return $this; } public function setJpegQuality($quality = null) { if ($quality) { $this->useQuality = true; } $this->quality = isset($quality) ? $quality : self::JPEG_QUALITY_DEFAULT; (is_numeric($this->quality) and $this->quality > 0 and $this->quality <= 100) or $this->raiseError('Quality not in range.'); $this->log("Setting JPEG quality to {$this->quality}."); return $this; } public function setPngCompression($compress = null) { if ($compress) { $this->useCompress = true; } $this->compress = isset($compress) ? $compress : self::PNG_COMPRESSION_DEFAULT; (is_numeric($this->compress) and $this->compress >= -1 and $this->compress <= 9) or $this->raiseError('Quality not in range.'); $this->log("Setting PNG compression level to {$this->compress}."); return $this; } public function useOriginalIfPossible($useOrig = true) { if ($useOrig && ($this->newWidth == $this->width) && ($this->newHeight == $this->height) && !$this->area && !$this->crop && !$this->cropToFit && !$this->fillToFit && !$this->filters && !$this->sharpen && !$this->emboss && !$this->blur && !$this->convolve && !$this->palette && !$this->useQuality && !$this->useCompress && !$this->saveAs && !$this->rotateBefore && !$this->rotateAfter && !$this->autoRotate && !$this->bgColor && ($this->upscale === self::UPSCALE_DEFAULT) ) { $this->log("Using original image."); $this->output($this->pathToImage); } return $this; } public function generateFilename($base = null, $useSubdir = true, $prefix = null) { $filename = basename($this->pathToImage); $cropToFit = $this->cropToFit ? '_cf' : null; $fillToFit = $this->fillToFit ? '_ff' : null; $crop_x = $this->crop_x ? "_x{$this->crop_x}" : null; $crop_y = $this->crop_y ? "_y{$this->crop_y}" : null; $scale = $this->scale ? "_s{$this->scale}" : null; $bgColor = $this->bgColor ? "_bgc{$this->bgColor}" : null; $quality = $this->quality ? "_q{$this->quality}" : null; $compress = $this->compress ? "_co{$this->compress}" : null; $rotateBefore = $this->rotateBefore ? "_rb{$this->rotateBefore}" : null; $rotateAfter = $this->rotateAfter ? "_ra{$this->rotateAfter}" : null; $saveAs = $this->normalizeFileExtension(); $saveAs = $saveAs ? "_$saveAs" : null; $copyStrat = null; if ($this->copyStrategy === self::RESIZE) { $copyStrat = "_rs"; } $width = $this->newWidth ? '_' . $this->newWidth : null; $height = $this->newHeight ? '_' . $this->newHeight : null; $offset = isset($this->offset) ? '_o' . $this->offset['top'] . '-' . $this->offset['right'] . '-' . $this->offset['bottom'] . '-' . $this->offset['left'] : null; $crop = $this->crop ? '_c' . $this->crop['width'] . '-' . $this->crop['height'] . '-' . $this->crop['start_x'] . '-' . $this->crop['start_y'] : null; $filters = null; if (isset($this->filters)) { foreach ($this->filters as $filter) { if (is_array($filter)) { $filters .= "_f{$filter['id']}"; for ($i=1; $i<=$filter['argc']; $i++) { $filters .= "-".$filter["arg{$i}"]; } } } } $sharpen = $this->sharpen ? 's' : null; $emboss = $this->emboss ? 'e' : null; $blur = $this->blur ? 'b' : null; $palette = $this->palette ? 'p' : null; $autoRotate = $this->autoRotate ? 'ar' : null; $optimize = $this->jpegOptimize ? 'o' : null; $optimize .= $this->pngFilter ? 'f' : null; $optimize .= $this->pngDeflate ? 'd' : null; $convolve = null; if ($this->convolve) { $convolve = '_conv' . preg_replace('/[^a-zA-Z0-9]/', '', $this->convolve); } $upscale = null; if ($this->upscale !== self::UPSCALE_DEFAULT) { $upscale = '_nu'; } $subdir = null; if ($useSubdir === true) { $subdir = str_replace('/', '-', dirname($this->imageSrc)); $subdir = ($subdir == '.') ? '_.' : $subdir; $subdir .= '_'; } $file = $prefix . $subdir . $filename . $width . $height . $offset . $crop . $cropToFit . $fillToFit . $crop_x . $crop_y . $upscale . $quality . $filters . $sharpen . $emboss . $blur . $palette . $optimize . $compress . $scale . $rotateBefore . $rotateAfter . $autoRotate . $bgColor . $convolve . $copyStrat . $saveAs; return $this->setTarget($file, $base); } public function useCacheIfPossible($useCache = true) { if ($useCache && is_readable($this->cacheFileName)) { $fileTime = filemtime($this->pathToImage); $cacheTime = filemtime($this->cacheFileName); if ($fileTime <= $cacheTime) { if ($this->useCache) { if ($this->verbose) { $this->log("Use cached file."); $this->log("Cached image filesize: " . filesize($this->cacheFileName) . " bytes."); } $this->output($this->cacheFileName, $this->outputFormat); } else { $this->log("Cache is valid but ignoring it by intention."); } } else { $this->log("Original file is modified, ignoring cache."); } } else { $this->log("Cachefile does not exists or ignoring it."); } return $this; } public function load($src = null, $dir = null) { if (isset($src)) { $this->setSource($src, $dir); } $this->loadImageDetails($this->pathToImage); $this->image = imagecreatefromstring(file_get_contents($this->pathToImage)); if ($this->image === false) { throw new Exception("Could not load image."); } if ($this->verbose) { $this->log("### Image successfully loaded from file."); $this->log(" imageistruecolor() : " . (imageistruecolor($this->image) ? 'true' : 'false')); $this->log(" imagecolorstotal() : " . imagecolorstotal($this->image)); $this->log(" Number of colors in image = " . $this->colorsTotal($this->image)); $index = imagecolortransparent($this->image); $this->log(" Detected transparent color = " . ($index >= 0 ? implode(", ", imagecolorsforindex($this->image, $index)) : "NONE") . " at index = $index"); } return $this; } public function getPngType($filename = null) { $filename = $filename ? $filename : $this->pathToImage; $pngType = ord(file_get_contents($filename, false, null, 25, 1)); if ($this->verbose) { $this->log("Checking png type of: " . $filename); $this->log($this->getPngTypeAsString($pngType)); } return $pngType; } private function getPngTypeAsString($pngType = null, $filename = null) { if ($filename || !$pngType) { $pngType = $this->getPngType($filename); } $index = imagecolortransparent($this->image); $transparent = null; if ($index != -1) { $transparent = " (transparent)"; } switch ($pngType) { case self::PNG_GREYSCALE: $text = "PNG is type 0, Greyscale$transparent"; break; case self::PNG_RGB: $text = "PNG is type 2, RGB$transparent"; break; case self::PNG_RGB_PALETTE: $text = "PNG is type 3, RGB with palette$transparent"; break; case self::PNG_GREYSCALE_ALPHA: $text = "PNG is type 4, Greyscale with alpha channel"; break; case self::PNG_RGB_ALPHA: $text = "PNG is type 6, RGB with alpha channel (PNG 32-bit)"; break; default: $text = "PNG is UNKNOWN type, is it really a PNG image?"; } return $text; } private function colorsTotal($im) { if (imageistruecolor($im)) { $this->log("Colors as true color."); $h = imagesy($im); $w = imagesx($im); $c = array(); for ($x=0; $x < $w; $x++) { for ($y=0; $y < $h; $y++) { @$c['c'.imagecolorat($im, $x, $y)]++; } } return count($c); } else { $this->log("Colors as palette."); return imagecolorstotal($im); } } public function preResize() { $this->log("### Pre-process before resizing"); if ($this->rotateBefore) { $this->log("Rotating image."); $this->rotate($this->rotateBefore, $this->bgColor) ->reCalculateDimensions(); } if ($this->autoRotate) { $this->log("Auto rotating image."); $this->rotateExif() ->reCalculateDimensions(); } if (isset($this->scale)) { $this->log("Scale by {$this->scale}%"); $newWidth = $this->width * $this->scale / 100; $newHeight = $this->height * $this->scale / 100; $img = $this->CreateImageKeepTransparency($newWidth, $newHeight); imagecopyresampled($img, $this->image, 0, 0, 0, 0, $newWidth, $newHeight, $this->width, $this->height); $this->image = $img; $this->width = $newWidth; $this->height = $newHeight; } return $this; } public function setCopyResizeStrategy($strategy) { $this->copyStrategy = $strategy; return $this; } public function imageCopyResampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { if($this->copyStrategy == self::RESIZE) { $this->log("Copy by resize"); imagecopyresized($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } else { $this->log("Copy by resample"); imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } } public function resize() { $this->log("### Starting to Resize()"); $this->log("Upscale = '$this->upscale'"); if (isset($this->offset)) { $this->log("Offset for area to use, cropping it width={$this->offset['width']}, height={$this->offset['height']}, start_x={$this->offset['left']}, start_y={$this->offset['top']}"); $img = $this->CreateImageKeepTransparency($this->offset['width'], $this->offset['height']); imagecopy($img, $this->image, 0, 0, $this->offset['left'], $this->offset['top'], $this->offset['width'], $this->offset['height']); $this->image = $img; $this->width = $this->offset['width']; $this->height = $this->offset['height']; } if ($this->crop) { $this->log("Cropping area width={$this->crop['width']}, height={$this->crop['height']}, start_x={$this->crop['start_x']}, start_y={$this->crop['start_y']}"); $img = $this->CreateImageKeepTransparency($this->crop['width'], $this->crop['height']); imagecopy($img, $this->image, 0, 0, $this->crop['start_x'], $this->crop['start_y'], $this->crop['width'], $this->crop['height']); $this->image = $img; $this->width = $this->crop['width']; $this->height = $this->crop['height']; } if (!$this->upscale) { } if ($this->cropToFit) { $this->log("Resizing using strategy - Crop to fit"); if (!$this->upscale && ($this->width < $this->newWidth || $this->height < $this->newHeight)) { $this->log("Resizing - smaller image, do not upscale."); $cropX = round(($this->cropWidth/2) - ($this->newWidth/2)); $cropY = round(($this->cropHeight/2) - ($this->newHeight/2)); $posX = 0; $posY = 0; if ($this->newWidth > $this->width) { $posX = round(($this->newWidth - $this->width) / 2); } if ($this->newHeight > $this->height) { $posY = round(($this->newHeight - $this->height) / 2); } $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); imagecopy($imageResized, $this->image, $posX, $posY, $cropX, $cropY, $this->newWidth, $this->newHeight); } else { $cropX = round(($this->cropWidth/2) - ($this->newWidth/2)); $cropY = round(($this->cropHeight/2) - ($this->newHeight/2)); $imgPreCrop = $this->CreateImageKeepTransparency($this->cropWidth, $this->cropHeight); $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); $this->imageCopyResampled($imgPreCrop, $this->image, 0, 0, 0, 0, $this->cropWidth, $this->cropHeight, $this->width, $this->height); imagecopy($imageResized, $imgPreCrop, 0, 0, $cropX, $cropY, $this->newWidth, $this->newHeight); } $this->image = $imageResized; $this->width = $this->newWidth; $this->height = $this->newHeight; } elseif ($this->fillToFit) { $this->log("Resizing using strategy - Fill to fit"); $posX = 0; $posY = 0; $ratioOrig = $this->width / $this->height; $ratioNew = $this->newWidth / $this->newHeight; if ($ratioOrig < $ratioNew) { $posX = round(($this->newWidth - $this->fillWidth) / 2); } else { $posY = round(($this->newHeight - $this->fillHeight) / 2); } if (!$this->upscale && ($this->width < $this->newWidth || $this->height < $this->newHeight) ) { $this->log("Resizing - smaller image, do not upscale."); $posX = round(($this->fillWidth - $this->width) / 2); $posY = round(($this->fillHeight - $this->height) / 2); $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); imagecopy($imageResized, $this->image, $posX, $posY, 0, 0, $this->fillWidth, $this->fillHeight); } else { $imgPreFill = $this->CreateImageKeepTransparency($this->fillWidth, $this->fillHeight); $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); $this->imageCopyResampled($imgPreFill, $this->image, 0, 0, 0, 0, $this->fillWidth, $this->fillHeight, $this->width, $this->height); imagecopy($imageResized, $imgPreFill, $posX, $posY, 0, 0, $this->fillWidth, $this->fillHeight); } $this->image = $imageResized; $this->width = $this->newWidth; $this->height = $this->newHeight; } elseif (!($this->newWidth == $this->width && $this->newHeight == $this->height)) { $this->log("Resizing, new height and/or width"); if (!$this->upscale && ($this->width < $this->newWidth || $this->height < $this->newHeight) ) { $this->log("Resizing - smaller image, do not upscale."); if (!$this->keepRatio) { $this->log("Resizing - stretch to fit selected."); $posX = 0; $posY = 0; $cropX = 0; $cropY = 0; if ($this->newWidth > $this->width && $this->newHeight > $this->height) { $posX = round(($this->newWidth - $this->width) / 2); $posY = round(($this->newHeight - $this->height) / 2); } elseif ($this->newWidth > $this->width) { $posX = round(($this->newWidth - $this->width) / 2); $cropY = round(($this->height - $this->newHeight) / 2); } elseif ($this->newHeight > $this->height) { $posY = round(($this->newHeight - $this->height) / 2); $cropX = round(($this->width - $this->newWidth) / 2); } $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); imagecopy($imageResized, $this->image, $posX, $posY, $cropX, $cropY, $this->newWidth, $this->newHeight); $this->image = $imageResized; $this->width = $this->newWidth; $this->height = $this->newHeight; } } else { $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); $this->imageCopyResampled($imageResized, $this->image, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height); $this->image = $imageResized; $this->width = $this->newWidth; $this->height = $this->newHeight; } } return $this; } public function postResize() { $this->log("### Post-process after resizing"); if ($this->rotateAfter) { $this->log("Rotating image."); $this->rotate($this->rotateAfter, $this->bgColor); } if (isset($this->filters) && is_array($this->filters)) { foreach ($this->filters as $filter) { $this->log("Applying filter {$filter['type']}."); switch ($filter['argc']) { case 0: imagefilter($this->image, $filter['type']); break; case 1: imagefilter($this->image, $filter['type'], $filter['arg1']); break; case 2: imagefilter($this->image, $filter['type'], $filter['arg1'], $filter['arg2']); break; case 3: imagefilter($this->image, $filter['type'], $filter['arg1'], $filter['arg2'], $filter['arg3']); break; case 4: imagefilter($this->image, $filter['type'], $filter['arg1'], $filter['arg2'], $filter['arg3'], $filter['arg4']); break; } } } if ($this->palette) { $this->log("Converting to palette image."); $this->trueColorToPalette(); } if ($this->blur) { $this->log("Blur."); $this->blurImage(); } if ($this->emboss) { $this->log("Emboss."); $this->embossImage(); } if ($this->sharpen) { $this->log("Sharpen."); $this->sharpenImage(); } if ($this->convolve) { $this->imageConvolution(); } return $this; } public function rotate($angle, $bgColor) { $this->log("Rotate image " . $angle . " degrees with filler color."); $color = $this->getBackgroundColor(); $this->image = imagerotate($this->image, $angle, $color); $this->width = imagesx($this->image); $this->height = imagesy($this->image); $this->log("New image dimension width x height: " . $this->width . " x " . $this->height); return $this; } public function rotateExif() { if (!in_array($this->fileType, array(IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM))) { $this->log("Autorotate ignored, EXIF not supported by this filetype."); return $this; } $exif = exif_read_data($this->pathToImage); if (!empty($exif['Orientation'])) { switch ($exif['Orientation']) { case 3: $this->log("Autorotate 180."); $this->rotate(180, $this->bgColor); break; case 6: $this->log("Autorotate -90."); $this->rotate(-90, $this->bgColor); break; case 8: $this->log("Autorotate 90."); $this->rotate(90, $this->bgColor); break; default: $this->log("Autorotate ignored, unknown value as orientation."); } } else { $this->log("Autorotate ignored, no orientation in EXIF."); } return $this; } public function trueColorToPalette() { $img = imagecreatetruecolor($this->width, $this->height); $bga = imagecolorallocatealpha($img, 0, 0, 0, 127); imagecolortransparent($img, $bga); imagefill($img, 0, 0, $bga); imagecopy($img, $this->image, 0, 0, 0, 0, $this->width, $this->height); imagetruecolortopalette($img, false, 255); imagesavealpha($img, true); if (imageistruecolor($this->image)) { $this->log("Matching colors with true color image."); imagecolormatch($this->image, $img); } $this->image = $img; } public function sharpenImage() { $this->imageConvolution('sharpen'); return $this; } public function embossImage() { $this->imageConvolution('emboss'); return $this; } public function blurImage() { $this->imageConvolution('blur'); return $this; } public function createConvolveArguments($expression) { if (isset($this->convolves[$expression])) { $expression = $this->convolves[$expression]; } $part = explode(',', $expression); $this->log("Creating convolution expressen: $expression"); if (count($part) != 11) { throw new Exception( "Missmatch in argument convolve. Expected comma-separated string with 11 float values. Got $expression." ); } array_walk($part, function ($item, $key) { if (!is_numeric($item)) { throw new Exception("Argument to convolve expression should be float but is not."); } }); return array( array( array($part[0], $part[1], $part[2]), array($part[3], $part[4], $part[5]), array($part[6], $part[7], $part[8]), ), $part[9], $part[10], ); } public function addConvolveExpressions($options) { $this->convolves = array_merge($this->convolves, $options); return $this; } public function imageConvolution($options = null) { $options = $options ? $options : $this->convolve; $this->log("Convolution with '$options'"); $options = explode(":", $options); foreach ($options as $option) { list($matrix, $divisor, $offset) = $this->createConvolveArguments($option); imageconvolution($this->image, $matrix, $divisor, $offset); } return $this; } public function setDefaultBackgroundColor($color) { $this->log("Setting default background color to '$color'."); if (!(strlen($color) == 6 || strlen($color) == 8)) { throw new Exception( "Background color needs a hex value of 6 or 8 digits. 000000-FFFFFF or 00000000-FFFFFF7F. Current value was: '$color'." ); } $red = hexdec(substr($color, 0, 2)); $green = hexdec(substr($color, 2, 2)); $blue = hexdec(substr($color, 4, 2)); $alpha = (strlen($color) == 8) ? hexdec(substr($color, 6, 2)) : null; if (($red < 0 || $red > 255) || ($green < 0 || $green > 255) || ($blue < 0 || $blue > 255) || ($alpha < 0 || $alpha > 127) ) { throw new Exception( "Background color out of range. Red, green blue should be 00-FF and alpha should be 00-7F. - Current value was: '$color'." ); } $this->bgColor = strtolower($color); $this->bgColorDefault = array( 'red' => $red, 'green' => $green, 'blue' => $blue, 'alpha' => $alpha ); return $this; } private function getBackgroundColor($img = null) { $img = isset($img) ? $img : $this->image; if ($this->bgColorDefault) { $red = $this->bgColorDefault['red']; $green = $this->bgColorDefault['green']; $blue = $this->bgColorDefault['blue']; $alpha = $this->bgColorDefault['alpha']; if ($alpha) { $color = imagecolorallocatealpha($img, $red, $green, $blue, $alpha); } else { $color = imagecolorallocate($img, $red, $green, $blue); } return $color; } else { return 0; } } private function createImageKeepTransparency($width, $height) { $this->log("Creating a new working image width={$width}px, height={$height}px."); $img = imagecreatetruecolor($width, $height); imagealphablending($img, false); imagesavealpha($img, true); $index = $this->image ? imagecolortransparent($this->image) : -1; if ($index != -1) { imagealphablending($img, true); $transparent = imagecolorsforindex($this->image, $index); $color = imagecolorallocatealpha($img, $transparent['red'], $transparent['green'], $transparent['blue'], $transparent['alpha']); imagefill($img, 0, 0, $color); $index = imagecolortransparent($img, $color); $this->Log("Detected transparent color = " . implode(", ", $transparent) . " at index = $index"); } elseif ($this->bgColorDefault) { $color = $this->getBackgroundColor($img); imagefill($img, 0, 0, $color); $this->Log("Filling image with background color."); } return $img; } public function setPostProcessingOptions($options) { if (isset($options['jpeg_optimize']) && $options['jpeg_optimize']) { $this->jpegOptimizeCmd = $options['jpeg_optimize_cmd']; } else { $this->jpegOptimizeCmd = null; } if (isset($options['png_filter']) && $options['png_filter']) { $this->pngFilterCmd = $options['png_filter_cmd']; } else { $this->pngFilterCmd = null; } if (isset($options['png_deflate']) && $options['png_deflate']) { $this->pngDeflateCmd = $options['png_deflate_cmd']; } else { $this->pngDeflateCmd = null; } return $this; } protected function getTargetImageExtension() { if (isset($this->extension)) { return strtolower($this->extension); } else { return substr(image_type_to_extension($this->fileType), 1); } } public function save($src = null, $base = null, $overwrite = true) { if (isset($src)) { $this->setTarget($src, $base); } if ($overwrite === false && is_file($this->cacheFileName)) { $this->Log("Not overwriting file since its already exists and \$overwrite if false."); return; } is_writable($this->saveFolder) or $this->raiseError('Target directory is not writable.'); $type = $this->getTargetImageExtension(); $this->Log("Saving image as " . $type); switch ($type) { case 'jpeg': case 'jpg': $this->Log("Saving image as JPEG to cache using quality = {$this->quality}."); imagejpeg($this->image, $this->cacheFileName, $this->quality); if ($this->jpegOptimizeCmd) { if ($this->verbose) { clearstatcache(); $this->log("Filesize before optimize: " . filesize($this->cacheFileName) . " bytes."); } $res = array(); $cmd = $this->jpegOptimizeCmd . " -outfile $this->cacheFileName $this->cacheFileName"; exec($cmd, $res); $this->log($cmd); $this->log($res); } break; case 'gif': $this->Log("Saving image as GIF to cache."); imagegif($this->image, $this->cacheFileName); break; case 'png': default: $this->Log("Saving image as PNG to cache using compression = {$this->compress}."); imagealphablending($this->image, false); imagesavealpha($this->image, true); imagepng($this->image, $this->cacheFileName, $this->compress); if ($this->pngFilterCmd) { if ($this->verbose) { clearstatcache(); $this->Log("Filesize before filter optimize: " . filesize($this->cacheFileName) . " bytes."); } $res = array(); $cmd = $this->pngFilterCmd . " $this->cacheFileName"; exec($cmd, $res); $this->Log($cmd); $this->Log($res); } if ($this->pngDeflateCmd) { if ($this->verbose) { clearstatcache(); $this->Log("Filesize before deflate optimize: " . filesize($this->cacheFileName) . " bytes."); } $res = array(); $cmd = $this->pngDeflateCmd . " $this->cacheFileName"; exec($cmd, $res); $this->Log($cmd); $this->Log($res); } break; } if ($this->verbose) { clearstatcache(); $this->log("Saved image to cache."); $this->log(" Cached image filesize: " . filesize($this->cacheFileName) . " bytes."); $this->log(" imageistruecolor() : " . (imageistruecolor($this->image) ? 'true' : 'false')); $this->log(" imagecolorstotal() : " . imagecolorstotal($this->image)); $this->log(" Number of colors in image = " . $this->ColorsTotal($this->image)); $index = imagecolortransparent($this->image); $this->log(" Detected transparent color = " . ($index > 0 ? implode(", ", imagecolorsforindex($this->image, $index)) : "NONE") . " at index = $index"); } return $this; } public function convert2sRGBColorSpace($src, $dir, $cache, $iccFile, $useCache = true) { if ($this->verbose) { $this->log("# Converting image to sRGB colorspace."); } if (!class_exists("Imagick")) { $this->log(" Ignoring since Imagemagick is not installed."); return false; } $this->setSaveFolder($cache) ->setSource($src, $dir) ->generateFilename(null, false, 'srgb_'); if ($useCache && is_readable($this->cacheFileName)) { $fileTime = filemtime($this->pathToImage); $cacheTime = filemtime($this->cacheFileName); if ($fileTime <= $cacheTime) { $this->log(" Using cached version: " . $this->cacheFileName); return $this->cacheFileName; } } if (is_writable($this->saveFolder)) { $image = new Imagick($this->pathToImage); $colorspace = $image->getImageColorspace(); $this->log(" Current colorspace: " . $colorspace); $profiles = $image->getImageProfiles('*', false); $hasICCProfile = (array_search('icc', $profiles) !== false); $this->log(" Has ICC color profile: " . ($hasICCProfile ? "YES" : "NO")); if ($colorspace != Imagick::COLORSPACE_SRGB || $hasICCProfile) { $this->log(" Converting to sRGB."); $sRGBicc = file_get_contents($iccFile); $image->profileImage('icc', $sRGBicc); $image->transformImageColorspace(Imagick::COLORSPACE_SRGB); $image->writeImage($this->cacheFileName); return $this->cacheFileName; } } return false; } public function linkToCacheFile($alias) { if ($alias === null) { $this->log("Ignore creating alias."); return $this; } if (is_readable($alias)) { unlink($alias); } $res = link($this->cacheFileName, $alias); if ($res) { $this->log("Created an alias as: $alias"); } else { $this->log("Failed to create the alias: $alias"); } return $this; } public function addHTTPHeader($type, $value) { $this->HTTPHeader[$type] = $value; } public function output($file = null, $format = null) { if (is_null($file)) { $file = $this->cacheFileName; } if (is_null($format)) { $format = $this->outputFormat; } $this->log("Output format is: $format"); if (!$this->verbose && $format == 'json') { header('Content-type: application/json'); echo $this->json($file); exit; } elseif ($format == 'ascii') { header('Content-type: text/plain'); echo $this->ascii($file); exit; } $this->log("Outputting image: $file"); clearstatcache(); $lastModified = filemtime($file); $gmdate = gmdate("D, d M Y H:i:s", $lastModified); if (!$this->verbose) { header('Last-Modified: ' . $gmdate . " GMT"); } foreach ($this->HTTPHeader as $key => $val) { header("$key: $val"); } if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified) { if ($this->verbose) { $this->log("304 not modified"); $this->verboseOutput(); exit; } header("HTTP/1.0 304 Not Modified"); } else { $info = getimagesize($file); !empty($info) or $this->raiseError("The file doesn't seem to be an image."); $mime = $info['mime']; $size = filesize($file); if ($this->verbose) { $this->log("Last-Modified: " . $gmdate . " GMT"); $this->log("Content-type: " . $mime); $this->log("Content-length: " . $size); $this->verboseOutput(); if (is_null($this->verboseFileName)) { exit; } } header("Content-type: $mime"); header("Content-length: $size"); readfile($file); } exit; } public function json($file = null) { $file = $file ? $file : $this->cacheFileName; $details = array(); clearstatcache(); $details['src'] = $this->imageSrc; $lastModified = filemtime($this->pathToImage); $details['srcGmdate'] = gmdate("D, d M Y H:i:s", $lastModified); $details['cache'] = basename($this->cacheFileName); $lastModified = filemtime($this->cacheFileName); $details['cacheGmdate'] = gmdate("D, d M Y H:i:s", $lastModified); $this->load($file); $details['filename'] = basename($file); $details['mimeType'] = image_type_to_mime_type($this->fileType); $details['width'] = $this->width; $details['height'] = $this->height; $details['aspectRatio'] = round($this->width / $this->height, 3); $details['size'] = filesize($file); $details['colors'] = $this->colorsTotal($this->image); $details['includedFiles'] = count(get_included_files()); $details['memoryPeek'] = round(memory_get_peak_usage()/1024/1024, 3) . " MB" ; $details['memoryCurrent'] = round(memory_get_usage()/1024/1024, 3) . " MB"; $details['memoryLimit'] = ini_get('memory_limit'); if (isset($_SERVER['REQUEST_TIME_FLOAT'])) { $details['loadTime'] = (string) round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 3) . "s"; } if ($details['mimeType'] == 'image/png') { $details['pngType'] = $this->getPngTypeAsString(null, $file); } $options = null; if (defined("JSON_PRETTY_PRINT") && defined("JSON_UNESCAPED_SLASHES")) { $options = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES; } return json_encode($details, $options); } public function setAsciiOptions($options = array()) { $this->asciiOptions = $options; } public function ascii($file = null) { $file = $file ? $file : $this->cacheFileName; $asciiArt = new CAsciiArt(); $asciiArt->setOptions($this->asciiOptions); return $asciiArt->createFromFile($file); } public function log($message) { if ($this->verbose) { $this->log[] = $message; } return $this; } public function setVerboseToFile($fileName) { $this->log("Setting verbose output to file."); $this->verboseFileName = $fileName; } private function verboseOutput() { $log = null; $this->log("As JSON: \n" . $this->json()); $this->log("Memory peak: " . round(memory_get_peak_usage() /1024/1024) . "M"); $this->log("Memory limit: " . ini_get('memory_limit')); $included = get_included_files(); $this->log("Included files: " . count($included)); foreach ($this->log as $val) { if (is_array($val)) { foreach ($val as $val1) { $log .= htmlentities($val1) . '
'; } } else { $log .= htmlentities($val) . '
'; } } if (!is_null($this->verboseFileName)) { file_put_contents( $this->verboseFileName, str_replace("
", "\n", $log) ); } else { echo <<bgColor = strtolower($color); $this->bgColorDefault = array( 'red' => $red, 'green' => $green, 'blue' => $blue, 'alpha' => $alpha ); return $this; } private function getBackgroundColor($img = null) { $img = isset($img) ? $img : $this->image; if ($this->bgColorDefault) { $red = $this->bgColorDefault['red']; $green = $this->bgColorDefault['green']; $blue = $this->bgColorDefault['blue']; $alpha = $this->bgColorDefault['alpha']; if ($alpha) { $color = imagecolorallocatealpha($img, $red, $green, $blue, $alpha); } else { $color = imagecolorallocate($img, $red, $green, $blue); } return $color; } else { return 0; } } private function createImageKeepTransparency($width, $height) { $this->log("Creating a new working image width={$width}px, height={$height}px."); $img = imagecreatetruecolor($width, $height); imagealphablending($img, false); imagesavealpha($img, true); $index = $this->image ? imagecolortransparent($this->image) : -1; if ($index != -1) { imagealphablending($img, true); $transparent = imagecolorsforindex($this->image, $index); $color = imagecolorallocatealpha($img, $transparent['red'], $transparent['green'], $transparent['blue'], $transparent['alpha']); imagefill($img, 0, 0, $color); $index = imagecolortransparent($img, $color); $this->Log("Detected transparent color = " . implode(", ", $transparent) . " at index = $index"); } elseif ($this->bgColorDefault) { $color = $this->getBackgroundColor($img); imagefill($img, 0, 0, $color); $this->Log("Filling image with background color."); } return $img; } public function setPostProcessingOptions($options) { if (isset($options['jpeg_optimize']) && $options['jpeg_optimize']) { $this->jpegOptimizeCmd = $options['jpeg_optimize_cmd']; } else { $this->jpegOptimizeCmd = null; } if (isset($options['png_filter']) && $options['png_filter']) { $this->pngFilterCmd = $options['png_filter_cmd']; } else { $this->pngFilterCmd = null; } if (isset($options['png_deflate']) && $options['png_deflate']) { $this->pngDeflateCmd = $options['png_deflate_cmd']; } else { $this->pngDeflateCmd = null; } return $this; } protected function getTargetImageExtension() { if (isset($this->extension)) { return strtolower($this->extension); } else { return substr(image_type_to_extension($this->fileType), 1); } } public function save($src = null, $base = null, $overwrite = true) { if (isset($src)) { $this->setTarget($src, $base); } if ($overwrite === false && is_file($this->cacheFileName)) { $this->Log("Not overwriting file since its already exists and \$overwrite if false."); return; } is_writable($this->saveFolder) or $this->raiseError('Target directory is not writable.'); $type = $this->getTargetImageExtension(); $this->Log("Saving image as " . $type); switch($type) { case 'jpeg': case 'jpg': $this->Log("Saving image as JPEG to cache using quality = {$this->quality}."); imagejpeg($this->image, $this->cacheFileName, $this->quality); if ($this->jpegOptimizeCmd) { if ($this->verbose) { clearstatcache(); $this->log("Filesize before optimize: " . filesize($this->cacheFileName) . " bytes."); } $res = array(); $cmd = $this->jpegOptimizeCmd . " -outfile $this->cacheFileName $this->cacheFileName"; exec($cmd, $res); $this->log($cmd); $this->log($res); } break; case 'gif': $this->Log("Saving image as GIF to cache."); imagegif($this->image, $this->cacheFileName); break; case 'png': default: $this->Log("Saving image as PNG to cache using compression = {$this->compress}."); imagealphablending($this->image, false); imagesavealpha($this->image, true); imagepng($this->image, $this->cacheFileName, $this->compress); if ($this->pngFilterCmd) { if ($this->verbose) { clearstatcache(); $this->Log("Filesize before filter optimize: " . filesize($this->cacheFileName) . " bytes."); } $res = array(); $cmd = $this->pngFilterCmd . " $this->cacheFileName"; exec($cmd, $res); $this->Log($cmd); $this->Log($res); } if ($this->pngDeflateCmd) { if ($this->verbose) { clearstatcache(); $this->Log("Filesize before deflate optimize: " . filesize($this->cacheFileName) . " bytes."); } $res = array(); $cmd = $this->pngDeflateCmd . " $this->cacheFileName"; exec($cmd, $res); $this->Log($cmd); $this->Log($res); } break; } if ($this->verbose) { clearstatcache(); $this->log("Saved image to cache."); $this->log(" Cached image filesize: " . filesize($this->cacheFileName) . " bytes."); $this->log(" imageistruecolor() : " . (imageistruecolor($this->image) ? 'true' : 'false')); $this->log(" imagecolorstotal() : " . imagecolorstotal($this->image)); $this->log(" Number of colors in image = " . $this->ColorsTotal($this->image)); $index = imagecolortransparent($this->image); $this->log(" Detected transparent color = " . ($index > 0 ? implode(", ", imagecolorsforindex($this->image, $index)) : "NONE") . " at index = $index"); } return $this; } public function convert2sRGBColorSpace($src, $dir, $cache, $iccFile, $useCache = true) { if ($this->verbose) { $this->log("# Converting image to sRGB colorspace."); } if (!class_exists("Imagick")) { $this->log(" Ignoring since Imagemagick is not installed."); return false; } $this->setSaveFolder($cache) ->setSource($src, $dir) ->generateFilename(null, false, 'srgb_'); if ($useCache && is_readable($this->cacheFileName)) { $fileTime = filemtime($this->pathToImage); $cacheTime = filemtime($this->cacheFileName); if ($fileTime <= $cacheTime) { $this->log(" Using cached version: " . $this->cacheFileName); return $this->cacheFileName; } } if (is_writable($this->saveFolder)) { $image = new Imagick($this->pathToImage); $colorspace = $image->getImageColorspace(); $this->log(" Current colorspace: " . $colorspace); $profiles = $image->getImageProfiles('*', false); $hasICCProfile = (array_search('icc', $profiles) !== false); $this->log(" Has ICC color profile: " . ($hasICCProfile ? "YES" : "NO")); if ($colorspace != Imagick::COLORSPACE_SRGB || $hasICCProfile) { $this->log(" Converting to sRGB."); $sRGBicc = file_get_contents($iccFile); $image->profileImage('icc', $sRGBicc); $image->transformImageColorspace(Imagick::COLORSPACE_SRGB); $image->writeImage($this->cacheFileName); return $this->cacheFileName; } } return false; } public function linkToCacheFile($alias) { if ($alias === null) { $this->log("Ignore creating alias."); return $this; } if (is_readable($alias)) { unlink($alias); } $res = link($this->cacheFileName, $alias); if ($res) { $this->log("Created an alias as: $alias"); } else { $this->log("Failed to create the alias: $alias"); } return $this; } public function addHTTPHeader($type, $value) { $this->HTTPHeader[$type] = $value; } public function output($file = null, $format = null) { if (is_null($file)) { $file = $this->cacheFileName; } if (is_null($format)) { $format = $this->outputFormat; } $this->log("Output format is: $format"); if (!$this->verbose && $format == 'json') { header('Content-type: application/json'); echo $this->json($file); exit; } elseif ($format == 'ascii') { header('Content-type: text/plain'); echo $this->ascii($file); exit; } $this->log("Outputting image: $file"); clearstatcache(); $lastModified = filemtime($file); $gmdate = gmdate("D, d M Y H:i:s", $lastModified); if (!$this->verbose) { header('Last-Modified: ' . $gmdate . " GMT"); } foreach($this->HTTPHeader as $key => $val) { header("$key: $val"); } if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified) { if ($this->verbose) { $this->log("304 not modified"); $this->verboseOutput(); exit; } header("HTTP/1.0 304 Not Modified"); } else { $info = getimagesize($file); !empty($info) or $this->raiseError("The file doesn't seem to be an image."); $mime = $info['mime']; $size = filesize($file); if ($this->verbose) { $this->log("Last-Modified: " . $gmdate . " GMT"); $this->log("Content-type: " . $mime); $this->log("Content-length: " . $size); $this->verboseOutput(); if (is_null($this->verboseFileName)) { exit; } } header("Content-type: $mime"); header("Content-length: $size"); readfile($file); } exit; } public function json($file = null) { $file = $file ? $file : $this->cacheFileName; $details = array(); clearstatcache(); $details['src'] = $this->imageSrc; $lastModified = filemtime($this->pathToImage); $details['srcGmdate'] = gmdate("D, d M Y H:i:s", $lastModified); $details['cache'] = basename($this->cacheFileName); $lastModified = filemtime($this->cacheFileName); $details['cacheGmdate'] = gmdate("D, d M Y H:i:s", $lastModified); $this->load($file); $details['filename'] = basename($file); $details['mimeType'] = image_type_to_mime_type($this->fileType); $details['width'] = $this->width; $details['height'] = $this->height; $details['aspectRatio'] = round($this->width / $this->height, 3); $details['size'] = filesize($file); $details['colors'] = $this->colorsTotal($this->image); $details['includedFiles'] = count(get_included_files()); $details['memoryPeek'] = round(memory_get_peak_usage()/1024/1024, 3) . " MB" ; $details['memoryCurrent'] = round(memory_get_usage()/1024/1024, 3) . " MB"; $details['memoryLimit'] = ini_get('memory_limit'); if (isset($_SERVER['REQUEST_TIME_FLOAT'])) { $details['loadTime'] = (string) round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 3) . "s"; } if ($details['mimeType'] == 'image/png') { $details['pngType'] = $this->getPngTypeAsString(null, $file); } $options = null; if (defined("JSON_PRETTY_PRINT") && defined("JSON_UNESCAPED_SLASHES")) { $options = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES; } return json_encode($details, $options); } public function setAsciiOptions($options = array()) { $this->asciiOptions = $options; } public function ascii($file = null) { $file = $file ? $file : $this->cacheFileName; $asciiArt = new CAsciiArt(); $asciiArt->setOptions($this->asciiOptions); return $asciiArt->createFromFile($file); } public function log($message) { if ($this->verbose) { $this->log[] = $message; } return $this; } public function setVerboseToFile($fileName) { $this->log("Setting verbose output to file."); $this->verboseFileName = $fileName; } private function verboseOutput() { $log = null; $this->log("As JSON: \n" . $this->json()); $this->log("Memory peak: " . round(memory_get_peak_usage() /1024/1024) . "M"); $this->log("Memory limit: " . ini_get('memory_limit')); $included = get_included_files(); $this->log("Included files: " . count($included)); foreach ($this->log as $val) { if (is_array($val)) { foreach ($val as $val1) { $log .= htmlentities($val1) . '
'; } } else { $log .= htmlentities($val) . '
'; } } if (!is_null($this->verboseFileName)) { file_put_contents( $this->verboseFileName, str_replace("
", "\n", $log) ); } else { echo <<CImage Verbose Output
{$log}
EOD;