2013-03-23 85 views
4

我试图将RGB图像转换为CMYK,因为它们需要打印。 我使用这个代码:使用Imagick将图像从RGB转换为CMYK

<?php 
$filePath = 'rgb.jpg'; 

// First save image as png 
$image = new Imagick($filePath); 
$image->setImageCompression(Imagick::COMPRESSION_UNDEFINED); 
$image->setImageCompressionQuality(0); 
$image->setImageFormat("png"); 
$filePath = 'rgb.png'; 
$image->writeImage($filePath); 
$image->clear(); 
$image->destroy(); 
$image = null; 

// Convert colors 
$image = new Imagick($filePath); 
$image->stripImage(); 
$image->setImageColorspace(Imagick::COLORSPACE_CMYK); 
$image->setImageCompression(Imagick::COMPRESSION_UNDEFINED); 
$image->setImageCompressionQuality(0); 
$image->setImageFormat("png"); 
$filePath = 'cmyk.png'; 
$image->writeImage($filePath); 

$image->clear(); 
$image->destroy(); 
$image = null; 


$fileUrl = 'http://www.product-designer.nl/rgb2cmyk/cmyk.png'; 
?> 
CMYK Image:<br/> 
<img src="<?php echo $fileUrl; ?>" width="400" /><br /><br /> 
<?php 
$fileUrl = 'http://www.product-designer.nl/rgb2cmyk/rgb.png'; 
?> 
RGB Image:<br/> 
<img src="<?php echo $fileUrl ?>" width="400" /> 

你可以看到http://product-designer.nl/rgb2cmyk 的结果,我不知道怎么回事,但不知何故,图片上的颜色变成反转。 我需要转换图像,但颜色需要尽可能接近RGB颜色。

有谁知道如何做到这一点?

感谢

+0

我有这个相同的问题..经过多次黑客攻击和研究,我解决了这个问题,请看我的答案。 – 2013-06-15 15:39:32

回答

3

看看here

<?php 
// don't use this (it inverts the image) 
// $img->setImageColorspace (imagick::COLORSPACE_RGB); 

if ($img->getImageColorspace() == Imagick::COLORSPACE_CMYK) { 
    $profiles = $img->getImageProfiles('*', false); 
    // we're only interested if ICC profile(s) exist 
    $has_icc_profile = (array_search('icc', $profiles) !== false); 
    // if it doesnt have a CMYK ICC profile, we add one 
    if ($has_icc_profile === false) { 
     $icc_cmyk = file_get_contents(dirname(__FILE__).'/USWebUncoated.icc'); 
     $img->profileImage('icc', $icc_cmyk); 
     unset($icc_cmyk); 
    } 
    // then we add an RGB profile 
    $icc_rgb = file_get_contents(dirname(__FILE__).'/sRGB_v4_ICC_preference.icc'); 
    $img->profileImage('icc', $icc_rgb); 
    unset($icc_rgb); 
} 

$img->stripImage(); // this will drop down the size of the image dramatically (removes all profiles) 
?> 
+0

Roman Nazarkin,谢谢你的回答。我实现了你提供的代码,它给了我以下结果: http://product-designer.nl/rgb2cmyk/ 我没有看到任何区别..我在正确的目录中添加.icc文件。 – 2013-03-23 14:52:17

+0

感谢您的回答。我实现了你提供的代码,它给了我以下结果:product-designer.nl/rgb2cmyk我没有看到任何区别..我在正确的目录中添加.icc文件 – 2013-03-23 15:25:32

+0

嗯,我看到一切都是作品 - http://s3.micp.ru/HHJhm.jpg – 2013-03-24 02:09:26

1

好了,所以这是一个棘手的问题。我有同样的问题,花了我几天的时间来解决这个问题。你需要negateImage(),请参阅我的例子,并确保你只能做它的PHP 5.3.x因为这个问题是唯一到PHP与

$range = $jpeg->getQuantumRange(); 
$php_vs_arr = preg_split("/\./", phpversion()); 
$php_vs = $php_vs_arr[0] . '.' . $php_vs_arr[1]; 
if ($jpeg->getImageColorspace() == Imagick::COLORSPACE_CMYK) { 

    //make sure cmyk color-space is set correctly 
$jpeg->setImageColorspace(12); 

// then we add an RGB profile 
$icc_rgb = file_get_contents(FRAMEWORK_PATH . DS . 'color' . DS . 'AdobeRGB1998.icc'); 
$jpeg->profileImage('icc', $icc_rgb); 
unset($icc_rgb); 

//set color space to rgb 
$jpeg->setImageColorspace(13); 

//fix gamma, hue, saturation, brightness 
if($php_vs < 5.3) { 
    //ADJUST GAMMA BY 2.0 for 5.2.x 
    $jpeg->levelImage(0, 2.0, $range['quantumRangeString']); 
} else { 
    //php 5.3 hack FOR INVERTED COLORS 
    $jpeg->negateImage(false, Imagick::CHANNEL_ALL); 
} 

} 
$jpeg->stripImage(); 

注:我Imagick对象显然$ JPEG

此外,您需要从Adobe网站下载AdobeRGB1998.icc配置文件,只需执行谷歌搜索即可。

我希望这会有帮助,请将其标记为正确的答案,因为很多人都有这方面的麻烦。

0

可能的原因是RGB是可加颜色方案,而CMYK是减色方案。即我们可以使用以下公式将RGB转换为CMYK:

C = 255 - R; 
M = 255 - G; 
Y = 255 - B; 

它看起来像“如何反转颜色”。 所以在这种情况下我们也应该算出K值。

为了避免它,我们可以使用ICC-profiles

一个可能的解决方案:

$image = new Imagick('img/test.jpg'); 
$profiles = $image->getImageProfiles('*', false); 
if (!array_search('icc', $profiles)) { 
    // Without this code Photoshop cannot open image with original icc-profile 
    $icc_rgb = file_get_contents('profiles/AppleRGB.icc'); 
    if ($image->profileImage('icc', $icc_rgb)) 
     echo '<br>Changed!'; 
    unset($icc_rgb); 
} 
// don't use this code, it leads to inverted image 
//$image->setImageColorspace(Imagick::COLORSPACE_CMYK); 
// tiff format supports CMYK colorscheme 
$image->setFormat('tiff'); 
$image->setImageCompression(Imagick::COMPRESSION_UNDEFINED); 
$image->setImageCompressionQuality(0); 
// We can download profiles here http://www.adobe.com/support/downloads/thankyou.jsp?ftpID=4075&fileID=3790 
$icc_cmyk_profile_path='profiles/USWebCoatedSWOP.icc'; 

$icc_cmyk = file_get_contents($icc_cmyk_profile_path); 
if ($image->profileImage('icc', $icc_cmyk)) 
    echo '<br>Changed!'; 
unset($icc_cmyk); 
// Drops all profiles, so we comment it 
//$image->stripImage(); 
if ($image->writeImage('img/test_cmyk.tiff')) 
    echo '<br>Save!'; 
$image->clear(); 
$image->destroy(); 

作品尺寸被改变时,例如从72Kb到1.4Mb。

相关问题