Imagick results in negative image while using imagick::profileImage - php

I m using PHP's Imagick extension to manipulate images. Following are my code
try{
$sourceimg=dirname(__FILE__)."\\source.jpg";
$destinationimg=dirname(__FILE__)."\\source_cmyk.tiff";
$im=new Imagick();
$im->setResolution(300,300);
$im->readImage($sourceimg);
$im->setImageColorSpace(imagick::COLORSPACE_CMYK);
$im->stripImage();
$cmykprofile=#file_get_contents("C:\\USWebUncoated.icc");
$im->profileImage("icm",$cmykprofile);
$im->setImageDepth(8);
$im->setImageUnits(1); //0=undefined, 1=pixelsperInch, 2=PixelsPerCentimeter
$im->setResolution(300,300); //set output resolution to 300 dpi
$im->setImageCompressionQuality(100);
$im->writeImage($destinationimg);
}catch(ImagickException $e){
echo $e->getMessage();
}
The problem i m having is the code negates the original Image. This issue occurs only if i use $im->profileImage. What will be the cause for this? And how to solve this issue? The profile i m using is cmyk color profile that i downloaded from http://www.adobe.com/support/downloads/detail.jsp?ftpID=3680
Any help will be highly appreciated.
Thanks

If you're using images with specialized color profiles or non-RGB images (e.g. CMYK JPEG), you should convert them to sRGB first.
With striping profile you strip ICC color profile as well. Most browsers display your stripped image with gamma 2.2 (default), which differs from CMYK.
Solution of your problem is in user contributed note here: http://www.php.net/manual/en/imagick.setimagecolorspace.php

Related

converting rgb image to cmyk using Imagick

I have image that i want to convert from rgb to cmyk using color profile.
Here is the image that i am using
And the peace of code that i am using to convert into cmyk.
$IMagick = new IMagick();
$IMagick->clear();
$IMagick->readImage($image);
$IMagick->transformImageColorspace(12);
$icc_cmyk = file_get_contents("USWebUncoated.icc");
$IMagick->profileImage('icc', $icc_cmyk);
$IMagick->writeImage ($image2);
And here is the output image that i got.
I then checked color space and yes it is cmyk, but you can mark that green color is change though. Am i doing anything wrong here?
It is known that converting RGB to CMYK can change colors. Also browsers hae challenges rendering CMYK images properly. There is nothing you are doing wrong.

I got a low quality image when converting sRGB to CMYK in PHP using Imagick

I am getting a low quality image while trying to convert from sRGB to CMYK in PHP using Imagick.
Original Image
Converted Image
And here is my PHP code:
$i = new \Imagick();
$i->clear();
$i->readimage("original_image.jpg");
$icc_cmyk = file_get_contents('/tmp/USWebCoatedSWOP.icc');
$i->profileimage('icc', $icc_cmyk);
unset($icc_cmyk);
$i->transformImageColorspace(12);
$i->writeImage('converted_image.php');
I couldn't figure out why this is happen.
Any ideas?
There were several bugs related to CMYK in the older versions of ImageMagick.
According to the changelog there was something fixed for 6.8.2-4 which might be related to what you are seeing.
Upgrading to the latest version of ImageMagick, when you see bad an unexpected output from image conversion is usually worth trying to see if that eliminates the problem.

PHP Imagick (ImageMagick) RGB > CMYK with Flat Black

