Comments (11)
My images are opened as Gd/Image object (ImageInterface).
So I guess I could either convert it into RGB, or find a way to avoid Gd/Image..
from imagine.
You could use a different driver, both Gmagick as well as Imagick should support CMYK.
Also, how did you open the image? I was not able to reproduce your issue. Can you post a stack trace that shows where the mentioned NotSupportedException
was thrown?
from imagine.
I have developed a function where I check if the image is CMYK palette:
When you load with GD an image which uses a CMYK palette, GD load it as RGB palette. Which makes the image using wrong colours.
$image = $this->imagine->open($path);
$image->usePalette(is_cmyk($path) ? new CMYK() : new RGB());
from imagine.
OK, so opening the image works, but the call to the usePalette()
method breaks?
This is the expected behavior of GD I think, because it doesn’t support color profile management.
from imagine.
I get it :)
But GD still opens a RGB palette by default although it is CMYK. This is actually very confusion.
Shouldn't GD just trigger an exception when trying to open an invalid image (with CMYK palette) or try to convert to RGB before ? I actually don't mind if GD says that my image is RGB as long as it transformed properly prior final opening
from imagine.
Hello, I just jumped back into this issue and found a partial solution to my issue. The issue was due to the use of CMYK custom profiles. It seems that the use of ImageInterface::useProfile (or ::usePalette including a profile) is not working. I have done some tests and the resulting picture returns only a generic CMYK profile.
Here is a method to bypass the issue. However this is only a partial answer because this applies after saving the image.
When I am converting my picture into a Webp format or any other compressed format using profiles, I fear that webp is wrongly compress and therefore changing the profile afterward will result in a wrongly colored picture.
if(is_cmyk($input))
write_cmyk_profiles($output, cmyk_profiles($input));
Here is a sample of some homemade functions used to get proper ICC profile. This is using Imagick only, and not imagine/imagine package.
/**
* Check if a JPEG image file uses the CMYK colour space.
* @param string $path The path to the file.
* return bool
*/
function is_cmyk(string $path)
{
if(!$path || !file_exists($path))
return false;
$imagesize = @getimagesize($path);
if($imagesize === false) return false;
return array_key_exists('mime', $imagesize) && 'image/jpeg' == $imagesize['mime'] &&
array_key_exists('channels', $imagesize) && 4 == $imagesize['channels'];
}
function is_rgb(string $path)
{
if(!$path || !file_exists($path))
return false;
$imagesize = @getimagesize($path);
return array_key_exists('channels', $imagesize) && 3 == $imagesize['channels'];
}
function cmyk_profile(string $path, string $name)
{
$image = new \Imagick($path); // load image
return $image->getImageProfiles($name, true)[$name] ?? null; // get profiles
}
function write_cmyk_profiles(string $path, array $profiles)
{
$image = new \Imagick($path); // load image
foreach($profiles as $name => $data)
$image->profileImage($name, $data);
$image->setImageColorSpace(is_cmyk($path) ? Imagick::COLORSPACE_CMYK : Imagick::COLORSPACE_RGB);
$image->writeImage($path);
}
function write_cmyk_profile(string $path, string $name, $data)
{
$image = new \Imagick($path); // load image
$image->profileImage($name, $data);
$image->setImageColorSpace(is_cmyk($path) ? Imagick::COLORSPACE_CMYK : Imagick::COLORSPACE_RGB);
$image->writeImage($path);
}
function cmyk_profiles(string $path)
{
$image = new \Imagick($path); // load image
return $image->getImageProfiles('*', true); // get profiles
}
function cmyk_icc_profile(string $path)
{
$image = new \Imagick($path); // load image
return $image->getImageProfiles('icc', true)['icc'] ?? null; // get profiles
}
function cmyk2rgb(string $path) // Not working... color are distorted if no icc profile
{
if(is_rgb($path)) return $path;
if(!class_exists("Imagick"))
throw new Exception(__FUNCTION__."(): Imagick driver not found.");
$image = new \Imagick($path);
$image->transformImageColorspace(\Imagick::COLORSPACE_SRGB);
$image->writeImage($path);
$image->destroy();
}
Any idea or support would be very welcome!
from imagine.
Any idea or support would be very welcome!
I’m not sure how I can help.
GD does not support color profiles, so using Imagine\Gd
just strips the profile and fully ignores it.
There is a ticket about this issue in GD itself: libgd/libgd#136
But if you can use Imagick
on your server, use Imagine\Imagick
that fully supports working with color profiles and just don’t use Imagine\Gd
at all.
from imagine.
Please have a look closely this is not using GD library. This is pure Imagick solution, I had to bypass Imagine\Imagick.
from imagine.
Using the following code should work with all images and convert them to an sRGB image:
$imagine = new \Imagine\Imagick\Imagine();
$image = $imagine->open($path);
$image->usePalette(new \Imagine\Image\Palette\RGB());
$image->strip();
$image->save($path);
from imagine.
This is what I was doing, but I have issue with CMYK pictures including custom profile. Only one profile is available, but the metadata contains multiple formats: icc,8bim..
^ ImagickException {#6464
#message: "Unable to set image alpha channel"
#code: 1
#file: "./vendor/imagine/imagine/src/Imagick/Image.php"
#line: 988
trace: {
./vendor/imagine/imagine/src/Imagick/Image.php:988 { …}
./vendor/imagine/imagine/src/Imagick/Image.php:700 { …}
[...]
}
}
}
from imagine.
I have investigated this issue I had in the past again.
This is actually not related to image alpha channel or palette.., but an imagick ressource exhausted.
These errors seem to not be cacheable. Keeping CMYK palette prevent high consumption, so that's also why my solution works.
Thank you for your reply, increasing resource disk (on the fly without reboot) in /etc/ImageMagick-6/policy.xml from 1GiB to 4GiB fixed my issue.
<policy domain="resource" name="disk" value="4GiB"/>
from imagine.
Related Issues (20)
- Split getSupportedFormats into getSupportedInputFormats and getSupportedOutputFormats HOT 2
- Release a PHP 8.1 version HOT 3
- Segmentation fault with GD on 1.3.1 HOT 24
- curl error: 1409442E:SSL routines:ssl3_read_bytes:tlsv1 alert protocol version HOT 9
- Unable to open remote jpg image HOT 1
- Issue reading GDImage object using \Imagine\Gd\Imagine read() method HOT 3
- Is there an inverse to getGdResource() HOT 7
- Gd\Drawer -> ellipse
- Why does Point class reject negative values...? HOT 3
- Hello, is this image library still being updated? I want him to be a replacement for intervention, because intervention is no longer updated. HOT 1
- `getImageAlphaChannel` returns incorrect value HOT 3
- Incorrect formula to calculate the linnear position. HOT 1
- Update min required PHP version and fix deprecation warnings HOT 3
- Error when file is coming from temp directory: Creating an image in "C:\Users\...\Temp\phpA6B2.tmp" format is not supported, please use one of the following formats: HOT 7
- Feature request: Add support for "setOption('jpeg:size', '800x800');"
- vulnerable libraries (jQuery) HOT 4
- Memory limit not throw new RuntimeException
- Saving Lossless Webp? HOT 5
- ExifMetadataReader class fails due to unnecessary check for 'allow_url_fopen()' HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from imagine.