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
Related
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.
I'm attempting to try and assign a value to an image based on its 'saturation level', to see if the image is black and white or color. I'm using Imagick, and have found what seems to be the perfect code for the command line and am trying to replicate it using the PHP library.
I think I understand the concept:
Convert image to HSL.
Extract the 'g' channel (which is the S channel in HSL).
Calculate the mean of this channel.
Command line code
convert '$image_path' -colorspace HSL -channel g -separate +channel -format '%[fx:mean]' info:
My PHP code
$imagick = new Imagick($image_path);
$imagick->setColorspace(imagick::COLORSPACE_HSL);
print_r($imagick->getImageChannelMean(imagick::CHANNEL_GREEN));
Output
My PHP code isn't outputting the same sort of values as the command line code, though. For example, a grayscale image gives 0 for the command line code, but the PHP code gives [mean] => 10845.392051182 [standardDeviation] => 7367.5888849872.
Similarly, another grayscale image gives 0 vs. [mean] => 31380.528443457 [standardDeviation] => 19703.501101904.
A colorful image gives 0.565309 vs. [mean] => 33991.552881892 [standardDeviation] => 16254.018540044.
There just doesn't seem to be any kind of pattern between the different values. Am I doing something obviously wrong?
Thanks.
Just to add, I've also tried this PHP code
$imagick = new Imagick($image_path);
$imagick->setColorspace(imagick::COLORSPACE_HSL);
$imagick->separateImageChannel(imagick::CHANNEL_GREEN);
$imagick->setFormat('%[fx:mean]');
But I get an Unable to set format error when I try and set the format. I've also tried setFormat('%[fx:mean] info:'), setFormat('%[mean]'), setFormat('%mean'), etc.
Update — FIXED!
Thanks to #danack for figuring out I needed to use transformImageColorspace() and not setColorspace(). The working code is below.
$imagick = new Imagick($image_path);
$imagick->transformImageColorspace(imagick::COLORSPACE_HSL);
$saturation_channel = $imagick->getImageChannelMean(imagick::CHANNEL_GREEN);
$saturation_level = $saturation_channel['mean']/65535;
setFormat doesn't replicate the command line option -format - the one in Imagick tries to set the image format, which should be png, jpg etc. The one in the command line is setting the format for info - the closest match for which in Imagick is calling $imagick->identifyImage(true) and parsing the results of that.
Also you're just calling the wrong function - it should be transformImageColorspace not setColorSpace. If you use that you can use the statistics from getImageChannelMean.
There are other ways to test for greyness which may be more appropriate under certain circumstances. The first is to convert the a clone of the image to grayscale, and then compare it to the original image:
$imagick = new Imagick($image_path);
$imagickGrey = clone $imagick;
$imagickGrey->setimagetype(\Imagick::IMGTYPE_GRAYSCALE);
$differenceInfo = $imagick->compareimages($imagickGrey, \Imagick::METRIC_MEANABSOLUTEERROR);
if ($differenceInfo['mean'] <= 0.0000001) {
echo "Grey enough";
}
That would probably be appropriate if you image had areas of color that were also transparent - so they theoretically have color, but not visibly.
Or if you only care about whether the image is of a grayscale format:
$imageType = $imagick->getImageType();
if ($imageType === \Imagick::IMGTYPE_GRAYSCALE ||
$imageType === Imagick::IMGTYPE_GRAYSCALEMATTE) {
//This is grayscale
}
I found a command line here:
convert image.png -colorspace HSL -channel g -separate +channel -format "%[fx:mean]" info:
It prints a number between 0 and 1, where zero means grayscale.
If your images have tint. (from scanner as example) you should do auto color for them before detecting gray scale.
You should normalizeImage(imagick::CHANNEL_ALL) for all separate channels of image. separateImageChannel()
But
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
I am trying to add round corners to a jpeg file, but the problem is that after adding round corners, I am getting a black background color. Somehow I am not able to change it to any other color (white, transparent, red). It just simply shows black background where the image has rounded corners.
The code that I am using is:
<?php
$image = new Imagick('example.jpg');
$image->setBackgroundColor("red");
$image->setImageFormat("jpg");
$image->roundCorners(575,575);
$image->writeImage("rounded.jpg");
header('Content-type: image/jpeg');
echo $image;
?>
I cannot use png as the jpeg files are huge, about 5 MB, so if I used png, the file size would go up to 26 MB, even though the png adds transparent round corners.
Also the IMagick version that i am using is:
ImageMagick 6.6.2-10 2010-06-29 Q16 http://www.imagemagick.org
Also the output(image generated) will get printed so I don't know if css will work over here.
Sorry, I am trying to actually create a new jpeg file with rounded corners from an already existing jpeg file that doesn't have round corners this is actually a photograph taken from a camera, so there are multiple/too many colors so I can't use gif as well.
Also my site will only just generate the round corner image then afterwards it will get downloaded using a FTP program by the admin of the site and then using a system software will get printed, so in short my website will not be printing the image but rather just generate it
Try this:
<?php
$input = 'example.jpg';
$size = getimagesize($input);
$background = new Imagick();
$background->newImage($size[0], $size[1], new ImagickPixel('red'));
$image = new Imagick($input);
$image->setImageFormat("png");
$image->roundCorners(575,575);
$image->compositeImage($background, imagick::COMPOSITE_DSTATOP, 0, 0);
$image->writeImage("rounded.jpg");
?>
I may get downvoted, but I say let css deal with the corners and take some load off of your server :)
CSS rounded corners.
JPG doesn't have a transparent color(s) (alpha channels) in its palette.
The output image must use either PNG or GIF (or another image format that supports alpha channels).
setImageBackgroundColor is another option if you want an opaque background.
EDIT
Your comment reminds me that you could try to use the command line; shell_exec() will run a command line argument from PHP. The command in the ImageMagick API you'll need to start with is convert example.jpg, and then you can pass flags with the various parameters you want.
Since ImageMagick is already installed, it will work right away. You may need to point your system PATH to the ImageMagick directory where all of the executables are.
There's plenty of questions and forums dedicated to rounded corners with this method so I'll leave that up to you.
Here's a helpful tip though - there is a silly confusion with the convert command, since Windows also has a convert.exe that is rarely used, but will confuse your command line, so make sure you're calling the right convert. ;) To test if it's working, try convert example.jpg example.gif (which should convert your example to a gif).
To get output from your command line, finish all commands with 2>&1 which will pipe cmd output back into PHP.
I have a PHP application which needs to deal with incoming TIFF files. I have neither control nor knowledge over the colorspaces of this TIFFs and the application should store all incoming images as RGB JPEGs.
Problem is, incoming TIFF files are anything: CMYK, RGB, some sort of YCbCr wrapped in sRGB, and so on, and I need to convert them somehow to RGB JPEGs before saving.
I need some sort of a conversion function in PHP which uses IMagick extension which can get any binary TIFF data and convert it to proper RGB JPEG binary data. It needs to handle different colorspaces inside TIFF images correctly. Output format (RGB JPEG) stays the same for any input file.
The following obvious solution converts some CMYK TIFFs correctly, some CMYK TIFFs get inverted colors and YCbCr RGB TIFFs get totally corrupted by red overlay:
$converter = new IMagick();
$converter->setResourceLimit(6, 1);
$converter->readImageBlob($data);
if ($converter->getImageColorspace() != IMagick::COLORSPACE_RGB
&& $converter->getImageColorspace() != IMagick::COLORSPACE_GRAY
) {
$icc_rgb = file_get_contents('sRGB_v4_ICC_preference.icc');
$converter->profileImage('icc', $icc_rgb);
$converter->setImageColorspace(IMagick::COLORSPACE_RGB);
}
$converter->setImageFormat('jpeg');
$converter->setImageCompression(Imagick::COMPRESSION_JPEG);
$converter->setImageCompressionQuality(60);
$converter->resizeImage(1000, 1000, IMagick::FILTER_LANCZOS, 1, true);
$converter->stripImage();
$result = $converter->getImagesBlob();
This solution is taken from there: http://blog.rodneyrehm.de/archives/4-CMYK-Images-And-Browsers-And-ImageMagick.html Obviously, it doesn't work for all colorspaces, because it doesn't detect them reliably. As you can see, it even uses the sRGB_v4 ICC color profile downloaded from it's homepage.
Google finds me one particular solution to the red overlay problem (just one of the conversion screw-ups), but it's only for console and when you know beforehand that you deal with YCbCr images:
convert some.tif -set colorspace YCbCr -colorspace RGB some.jpg
I can live with passthru-ing convert and pass to convert all the magical switches needed, but I suppose I need to detect the source image's colorspace beforehand and call a identify | grep before every convert in an otherwise PHP application is an overkill.
I've experienced this same issue.
It also came up in the imagick forums and the correction was pushed into ImageMagick 6.8.0-4 .
So upgrading should solve this issue. I've upgraded to ImageMagick 6.8.1-9 and haven't encountered this since.