2013-03-23 121 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。

相關問題