I'm using PHP Imagick to convert PNG images generated in PhantomJS to TIF CMYK,
for print purposes I need a flat Black (cmyk - 0,0,0,100) - the conversion generates blacks like (cmyk - 58,49,44,89).
I'm converting the images using color profile (section of my code below) -> the code is based on Convert image from RGB to CMYK with Imagick
is it possible to force a flat black with Imagick ? do you know any other tools that might help ?
thanks,
if ($has_icc_profile === false) {
$icc_rgb = file_get_contents( '/srgb_profiles' . '/sRGB.icc');
$image->profileImage('icc', $icc_rgb);
unset($icc_rgb);
}
// then we add an CMYK profile
$icc_cmyk = file_get_contents( '/cmyk_profiles'.'/JapanColor2002Newspaper.icc');
$image->profileImage('icc', $icc_cmyk);
UPDATE :
after checking online I think I'm looking for a UCR en.wikipedia.org/wiki/Under_color_removal method for ImageMagick - I found that convert old versions supported under color removal
-undercolor <undercolor factor>x<black-generation factor>
control undercolor removal and black generation on CMYK images.
This option enables you to perform undercolor removal and black generation on CMYK images-- images to be printed on a four-color printing system. You can con- trol how much cyan, magenta, and yellow to remove from your image and how much black to add to it. The standard undercolor removal is 1.0x1.0. You'll frequently get better results, though, if the percentage of black you add to your image is slightly higher than the percentage of C, M, and Y you remove from it. For example you might try 0.5x0.7. (http://www.chemie.fu-berlin.de/chemnet/use/suppl/imagemagick/www/convert.html) -
apparently the option is not supported anymore, I'm interested if anyone knows if UCR is the solution I'm looking for and if anyone knows if it's supported or if I'm supposed to use a different method to get the same result.
If you use ImageMagick's convert at the command line like this to generate a grayscale ramp, 1 pixel wide and 256 pixels tall, going from white to black and convert it to CMYK colorspace and then show it as text, you get what you want:
convert -size 1x256 'gradient:rgb(255,255,255)-rgb(0,0,0)' -colorspace cmyk txt:
# ImageMagick pixel enumeration: 1,256,65535,cmyk
0,0: (0%,0%,0%,0%) #0000000000000000 cmyk(0,0,0,0)
0,1: (0%,0%,0%,0.392157%) #0000000000000101 cmyk(0,0,0,1)
0,2: (0%,0%,0%,0.784314%) #0000000000000202 cmyk(0,0,0,2)
0,3: (0%,0%,0%,1.17647%) #0000000000000303 cmyk(0,0,0,3)
0,4: (0%,0%,0%,1.56863%) #0000000000000404 cmyk(0,0,0,4)
0,5: (0%,0%,0%,1.96078%) #0000000000000505 cmyk(0,0,0,5)
0,6: (0%,0%,0%,2.35294%) #0000000000000606 cmyk(0,0,0,6)
0,7: (0%,0%,0%,2.7451%) #0000000000000707 cmyk(0,0,0,7)
0,8: (0%,0%,0%,3.13725%) #0000000000000808 cmyk(0,0,0,8)
0,9: (0%,0%,0%,3.52941%) #0000000000000909 cmyk(0,0,0,9)
0,10: (0%,0%,0%,3.92157%) #0000000000000A0A cmyk(0,0,0,10)
...
...
0,249: (0%,0%,0%,97.6471%) #000000000000F9F9 cmyk(0,0,0,249)
0,250: (0%,0%,0%,98.0392%) #000000000000FAFA cmyk(0,0,0,250)
0,251: (0%,0%,0%,98.4314%) #000000000000FBFB cmyk(0,0,0,251)
0,252: (0%,0%,0%,98.8235%) #000000000000FCFC cmyk(0,0,0,252)
0,253: (0%,0%,0%,99.2157%) #000000000000FDFD cmyk(0,0,0,253)
0,254: (0%,0%,0%,99.6078%) #000000000000FEFE cmyk(0,0,0,254)
0,255: (0%,0%,0%,100%) #000000000000FFFF cmyk(0,0,0,255)
You must be doing something different - maybe this will help you work it out. I am guessing it is your ICC profiles but you can experiment with the above command.
If you just want to experiment with spot values, you can just have IM translate a single pixel like this:
convert -size 1x1 xc:#000000 -colorspace cmyk txt:
# ImageMagick pixel enumeration: 1,1,65535,cmyk
0,0: (0%,0%,0%,100%) #000000000000FFFF cmyk(0,0,0,255)
or maybe more simply like this:
convert -size 1x1 xc:#000000 -depth 8 -colorspace cmyk txt:
# ImageMagick pixel enumeration: 1,1,255,cmyk
0,0: (0,0,0,255) #000000FF cmyk(0,0,0,255)
Note the following though:
You must put profiles between input image and output image names on the command line.
If your image has no embedded profile, the first profile you give is applied to the input image and the second to the output image. If your input image does have a profile, the first profile you give is applied to the output image.

