1
0
mirror of https://github.com/mosbth/cimage.git synced 2025-08-03 22:57:43 +02:00

Corrected behaviour for skip-original. Fix #60

This commit is contained in:
Mikael Roos
2014-12-13 00:12:39 +01:00
parent 9680cd421b
commit 00dca21be0
5 changed files with 84 additions and 10 deletions

View File

@@ -34,6 +34,13 @@ class CImage
/**
* Is the quality level set from external use (true) or is it default (false)?
*/
private $useQuality = false;
/** /**
* Constant for default image quality when not set * Constant for default image quality when not set
*/ */
@@ -48,6 +55,14 @@ class CImage
/**
* Is the compress level set from external use (true) or is it default (false)?
*/
private $useCompress = false;
/** /**
* Default background color, red, green, blue, alpha. * Default background color, red, green, blue, alpha.
* *
@@ -548,22 +563,26 @@ class CImage
/** /**
* Load image details from original image file. * Load image details from original image file.
* *
* @param string $file the file to load or null to use $this->pathToImage.
*
* @return $this * @return $this
* @throws Exception * @throws Exception
*/ */
public function loadImageDetails() public function loadImageDetails($file = null)
{ {
is_readable($this->pathToImage) $file = $file ? $file : $this->pathToImage;
is_readable($file)
or $this->raiseError('Image file does not exist.'); or $this->raiseError('Image file does not exist.');
// Get details on image // Get details on image
$info = list($this->width, $this->height, $this->type, $this->attr) = getimagesize($this->pathToImage); $info = list($this->width, $this->height, $this->type, $this->attr) = getimagesize($file);
!empty($info) or $this->raiseError("The file doesn't seem to be an image."); !empty($info) or $this->raiseError("The file doesn't seem to be an image.");
if ($this->verbose) { if ($this->verbose) {
$this->log("Image file: {$this->pathToImage}"); $this->log("Image file: {$file}");
$this->log("Image width x height (type): {$this->width} x {$this->height} ({$this->type})."); $this->log("Image width x height (type): {$this->width} x {$this->height} ({$this->type}).");
$this->log("Image filesize: " . filesize($this->pathToImage) . " bytes."); $this->log("Image filesize: " . filesize($file) . " bytes.");
} }
return $this; return $this;
@@ -813,6 +832,10 @@ class CImage
*/ */
public function setJpegQuality($quality = null) public function setJpegQuality($quality = null)
{ {
if ($quality) {
$this->useQuality = true;
}
$this->quality = isset($quality) $this->quality = isset($quality)
? $quality ? $quality
: self::JPEG_QUALITY_DEFAULT; : self::JPEG_QUALITY_DEFAULT;
@@ -836,6 +859,10 @@ class CImage
*/ */
public function setPngCompression($compress = null) public function setPngCompression($compress = null)
{ {
if ($compress) {
$this->useCompress = true;
}
$this->compress = isset($compress) $this->compress = isset($compress)
? $compress ? $compress
: self::PNG_COMPRESSION_DEFAULT; : self::PNG_COMPRESSION_DEFAULT;
@@ -872,8 +899,8 @@ class CImage
&& !$this->blur && !$this->blur
&& !$this->convolve && !$this->convolve
&& !$this->palette && !$this->palette
&& !$this->quality && !$this->useQuality
&& !$this->compress && !$this->useCompress
&& !$this->saveAs && !$this->saveAs
&& !$this->rotateBefore && !$this->rotateBefore
&& !$this->rotateAfter && !$this->rotateAfter
@@ -883,6 +910,7 @@ class CImage
$this->log("Using original image."); $this->log("Using original image.");
$this->output($this->pathToImage); $this->output($this->pathToImage);
} }
return $this; return $this;
} }
@@ -1919,10 +1947,14 @@ class CImage
/** /**
* Create a JSON object from the image details. * Create a JSON object from the image details.
* *
* @param string $file the file to output.
*
* @return string json-encoded representation of the image. * @return string json-encoded representation of the image.
*/ */
public function json() public function json($file = null)
{ {
$file = $file ? $file : $this->cacheFileName;
$details = array(); $details = array();
clearstatcache(); clearstatcache();
@@ -1935,9 +1967,13 @@ class CImage
$lastModified = filemtime($this->cacheFileName); $lastModified = filemtime($this->cacheFileName);
$details['cacheGmdate'] = gmdate("D, d M Y H:i:s", $lastModified); $details['cacheGmdate'] = gmdate("D, d M Y H:i:s", $lastModified);
$this->loadImageDetails($file);
$details['filename'] = basename($file);
$details['width'] = $this->width; $details['width'] = $this->width;
$details['height'] = $this->height; $details['height'] = $this->height;
$details['aspectRatio'] = round($this->width / $this->height, 3); $details['aspectRatio'] = round($this->width / $this->height, 3);
$details['size'] = filesize($file);
$options = null; $options = null;
if (defined("JSON_PRETTY_PRINT") && defined("JSON_UNESCAPED_SLASHES")) { if (defined("JSON_PRETTY_PRINT") && defined("JSON_UNESCAPED_SLASHES")) {

View File

@@ -278,6 +278,11 @@ Revision history
------------------------------------- -------------------------------------
v0.6.x (latest)
* Corrected behaviour for skip-original. Fix #60.
v0.6 (2014-12-06) v0.6 (2014-12-06)
* Rewrote and added documentation. * Rewrote and added documentation.

View File

@@ -341,7 +341,7 @@ verbose("area = $area");
/** /**
* skip-original, so - skip the original image and always process a new image * skip-original, so - skip the original image and always process a new image
*/ */
$useOriginal = getDefined(array('save-as', 'sa'), false, true); $useOriginal = getDefined(array('skip-original', 'so'), false, true);
verbose("use original = $useOriginal"); verbose("use original = $useOriginal");

View File

@@ -11,7 +11,7 @@
window.getDetails = function (url, id) { window.getDetails = function (url, id) {
$.getJSON(url, function(data) { $.getJSON(url, function(data) {
element = document.getElementById(id); element = document.getElementById(id);
element.innerHTML = "width: " + data.width + "\nheigh: " + data.height + "\naspect-ratio: " + data.aspectRatio; element.innerHTML = "filename: " + data.filename + "\nsize: " + data.size + "\nwidth: " + data.width + "\nheigh: " + data.height + "\naspect-ratio: " + data.aspectRatio;
}); });
} }
</script> </script>

View File

@@ -0,0 +1,33 @@
<?php
// Include config for all testcases
include __DIR__ . "/config.php";
// The title of the test case
$title = "Testing issue 60 - skip original";
// Provide a short description of the testcase.
$description = "Always use the original if suitable, use skip-original to force using the cached version.";
// Use these images in the test
$images = array(
'car.png',
);
// For each image, apply these testcases
$testcase = array(
'',
'&so',
);
// Apply testcases and present results
include __DIR__ . "/template.php";