From 17885535940efcf5d68094c2a212ea1fe7cc88ef Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sun, 27 Aug 2017 00:50:12 +0200 Subject: [PATCH] Add gray and blur features --- README.md | 17 +++++++--- index.php | 93 ++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 84 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index bbbf5b6..e7fc267 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,25 @@ # ![Nyan Cat](https://raw.githubusercontent.com/Devenet/NyanPlaceholder/master/assets/icon/favicon.png) Nyan Cat Placeholder -Replace your depressing image placeholder by a happy Nyan Cat image placeholder. +Replace your depressing placeholder image by a happy Nyan Cat placeholder image. *** -For a `300×200px` Nyan Cat image placeholder, use: +☞ For a square `200px` Nyan Cat placeholder image: + + http://github.banana.dev/NyanPlaceholder/200 + +For a `300×200px` Nyan Cat placeholder image: http://github.banana.dev/NyanPlaceholder/300/200 -For a square `200px` Nyan Cat image placeholder, use: - http://github.banana.dev/NyanPlaceholder/200 +☞ For a _gray_ Nyan Cat placeholder image: + + http://github.banana.dev/NyanPlaceholder/g/200 + +☞ For a _blur_ Nyan Cat placeholder image: + + http://github.banana.dev/NyanPlaceholder/b/200 ![Nyan Cat](https://labs.devenet.eu/nyancat-placeholder/150) diff --git a/index.php b/index.php index 9e696c9..3398193 100644 --- a/index.php +++ b/index.php @@ -6,11 +6,13 @@ define('IMG_SOURCE_HEIGHT', 1500); define('IMG_SOURCE_RATIO', 1.66); define('IMG_COMPRESSION', 85); - define('CACHE_FOLDER', './cache/'); - define('CACHE_IMG_NAME', 'nyan_%sx%s.jpeg'); + define('IMG_DEST_NAME', 'nyan_%sx%s'); + define('IMG_DEST_EXTENSION', '.jpeg'); define('SOCIAL_TITLE', 'Nyan Cat Placeholder'); - define('SOCIAL_DESC', 'Replace your depressing image placeholder by a happy Nyan Cat image placeholder.'); + define('SOCIAL_DESC', 'Replace your depressing placeholder image by a happy Nyan Cat placeholder image.'); define('SOCIAL_IMG', URL.'/assets/img/nyan_social.png'); + define('CACHE_FOLDER', './cache/'); + define('CACHE_ENABLED', true); function parsingError() { @@ -33,12 +35,18 @@ function parseParameter($parameter) $img = new NyanPlaceholder(); - // do we want black and white image? + // do we want gray image? if ($r[0] === 'g') { $img->setIsGray(true); array_shift($r); } + // do we want blur image? + else if ($r[0] === 'b') + { + $img->setIsBlur(true); + array_shift($r); + } // no parameters omitted? if (empty($r[0])) @@ -63,23 +71,35 @@ class NyanPlaceholder private $width; private $height; private $isGray = false; + private $isBlur = false; public function setWidth($width) { $this->width = $width; } public function setHeight($height) { $this->height = $height; } public function setIsGray($isGray) { $this->isGray = $isGray; } + public function setIsBlur($isBlur) { $this->isBlur = $isBlur; } private function fixHeight() { $this->height = empty($this->height) ? $this->width : $this->height; } + + private function filename() + { + $name = sprintf(IMG_DEST_NAME, $this->width, $this->height); + if ($this->isGray || $this->isBlur) + { + $name .= sprintf('_%s', $this->isGray ? 'gray' : 'blur'); + } + return $name.IMG_DEST_EXTENSION; + } public function display() { $this->fixHeight(); header('Content-Type: image/jpeg'); - header('Content-Disposition: inline; filename="nyan_'.$this->width.'x'.$this->height.'.jpeg"'); + header('Content-Disposition: inline; filename="'.$this->filename().'"'); // can we use cached image? - $image_path = CACHE_FOLDER.sprintf(CACHE_IMG_NAME, $this->width, $this->height); - if (file_exists($image_path)) + $image_path = CACHE_FOLDER.$this->filename(); + if (CACHE_ENABLED && file_exists($image_path)) { exit(file_get_contents($image_path)); } @@ -106,10 +126,21 @@ public function display() $dest = imagecreatetruecolor($this->width, $this->height); - $white = imagecolorallocate($dest, 1, 38, 77); - imagefilledrectangle($dest, 0, 0, $this->width, $this->height, $white); + $background = imagecolorallocate($dest, 1, 38, 77); + imagefilledrectangle($dest, 0, 0, $this->width, $this->height, $background); imageinterlace($dest, 1); imagecopyresized($dest, $src, $width_gap, $height_gap, 0, 0, $width, $height, IMG_SOURCE_WIDTH, IMG_SOURCE_HEIGHT); + + if ($this->isGray) + { + imagefilter($dest, IMG_FILTER_GRAYSCALE); + } + else if ($this->isBlur) + { + imagefilter($dest, IMG_FILTER_GAUSSIAN_BLUR); + imagefilter($dest, IMG_FILTER_PIXELATE, 5, true); + imagefilter($dest, IMG_FILTER_GAUSSIAN_BLUR); + } // hack to avoid bad image format for browser if (isset($_GET['cache'])) @@ -122,7 +153,9 @@ public function display() imagejpeg($dest, NULL, IMG_COMPRESSION); imagedestroy($dest); - file_get_contents(URL.'/'.$this->width.'/'.$this->height.'?cache'); + // hack to avoid bad image format for browser + if (CACHE_ENABLED) { file_get_contents(URL.'/'.$_SERVER['QUERY_STRING'].'?cache'); } + exit; } } @@ -166,11 +199,20 @@ public function display() @@ -189,19 +231,26 @@ public function display()
-

For a 300×200px Nyan Cat image placeholder, use:

-
/300/200
+

For a square 100px image

+
/100
+ Nyan Cat Placeholder + +

For a 100×80px image

+
/100/80
-

For a square 200px Nyan Cat image placeholder, use:

-
/200
+

For a gray image

+
/g/100
+ Nyan Cat Placeholder
-
- +
+

For a blur image

+
/b/100
+ Nyan Cat Placeholder +
+