Convert vector to raster in php

I am trying to convert a vector image formats .emf,.wmf to a high resolution sharp and crisp raster image .gif,jpg. (Usually this could be easily done in Illustrator). But i am unable to do this in PHP. I am trying the following code but the results are either blurry or distorted or even totally black.
<?php
$image = new Imagick("1.emf");
$image->resizeImage(1500,0,Imagick::FILTER_LANCZOS,1);
$image->setImageFormat('gif');
$image->setresolution(900, 900);
$image->writeImage("2.gif");
?>
We just needed to set the resolution before loading the image.
$image = new Imagick();
$image->setresolution(300, 300);
$image->readimage($filename);
$image->resizeImage(1500,0,Imagick::FILTER_LANCZOS,1);
$image->setImageFormat('jpg');
$image->writeImage("1.jpg");
This code will convert a vector to a sharp and crisp raster image. It works for all vector formats (svg, ai, emf, wmf, etc). If the jpg result is unexpectedly a black image, you need to change the image transparency to white (check this link). Another way to get around with transparency problem is by getting your PHP updated to 5.5 and by installing Imagick for that version. By doing so, it will not cause any problems with transparent images and the above code will just work fine.
For testing purposes you could change jpg to png because it supports transparency.

PHP IMagick RGB to CMYK inverts?

I'm trying to convert a RGB .gif to a CMYK .gif using IMagick PHP module.
I've wrote this piece of code
$i = new Imagick('mosaique.gif');
$i->setImageColorspace(Imagick::COLORSPACE_CMYK);
$i->setImageFormat('gif');
$i->writeImage('mosaique-cmyk.gif');
But the resultant "mosaique-cmyk.gif" still a RGB... but with inverted colors (O_O)
What am I doing wrong?
EDIT:
I've tried with a .jpg and the image is converted to CMYK but it stills in negative.
EDIT 2:
I've tried to run my script making a .pdf on another server and it works fine.
Are there any known bug in IMagick?
Are there some options to set in the php5 library?
The version that returns me the inverted image is newer than the one that works correctly
WRONG RESULT
PHP 5.3.3
IMagick 3.0.0RC1
ImageMagick 6.6.2
CORRECT RESULT
PHP 5.2.10
IMagick 2.1.1
ImageMagick 6.5.1
The error in fact it's a bug ;)
I reported it, some other has confirmed my fear and now it's assigned to a developer for a fix: http://pecl.php.net/bugs/bug.php?id=22184
At this moment the solution it's to use a different version of the libraries.
GIF is 256-color format aka "indexed." I do not think one can save a gif as cmyk. Each of the 256 colors is an RGB value, but it is not capable of storing the full RGB gamut.
Try this:
$im->stripImage();
$icc_cmyk_profile_path='image_functions/cmyk_icc_profiles/USWebUncoated.icc';
//[http://www.mattbeals.com/icc/][1]
$icc_cmyk = file_get_contents($icc_cmyk_profile_path);
$im->profileImage('icc', $icc_cmyk);
unset($icc_cmyk);
$colorspace=$im->getImageColorspace();
if ($colorspace==12) {
echo "CMYK";
}
$im->stripImage();
$im->writeImage($destination);
$im->clear();
$im->destroy();
see here http://imagemagick.org/Usage/formats/#color_profile
convert cmyk_image.jpg -colorspace
rgb rgb_image.jpg

Categories