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

Added conversion to sRGB using option , partly working, #120

This commit is contained in:
Mikael Roos
2015-10-23 14:19:46 +02:00
parent a62425e759
commit aedce7021f
6 changed files with 184 additions and 5 deletions

View File

@@ -1323,8 +1323,8 @@ class CImage
$copyStrat = "_rs";
}
$width = $this->newWidth;
$height = $this->newHeight;
$width = $this->newWidth ? '_' . $this->newWidth : null;
$height = $this->newHeight ? '_' . $this->newHeight : null;
$offset = isset($this->offset)
? '_o' . $this->offset['top'] . '-' . $this->offset['right'] . '-' . $this->offset['bottom'] . '-' . $this->offset['left']
@@ -1374,8 +1374,8 @@ class CImage
$subdir .= '_';
}
$file = $subdir . $filename . '_' . $width . '_'
. $height . $offset . $crop . $cropToFit . $fillToFit
$file = $subdir . $filename . $width . $height
. $offset . $crop . $cropToFit . $fillToFit
. $crop_x . $crop_y . $upscale
. $quality . $filters . $sharpen . $emboss . $blur . $palette
. $optimize . $compress
@@ -2397,6 +2397,75 @@ class CImage
/**
* Convert image from one colorpsace/color profile to sRGB without
* color profile.
*
* @param string $src of image.
* @param string $dir as base directory where images are.
* @param string $cache as base directory where to store images.
* @param boolean $useCache or not, default to always use cache.
*
* @return string | boolean false if no conversion else the converted
* filename.
*/
public function convert2sRGBColorSpace($src, $dir, $cache, $useCache = true)
{
if ($this->verbose) {
$this->log("# Converting image to sRGB colorspace.");
}
if (!class_exists("Imagick")) {
$this->log(" Ignoring since Imagemagick is not installed.");
return false;
}
// Prepare
$this->setSaveFolder($cache)
->setSource($src, $dir)
->generateFilename(null, false);
// Check if the cached version is accurate.
if ($useCache && is_readable($this->cacheFileName)) {
$fileTime = filemtime($this->pathToImage);
$cacheTime = filemtime($this->cacheFileName);
if ($fileTime <= $cacheTime) {
$this->log(" Using cached version: " . $this->cacheFileName);
return $this->cacheFileName;
}
}
// Only covert if cachedir is writable
if (is_writable($this->saveFolder)) {
// Load file and check if conversion is needed
$image = new Imagick($this->pathToImage);
$colorspace = $image->getImageColorspace();
$this->log(" Current colorspace: " . $colorspace);
$profiles = $image->getImageProfiles('*', false);
$hasICCProfile = (array_search('icc', $profiles) !== false);
$this->log(" Has ICC color profile: " . ($hasICCProfile ? "YES" : "NO"));
if ($colorspace != Imagick::COLORSPACE_SRGB || $hasICCProfile) {
$this->log(" Converting to sRGB.");
/*
$icc_rgb = file_get_contents('/path/to/icc/SomeRGBProfile.icc');
$image->profileImage('icc', $icc_rgb);
*/
$image->transformImageColorspace(Imagick::COLORSPACE_SRGB);
$image->writeImage($this->cacheFileName);
return $this->cacheFileName;
}
}
return false;
}
/**
* Create a hard link, as an alias, to the cached file.
*

View File

@@ -8,6 +8,7 @@ Revision history
v0.7.7* (2015-10-21)
-------------------------------------
* Added conversion to sRGB using option `?srgb`, partly working. #120.
* Change path in `webroot/htaccess` to make it work in current environment.

70
test/CImageSRGBTest.php Normal file
View File

@@ -0,0 +1,70 @@
<?php
/**
* A testclass
*
*/
class CImageSRGBTest extends \PHPUnit_Framework_TestCase
{
private $srgbDir = "srgb";
private $cache;
/**
* Setup before test
*
* @return void
*/
protected function setUp()
{
$this->cache = CACHE_PATH . "/" . $this->srgbDir;
if (!is_writable($this->cache)) {
mkdir($this->cache);
}
}
/**
* Test
*
* @return void
*/
public function testCreate1()
{
$img = new CImage();
$filename = $img->convert2sRGBColorSpace(
'car.png',
IMAGE_PATH,
$this->cache
);
if (class_exists("Imagick")) {
$this->assertEquals("car.png", basename($filename), "Filename not as expected on image.");
} else {
$this->assertFalse($filename, "ImageMagick not installed, silent fail");
}
}
/**
* Test
*
* @return void
*/
public function testCreate2()
{
$img = new CImage();
$filename = $img->convert2sRGBColorSpace(
'car.jpg',
IMAGE_PATH,
$this->cache
);
$this->assertFalse($filename);
}
}

View File

@@ -10,6 +10,9 @@ echo "Extension exif is $no loaded.<br>";
$no = extension_loaded('curl') ? null : 'NOT';
echo "Extension curl is $no loaded.<br>";
$no = extension_loaded('imagick') ? null : 'NOT';
echo "Extension imagick is $no loaded.<br>";
$no = extension_loaded('gd') ? null : 'NOT';
echo "Extension gd is $no loaded.<br>";

View File

@@ -8,7 +8,7 @@
*
*/
$version = "v0.7.7 (2015-10-21)";
$version = "v0.7.7* (2015-10-21)";
@@ -972,6 +972,39 @@ if ($dummyImage === true) {
/**
* Prepare a sRGB version of the image and use it as source image.
*/
$srgbDirName = "/srgb";
$srgbDir = getConfig('srgb_dir', $cachePath . $srgbDirName);
$srgb = getDefined('srgb', true, null);
if ($srgb) {
if (!is_writable($srgbDir)) {
if (is_writable($cachePath)) {
mkdir($srgbDir);
}
}
$filename = $img->convert2sRGBColorSpace(
$srcImage,
$imagePath,
$srgbDir,
$useCache
);
if ($filename) {
$srcImage = $img->getTarget();
$imagePath = null;
verbose("srgb conversion and saved to cache = $srcImage");
} else {
verbose("srgb not op");
}
}
/**
* Display status
*/
@@ -990,6 +1023,9 @@ if ($status) {
$no = extension_loaded('curl') ? null : 'NOT';
$text .= "Extension curl is $no loaded.<br>";
$no = extension_loaded('imagick') ? null : 'NOT';
$text .= "Extension imagick is $no loaded.<br>";
$no = extension_loaded('gd') ? null : 'NOT';
$text .= "Extension gd is $no loaded.<br>";

BIN
webroot/img/car_srgb.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 245 KiB