1
0
mirror of https://github.com/mosbth/cimage.git synced 2025-07-25 18:51:19 +02:00

* Cache now uses same file extension as original image #37.

* Can output image as json format using `json` #11.
This commit is contained in:
Mikael Roos
2014-11-21 22:20:30 +01:00
parent ccc59fbb3a
commit ba45f746bb
3 changed files with 75 additions and 7 deletions

View File

@@ -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 * Verbose mode to print out a trace and display the created image
*/ */
@@ -263,7 +270,7 @@ class CImage
$this->imageFolder = rtrim($dir, '/'); $this->imageFolder = rtrim($dir, '/');
$this->pathToImage = $this->imageFolder . '/' . $this->imageSrc; $this->pathToImage = $this->imageFolder . '/' . $this->imageSrc;
$this->fileExtension = strtolower(pathinfo($this->pathToImage, PATHINFO_EXTENSION)); $this->fileExtension = strtolower(pathinfo($this->pathToImage, PATHINFO_EXTENSION));
$this->extension = $this->fileExtension; //$this->extension = $this->fileExtension;
$this->checkFileExtension($this->fileExtension); $this->checkFileExtension($this->fileExtension);
@@ -342,6 +349,9 @@ class CImage
'rotateAfter' => null, 'rotateAfter' => null,
'autoRotate' => false, 'autoRotate' => false,
// Output format
'outputFormat' => null,
// Options for saving // Options for saving
//'quality' => null, //'quality' => null,
//'compress' => null, //'compress' => null,
@@ -811,11 +821,9 @@ class CImage
$autoRotate = $this->autoRotate ? 'ar' : null; $autoRotate = $this->autoRotate ? 'ar' : null;
/*
$this->extension = isset($this->extension) $this->extension = isset($this->extension)
? $this->extension ? $this->extension
: $parts['extension']; : $parts['extension'];
*/
// Check optimizing options // Check optimizing options
$optimize = null; $optimize = null;
@@ -857,7 +865,7 @@ class CImage
$this->log("Use cached file."); $this->log("Use cached file.");
$this->log("Cached image filesize: " . filesize($this->cacheFileName) . " bytes."); $this->log("Cached image filesize: " . filesize($this->cacheFileName) . " bytes.");
} }
$this->output($this->cacheFileName); $this->output($this->cacheFileName, $this->outputFormat);
} else { } else {
$this->log("Cache is valid but ignoring it by intention."); $this->log("Cache is valid but ignoring it by intention.");
} }
@@ -1454,7 +1462,7 @@ class CImage
$this->setTarget($src, $base); $this->setTarget($src, $base);
} }
switch($this->extension) { switch(strtolower($this->extension)) {
case 'jpeg': case 'jpeg':
case 'jpg': case 'jpg':
@@ -1538,16 +1546,29 @@ class CImage
/** /**
* Output image to browser using caching. * 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 * @return void
*/ */
public function output($file = null) public function output($file = null, $format = null)
{ {
if (is_null($file)) { if (is_null($file)) {
$file = $this->cacheFileName; $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"); $this->log("Outputting image: $file");
// Get image modification time // 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. * Log an event if verbose mode.
* *
@@ -1617,6 +1665,7 @@ class CImage
private function verboseOutput() private function verboseOutput()
{ {
$log = null; $log = null;
$this->log("As JSON: \n" . $this->json());
$this->log("Memory peak: " . round(memory_get_peak_usage() /1024/1024) . "M"); $this->log("Memory peak: " . round(memory_get_peak_usage() /1024/1024) . "M");
$this->log("Memory limit: " . ini_get('memory_limit')); $this->log("Memory limit: " . ini_get('memory_limit'));

View File

@@ -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) v0.5.3 (2014-11-21)
* Support filenames of uppercase JPEG, JPG, PNG and GIF, as proposed in #37. * Support filenames of uppercase JPEG, JPG, PNG and GIF, as proposed in #37.

View File

@@ -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 * Display image if verbose mode
*/ */
@@ -464,6 +473,7 @@ if ($verbose) {
unset($query['v']); unset($query['v']);
unset($query['nocache']); unset($query['nocache']);
unset($query['nc']); unset($query['nc']);
unset($query['json']);
$url1 = '?' . http_build_query($query); $url1 = '?' . http_build_query($query);
echo <<<EOD echo <<<EOD
<a href=$url1><code>$url1</code></a><br> <a href=$url1><code>$url1</code></a><br>
@@ -509,6 +519,9 @@ $img->setVerbose($verbose)
'blur' => $blur, 'blur' => $blur,
'rotateAfter' => $rotateAfter, 'rotateAfter' => $rotateAfter,
'autoRotate' => $autoRotate, 'autoRotate' => $autoRotate,
// Output format
'outputFormat' => $outputFormat,
) )
) )
->loadImageDetails() ->loadImageDetails()