diff --git a/CImage.php b/CImage.php index c16385a..c4a0f63 100644 --- a/CImage.php +++ b/CImage.php @@ -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. * diff --git a/REVISION.md b/REVISION.md index 06c134e..75aab55 100644 --- a/REVISION.md +++ b/REVISION.md @@ -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. diff --git a/test/CImageSRGBTest.php b/test/CImageSRGBTest.php new file mode 100644 index 0000000..ba19f04 --- /dev/null +++ b/test/CImageSRGBTest.php @@ -0,0 +1,70 @@ +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); + } +} diff --git a/webroot/check_system.php b/webroot/check_system.php index fb6ea28..2ae22b9 100644 --- a/webroot/check_system.php +++ b/webroot/check_system.php @@ -10,6 +10,9 @@ echo "Extension exif is $no loaded.
"; $no = extension_loaded('curl') ? null : 'NOT'; echo "Extension curl is $no loaded.
"; +$no = extension_loaded('imagick') ? null : 'NOT'; +echo "Extension imagick is $no loaded.
"; + $no = extension_loaded('gd') ? null : 'NOT'; echo "Extension gd is $no loaded.
"; diff --git a/webroot/img.php b/webroot/img.php index d0b9be2..f2f6fbf 100644 --- a/webroot/img.php +++ b/webroot/img.php @@ -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.
"; + $no = extension_loaded('imagick') ? null : 'NOT'; + $text .= "Extension imagick is $no loaded.
"; + $no = extension_loaded('gd') ? null : 'NOT'; $text .= "Extension gd is $no loaded.
"; diff --git a/webroot/img/car_srgb.png b/webroot/img/car_srgb.png new file mode 100644 index 0000000..b2af9e5 Binary files /dev/null and b/webroot/img/car_srgb.png differ