`
hooroger
  • 浏览: 12608 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

M4JAVA+GraphicsMagick处理网站图片

 
阅读更多

现在做的网站需要保存用户上传的图片,同时需要进行压缩和图片切割等特殊效果的处理。

    一开始我们用的是JMagick+ ImageMagick处理用户上传的图片,但是存在一个非常严重的问题,Tomcat在跑了大概10天左右后会crash掉,异常内容为:

Java代码  收藏代码
  1. magick.MagickException: Unable to retrieve handle  

 

    从表象看,应该是没有释放ImageMagick的句柄导致的,查找API后发现程序中没有调用

 

Java代码  收藏代码
  1. magick.MagickImage.destroyImages()   Called by finalize to deallocate the image handle.  

    在程序加上本方法,情况有所改善,大概在40天左右后出现了一次tomcat进程crash。

 

    似乎并没有从本质上找到问题的根本。。。

    在JMagick的邮件列表中找到一份我认为有价值的邮件,http://sourceforge.net/mailarchive/message.php?msg_name=20cf28cd1002231148s33c99843q875f59906dd32b8b%40mail.gmail.com

    里面讲到了JMagick作为应用服务的缺点,并建议可以使用IM4JAVA:

Java代码  收藏代码
  1. The "JNI hazard" here is that if something you use (f.ex libtiff for reading  
  2. TIFF files) has a memory bug then it can make your whole JVM crash. Thats of  
  3. course frustrating and therefore its great to have im4java around, which  
  4. starts IM as an external process, so JVM crashes are avoided.  
  5.  * *  
  6. Coolest thing would be if JMagick and im4java could have the same API so it  
  7. was easy to switch depending on luckyness. Ive asked the author of im4java  
  8. to attemt to be as compatible as possible, but as im4java is very much  
  9. different internally its limited how much can be done in that direction.  
  10.   
  11. If you don't like the risk, stick to im4java. If your want optimal  
  12. performance give JMagick a try.  
  13.   
  14. And, its not JMagick that is buggy, its what it depends on (hereunder IM)  
  15. that is not always (memory) bug free on long running processes.  
  16. I also have never seen a mismatch between JMagick binary and ImageMagick  
  17. binaries leading to crashes.  

 

    所以我把思路转向了IM4Java。

    IM4Java的官网 http://im4java.sourceforge.net/index.html 。

    在 Developer's Guide 中提到im4java支持ImageMagick和GraphicsMagick。GraphicsMagick是ImageMagick的一个分支,相对于ImageMagick而言,TA处理速度更快,消耗资源更少,并且大的图片处理网站,如 Flickr andEtsy  已经在使用TA了。

    下载GraphicsMagick的Q8版本(还有Q16、Q32的,版本见的区别可以查看ImageMagick的官网,大致意思是每像素用多少bit来存储信息,16、32要比8消耗更多的内存^_^)。

    下面这段话是在GraphicsMagick的邮件列表中找到的:

Java代码  收藏代码
  1. > I've got question about IM Q16 & Q8. What's a difference between that  two versions?   
  2. I know, that Q16 version takes 2 times more memory than Q8.   
  3. But is that so great precision in algorithm is needed in converting  jpegs, gif or pngs   
  4. (I need IM for converting that types of formats)   
  5.   
  6. & JPEG and GIF only have 8-bit versions, so there will almost certainly be no benefit to using Q16.   
  7. It is possible to have 16-bit PNGs, but if you have to ask, you probably don't have to worry about it  

   安装GraphicsMagick和IM4Java非常简单,按照官网做就ok了,下面是一个简单的例子:

 

Java代码  收藏代码
  1. /** 
  2.      * 先缩放,后居中切割图片 
  3.      * @param srcPath 源图路径 
  4.      * @param desPath 目标图保存路径 
  5.      * @param rectw 待切割在宽度 
  6.      * @param recth 待切割在高度 
  7.      * @throws IM4JavaException  
  8.      * @throws InterruptedException  
  9.      * @throws IOException  
  10.      */  
  11.     public static void cropImageCenter(String srcPath, String desPath, int rectw, int recth) throws IOException, InterruptedException, IM4JavaException  
  12.     {  
  13.         IMOperation op = new IMOperation();  
  14.           
  15.         op.addImage();  
  16.         op.resize(rectw, recth, '^').gravity("center").extent(rectw, recth);  
  17.         op.addImage();  
  18.   
  19.         ConvertCmd convert = new ConvertCmd(true);  
  20.         //convert.createScript("e:\\test\\myscript.sh",op);  
  21.         convert.run(op, srcPath, desPath);  
  22.   
  23.     }  

   我的头像就是用这个方法压缩的,源图为:

 原始尺寸为578*800,大小为68.8KB,处理后为180*180,大小为 6.15KB

 

在我的机器上使用LoadRunner进行了压力测试,并发10用户的情况下,每秒能处理11张左右的图片。

我的机器配置为:

Inter(R) Pentium(R) D CPU2.80GHz, 2.00GB 内存,迈拓 6V160E0 7200rpm

操作系统为 Windows server2003 Enterprise Edition Service Pack 2

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics