1
0
mirror of https://github.com/mosbth/cimage.git synced 2025-04-22 09:54:07 +02:00

Correcting internal details for save-as and response json which indicated wrong colors. Fix #62.

This commit is contained in:
Mikael Roos 2014-12-15 09:19:23 +01:00
parent e236757e5d
commit ece0be086c
5 changed files with 51 additions and 14 deletions

View File

@ -369,10 +369,13 @@ class CImage
*
* @return $this
*/
public function setSource($src = null, $dir = null)
public function setSource($src, $dir = null)
{
if (!(isset($src) && isset($dir))) {
if (!isset($src)) {
return $this;
} else if (!isset($dir)) {
$dir = dirname($src);
$src = basename($src);
}
$this->imageSrc = ltrim($src, '/');
@ -1215,6 +1218,7 @@ class CImage
private function colorsTotal($im)
{
if (imageistruecolor($im)) {
$this->log("Colors as true color.");
$h = imagesy($im);
$w = imagesx($im);
$c = array();
@ -1225,6 +1229,7 @@ class CImage
}
return count($c);
} else {
$this->log("Colors as palette.");
return imagecolorstotal($im);
}
}
@ -1292,22 +1297,12 @@ class CImage
$this->height = $this->offset['height'];
}
// SaveAs need to copy image to remove transparency, if any
if ($this->saveAs) {
$this->log("Copying image before saving as another format, loosing transparency, width={$this->width}, height={$this->height}.");
$img = imagecreatetruecolor($this->width, $this->height);
$bg = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $bg);
imagecopy($img, $this->image, 0, 0, 0, 0, $this->width, $this->height);
$this->image = $img;
}
if ($this->crop) {
// Do as crop, take only part of image
$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']);
//imgcopy
imagecopyresampled($img, $this->image, 0, 0, $this->crop['start_x'], $this->crop['start_y'], $this->crop['width'], $this->crop['height'], $this->crop['width'], $this->crop['height']);
$this->image = $img;
$this->width = $this->crop['width'];
@ -2016,6 +2011,9 @@ class CImage
$details['aspectRatio'] = round($this->width / $this->height, 3);
$details['size'] = filesize($file);
$this->load($file);
$details['colors'] = $this->colorsTotal($this->image);
$options = null;
if (defined("JSON_PRETTY_PRINT") && defined("JSON_UNESCAPED_SLASHES")) {
$options = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES;

View File

@ -280,6 +280,7 @@ Revision history
v0.6.x (latest)
* Correcting internal details for save-as and response json which indicated wrong colors. Fix #62.
* Fixed fill-to-fit that failed when using aspect-ratio. Fix #52.
* JSON returns correct values for resulting image. Fix #58.
* Corrected behaviour for skip-original. Fix #60.

BIN
webroot/img/car.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

View File

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

View File

@ -0,0 +1,38 @@
<?php
// Include config for all testcases
include __DIR__ . "/config.php";
// The title of the test case
$title = "Testing option save-as - save image to another format";
// Provide a short description of the testcase.
$description = "";
// Use these images in the test
$images = array(
'car.png',
'ball24.png',
'wider.jpg',
);
// For each image, apply these testcases
$nc = "&nc"; //null; //&nc';
$testcase = array(
$nc . '&w=300&sa=jpg',
$nc . '&w=300&sa=png',
$nc . '&w=300&sa=gif',
$nc . '&w=300&sa=png&palette',
);
// Apply testcases and present results
include __DIR__ . "/template.php";