From ba45f746bbe2a8a3a08cb80a1c19d98ea7b0551a Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Fri, 21 Nov 2014 22:20:30 +0100 Subject: [PATCH] * Cache now uses same file extension as original image #37. * Can output image as json format using `json` #11. --- CImage.php | 63 +++++++++++++++++++++++++++++++++++++++++++------ README.md | 6 +++++ webroot/img.php | 13 ++++++++++ 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/CImage.php b/CImage.php index 3f631b8..94f745d 100644 --- a/CImage.php +++ b/CImage.php @@ -97,6 +97,13 @@ class CImage + /** + * Output format, supports null (image) or json. + */ + private $outputFormat = null; + + + /** * Verbose mode to print out a trace and display the created image */ @@ -263,7 +270,7 @@ class CImage $this->imageFolder = rtrim($dir, '/'); $this->pathToImage = $this->imageFolder . '/' . $this->imageSrc; $this->fileExtension = strtolower(pathinfo($this->pathToImage, PATHINFO_EXTENSION)); - $this->extension = $this->fileExtension; + //$this->extension = $this->fileExtension; $this->checkFileExtension($this->fileExtension); @@ -342,6 +349,9 @@ class CImage 'rotateAfter' => null, 'autoRotate' => false, + // Output format + 'outputFormat' => null, + // Options for saving //'quality' => null, //'compress' => null, @@ -811,11 +821,9 @@ class CImage $autoRotate = $this->autoRotate ? 'ar' : null; -/* $this->extension = isset($this->extension) ? $this->extension : $parts['extension']; -*/ // Check optimizing options $optimize = null; @@ -857,7 +865,7 @@ class CImage $this->log("Use cached file."); $this->log("Cached image filesize: " . filesize($this->cacheFileName) . " bytes."); } - $this->output($this->cacheFileName); + $this->output($this->cacheFileName, $this->outputFormat); } else { $this->log("Cache is valid but ignoring it by intention."); } @@ -1454,7 +1462,7 @@ class CImage $this->setTarget($src, $base); } - switch($this->extension) { + switch(strtolower($this->extension)) { case 'jpeg': case 'jpg': @@ -1538,16 +1546,29 @@ class CImage /** * Output image to browser using caching. * - * @param string $file to read and output, default is to use $this->cacheFileName + * @param string $file to read and output, default is to use $this->cacheFileName + * @param string $format set to json to output file as json object with details * * @return void */ - public function output($file = null) + 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; + } + $this->log("Outputting image: $file"); // Get image modification time @@ -1591,6 +1612,33 @@ class CImage + /** + * Create a JSON object from the image details. + * + * @return string json-encoded representation of the image. + */ + public function json() + { + $details = array(); + + clearstatcache(); + + $details['src'] = $this->imageSrc; + $lastModified = filemtime($this->pathToImage); + $details['src-gmdate'] = gmdate("D, d M Y H:i:s", $lastModified); + + $details['cache'] = basename($this->cacheFileName); + $lastModified = filemtime($this->cacheFileName); + $details['cache-gmdate'] = gmdate("D, d M Y H:i:s", $lastModified); + + $details['width'] = $this->width; + $details['height'] = $this->height; + + return json_encode($details, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + } + + + /** * Log an event if verbose mode. * @@ -1617,6 +1665,7 @@ class CImage 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')); diff --git a/README.md b/README.md index 90422f1..b0b95ac 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,12 @@ Revision history ------------------------------------- +v0.5.x (latest) + +* Cache now uses same file extension as original image #37. +* Can output image as json format using `json` #11. + + v0.5.3 (2014-11-21) * Support filenames of uppercase JPEG, JPG, PNG and GIF, as proposed in #37. diff --git a/webroot/img.php b/webroot/img.php index cb1008e..ca5700b 100644 --- a/webroot/img.php +++ b/webroot/img.php @@ -454,6 +454,15 @@ verbose("filters = " . print_r($filters, 1)); +/** + * json - output the image as a JSON object with details on the image. + */ +$outputFormat = getDefined('json', 'json', null); + +verbose("json = $outputFormat"); + + + /** * Display image if verbose mode */ @@ -464,6 +473,7 @@ if ($verbose) { unset($query['v']); unset($query['nocache']); unset($query['nc']); + unset($query['json']); $url1 = '?' . http_build_query($query); echo <<$url1
@@ -509,6 +519,9 @@ $img->setVerbose($verbose) 'blur' => $blur, 'rotateAfter' => $rotateAfter, 'autoRotate' => $autoRotate, + + // Output format + 'outputFormat' => $outputFormat, ) ) ->loadImageDetails()