mirror of
https://github.com/mosbth/cimage.git
synced 2025-08-07 00:26:33 +02:00
* Added option to decide if resample or resize when copying images internally. &no-resample
makes resize, instead of resample as is default.
* Verbose now correctly states if transparent color is detected. * Removed that source png-files, containing less than 255 colors, is always saved as palette images since this migth depend on processing of the image.
This commit is contained in:
84
CImage.php
84
CImage.php
@@ -382,12 +382,21 @@ class CImage
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* output to ascii can take som options as an array.
|
* Output to ascii can take som options as an array.
|
||||||
*/
|
*/
|
||||||
private $asciiOptions = array();
|
private $asciiOptions = array();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Image copy strategy, defaults to RESAMPLE.
|
||||||
|
*/
|
||||||
|
const RESIZE = 1;
|
||||||
|
const RESAMPLE = 2;
|
||||||
|
private $copyStrategy = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Properties, the class is mutable and the method setOptions()
|
* Properties, the class is mutable and the method setOptions()
|
||||||
* decides (partly) what properties are created.
|
* decides (partly) what properties are created.
|
||||||
@@ -1302,6 +1311,11 @@ class CImage
|
|||||||
$saveAs = $this->normalizeFileExtension();
|
$saveAs = $this->normalizeFileExtension();
|
||||||
$saveAs = $saveAs ? "_$saveAs" : null;
|
$saveAs = $saveAs ? "_$saveAs" : null;
|
||||||
|
|
||||||
|
$copyStrat = null;
|
||||||
|
if ($this->copyStrategy === self::RESIZE) {
|
||||||
|
$copyStrat = "_rs";
|
||||||
|
}
|
||||||
|
|
||||||
$width = $this->newWidth;
|
$width = $this->newWidth;
|
||||||
$height = $this->newHeight;
|
$height = $this->newHeight;
|
||||||
|
|
||||||
@@ -1359,7 +1373,7 @@ class CImage
|
|||||||
. $quality . $filters . $sharpen . $emboss . $blur . $palette
|
. $quality . $filters . $sharpen . $emboss . $blur . $palette
|
||||||
. $optimize . $compress
|
. $optimize . $compress
|
||||||
. $scale . $rotateBefore . $rotateAfter . $autoRotate . $bgColor
|
. $scale . $rotateBefore . $rotateAfter . $autoRotate . $bgColor
|
||||||
. $convolve . $saveAs;
|
. $convolve . $copyStrat . $saveAs;
|
||||||
|
|
||||||
return $this->setTarget($file, $base);
|
return $this->setTarget($file, $base);
|
||||||
}
|
}
|
||||||
@@ -1428,21 +1442,23 @@ class CImage
|
|||||||
$type = $this->getPngType();
|
$type = $this->getPngType();
|
||||||
$hasFewColors = imagecolorstotal($this->image);
|
$hasFewColors = imagecolorstotal($this->image);
|
||||||
|
|
||||||
|
/* Removed v0.7.7
|
||||||
if ($type == self::PNG_RGB_PALETTE || ($hasFewColors > 0 && $hasFewColors <= 256)) {
|
if ($type == self::PNG_RGB_PALETTE || ($hasFewColors > 0 && $hasFewColors <= 256)) {
|
||||||
if ($this->verbose) {
|
if ($this->verbose) {
|
||||||
$this->log("Handle this image as a palette image.");
|
$this->log("Handle this image as a palette image.");
|
||||||
}
|
}
|
||||||
$this->palette = true;
|
$this->palette = true;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->verbose) {
|
if ($this->verbose) {
|
||||||
$this->log("Image successfully loaded from file.");
|
$this->log("### Image successfully loaded from file.");
|
||||||
$this->log(" imageistruecolor() : " . (imageistruecolor($this->image) ? 'true' : 'false'));
|
$this->log(" imageistruecolor() : " . (imageistruecolor($this->image) ? 'true' : 'false'));
|
||||||
$this->log(" imagecolorstotal() : " . imagecolorstotal($this->image));
|
$this->log(" imagecolorstotal() : " . imagecolorstotal($this->image));
|
||||||
$this->log(" Number of colors in image = " . $this->colorsTotal($this->image));
|
$this->log(" Number of colors in image = " . $this->colorsTotal($this->image));
|
||||||
$index = imagecolortransparent($this->image);
|
$index = imagecolortransparent($this->image);
|
||||||
$this->log(" Detected transparent color = " . ($index > 0 ? implode(", ", imagecolorsforindex($this->image, $index)) : "NONE") . " at index = $index");
|
$this->log(" Detected transparent color = " . ($index >= 0 ? implode(", ", imagecolorsforindex($this->image, $index)) : "NONE") . " at index = $index");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@@ -1489,18 +1505,24 @@ class CImage
|
|||||||
$pngType = $this->getPngType($filename);
|
$pngType = $this->getPngType($filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$index = imagecolortransparent($this->image);
|
||||||
|
$transparent = null;
|
||||||
|
if ($index != -1) {
|
||||||
|
$transparent = " (transparent)";
|
||||||
|
}
|
||||||
|
|
||||||
switch ($pngType) {
|
switch ($pngType) {
|
||||||
|
|
||||||
case self::PNG_GREYSCALE:
|
case self::PNG_GREYSCALE:
|
||||||
$text = "PNG is type 0, Greyscale.";
|
$text = "PNG is type 0, Greyscale$transparent";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case self::PNG_RGB:
|
case self::PNG_RGB:
|
||||||
$text = "PNG is type 2, RGB";
|
$text = "PNG is type 2, RGB$transparent";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case self::PNG_RGB_PALETTE:
|
case self::PNG_RGB_PALETTE:
|
||||||
$text = "PNG is type 3, RGB with palette";
|
$text = "PNG is type 3, RGB with palette$transparent";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case self::PNG_GREYSCALE_ALPHA:
|
case self::PNG_GREYSCALE_ALPHA:
|
||||||
@@ -1556,7 +1578,7 @@ class CImage
|
|||||||
*/
|
*/
|
||||||
public function preResize()
|
public function preResize()
|
||||||
{
|
{
|
||||||
$this->log("Pre-process before resizing");
|
$this->log("### Pre-process before resizing");
|
||||||
|
|
||||||
// Rotate image
|
// Rotate image
|
||||||
if ($this->rotateBefore) {
|
if ($this->rotateBefore) {
|
||||||
@@ -1589,6 +1611,39 @@ class CImage
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resize or resample the image while resizing.
|
||||||
|
*
|
||||||
|
* @param int $strategy as CImage::RESIZE or CImage::RESAMPLE
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setCopyResizeStrategy($strategy)
|
||||||
|
{
|
||||||
|
$this->copyStrategy = $strategy;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resize and or crop the image.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function imageCopyResampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
|
||||||
|
{
|
||||||
|
if($this->copyStrategy == self::RESIZE) {
|
||||||
|
$this->log("Copy by resize");
|
||||||
|
imagecopyresized($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
|
||||||
|
} else {
|
||||||
|
$this->log("Copy by resample");
|
||||||
|
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resize and or crop the image.
|
* Resize and or crop the image.
|
||||||
*
|
*
|
||||||
@@ -1597,7 +1652,7 @@ class CImage
|
|||||||
public function resize()
|
public function resize()
|
||||||
{
|
{
|
||||||
|
|
||||||
$this->log("Starting to Resize()");
|
$this->log("### Starting to Resize()");
|
||||||
$this->log("Upscale = '$this->upscale'");
|
$this->log("Upscale = '$this->upscale'");
|
||||||
|
|
||||||
// Only use a specified area of the image, $this->offset is defining the area to use
|
// Only use a specified area of the image, $this->offset is defining the area to use
|
||||||
@@ -1657,7 +1712,7 @@ class CImage
|
|||||||
$cropY = round(($this->cropHeight/2) - ($this->newHeight/2));
|
$cropY = round(($this->cropHeight/2) - ($this->newHeight/2));
|
||||||
$imgPreCrop = $this->CreateImageKeepTransparency($this->cropWidth, $this->cropHeight);
|
$imgPreCrop = $this->CreateImageKeepTransparency($this->cropWidth, $this->cropHeight);
|
||||||
$imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight);
|
$imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight);
|
||||||
imagecopyresampled($imgPreCrop, $this->image, 0, 0, 0, 0, $this->cropWidth, $this->cropHeight, $this->width, $this->height);
|
$this->imageCopyResampled($imgPreCrop, $this->image, 0, 0, 0, 0, $this->cropWidth, $this->cropHeight, $this->width, $this->height);
|
||||||
imagecopy($imageResized, $imgPreCrop, 0, 0, $cropX, $cropY, $this->newWidth, $this->newHeight);
|
imagecopy($imageResized, $imgPreCrop, 0, 0, $cropX, $cropY, $this->newWidth, $this->newHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1696,7 +1751,7 @@ class CImage
|
|||||||
} else {
|
} else {
|
||||||
$imgPreFill = $this->CreateImageKeepTransparency($this->fillWidth, $this->fillHeight);
|
$imgPreFill = $this->CreateImageKeepTransparency($this->fillWidth, $this->fillHeight);
|
||||||
$imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight);
|
$imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight);
|
||||||
imagecopyresampled($imgPreFill, $this->image, 0, 0, 0, 0, $this->fillWidth, $this->fillHeight, $this->width, $this->height);
|
$this->imageCopyResampled($imgPreFill, $this->image, 0, 0, 0, 0, $this->fillWidth, $this->fillHeight, $this->width, $this->height);
|
||||||
imagecopy($imageResized, $imgPreFill, $posX, $posY, 0, 0, $this->fillWidth, $this->fillHeight);
|
imagecopy($imageResized, $imgPreFill, $posX, $posY, 0, 0, $this->fillWidth, $this->fillHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1742,7 +1797,7 @@ class CImage
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight);
|
$imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight);
|
||||||
imagecopyresampled($imageResized, $this->image, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height);
|
$this->imageCopyResampled($imageResized, $this->image, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height);
|
||||||
$this->image = $imageResized;
|
$this->image = $imageResized;
|
||||||
$this->width = $this->newWidth;
|
$this->width = $this->newWidth;
|
||||||
$this->height = $this->newHeight;
|
$this->height = $this->newHeight;
|
||||||
@@ -1761,7 +1816,7 @@ class CImage
|
|||||||
*/
|
*/
|
||||||
public function postResize()
|
public function postResize()
|
||||||
{
|
{
|
||||||
$this->log("Post-process after resizing");
|
$this->log("### Post-process after resizing");
|
||||||
|
|
||||||
// Rotate image
|
// Rotate image
|
||||||
if ($this->rotateAfter) {
|
if ($this->rotateAfter) {
|
||||||
@@ -2484,8 +2539,7 @@ class CImage
|
|||||||
$details['memoryLimit'] = ini_get('memory_limit');
|
$details['memoryLimit'] = ini_get('memory_limit');
|
||||||
|
|
||||||
if ($details['mimeType'] == 'image/png') {
|
if ($details['mimeType'] == 'image/png') {
|
||||||
$details['pngTypeSource'] = $this->getPngTypeAsString();
|
$details['pngType'] = $this->getPngTypeAsString(null, $file);
|
||||||
$details['pngTypeCache'] = $this->getPngTypeAsString(null, $this->cacheFileName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$options = null;
|
$options = null;
|
||||||
|
@@ -8,6 +8,11 @@ Revision history
|
|||||||
v0.7.6* (2015-10-20)
|
v0.7.6* (2015-10-20)
|
||||||
-------------------------------------
|
-------------------------------------
|
||||||
|
|
||||||
|
* Added option to decide if resample or resize when copying images internally. `&no-resample` makes resize, instead of resample as is default.
|
||||||
|
* Verbose now correctly states if transparent color is detected.
|
||||||
|
* Compare-tool now supports 6 images.
|
||||||
|
* Added option for dark background in the compare-tool.
|
||||||
|
* Removed that source png-files, containing less than 255 colors, is always saved as palette images since this migth depend on processing of the image.
|
||||||
* Adding save-as as part of the generated cache filename, #121.
|
* Adding save-as as part of the generated cache filename, #121.
|
||||||
* Add extra fields to json-response, #114.
|
* Add extra fields to json-response, #114.
|
||||||
* Add header for Content-Length, #111.
|
* Add header for Content-Length, #111.
|
||||||
|
@@ -558,6 +558,19 @@ verbose("bgColor = $bgColor");
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Do or do not resample image when resizing.
|
||||||
|
*/
|
||||||
|
$resizeStrategy = getDefined(array('no-resample'), true, false);
|
||||||
|
|
||||||
|
if ($resizeStrategy) {
|
||||||
|
$img->setCopyResizeStrategy($img::RESIZE);
|
||||||
|
verbose("Setting = Resize instead of resample");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* fill-to-fit, ff - affecting the resulting image width, height and resize options
|
* fill-to-fit, ff - affecting the resulting image width, height and resize options
|
||||||
*/
|
*/
|
||||||
@@ -924,7 +937,7 @@ if ($verbose) {
|
|||||||
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 = "filename: " + data.filename + "\\nmime type: " + data.mimeType + "\\ncolors: " + data.colors + "\\nsize: " + data.size + "\\nwidth: " + data.width + "\\nheigh: " + data.height + "\\naspect-ratio: " + data.aspectRatio;
|
element.innerHTML = "filename: " + data.filename + "\\nmime type: " + data.mimeType + "\\ncolors: " + data.colors + "\\nsize: " + data.size + "\\nwidth: " + data.width + "\\nheigh: " + data.height + "\\naspect-ratio: " + data.aspectRatio + "\\npng-type: " + data.pngType;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
Reference in New Issue
Block a user