用PHP将CMYK格式的JPG文件转为RGB格式

来源:互联网 发布:剑雨江湖宠物进阶数据 编辑:程序博客网 时间:2024/05/22 10:56

上次说到,CMYK格式的JPG用IE6浏览时无法显示,解决方法是用PS之类的软件转成RGB。但对于网站来说,用户并不知道这么解决,所以还是要程序想办法解决了。解决方法是用imagick或者imagemagick来处理图片,imagick代码如下:

[php] view plaincopyprint?
  1. <?php  
  2.   
  3. $filePath = '/path/to/your/file.jpg';  
  4. $i = new Imagick($filePath);  
  5.   
  6. $cs = $i->getImageColorspace();  
  7.   
  8. if ($cs == Imagick::COLORSPACE_CMYK) {  
  9.   
  10.     print "Image is CMYK<br/>n";  
  11.   
  12.     ?>  
  13.     CMYK Image:<br/>;  
  14.     <img src="<?php echo $filePath ?>"/>  
  15.     <br/><br/>  
  16.     <?php  
  17.   
  18.     $i->setImageColorspace(Imagick::COLORSPACE_SRGB);  
  19.     $i->setImageFormat('jpeg');  
  20.   
  21.     $cs = $i->getImageColorspace();  
  22.   
  23.     if ($cs != Imagick::COLORSPACE_CMYK) {  
  24.   
  25.         print "Image is no longer CMYK<br/>n";  
  26.   
  27.         // write it to a temp file   
  28.         $filePath = '/path/to/temp/file.jpg';  
  29.         $i->writeImage($filePath);  
  30.   
  31.     }  
  32.   
  33. else {  
  34.     print "Image is not CMYK<br/>n";  
  35. }  
  36.   
  37. if ($cs == Imagick::COLORSPACE_SRGB ||  
  38.     $cs == Imagick::COLORSPACE_RGB){  
  39.     print "Image is RGB<br/>n";  
  40. }  
  41.   
  42. ?>  
  43. RGB Image:<br/>  
  44. <img src="<?php echo $filePath ?>"/>;  
  45. <?php  
  46.   
  47. $i->clear();  
  48. $i->destroy();  
  49. $i = null;  

Linux下====================
convert -colorspace rgb 123.jpg 123.jpg
原创粉丝点击