简单人生
幻想指点江山,梦中激扬文字

原文如下:附本人翻译(翻译水平其差,可以忽略直接看英文)

原文网址:http://valokuva.org/?p=92
  1. I know, it's been a while since I last blogged. This is because a lot of things are happening in my personal life. I recently relocated to London from Finland and started a new job. Things are quite busy but I will try to post an example now and then. In the meanwhile I would like to hear about sites using Imagick, so if your project is not super secret please post an url and maybe a small explanation what you're doing with Imagick on the site. This is purely for my personal interest.
  2. Anyway, to the point. Today's example originates from a question asked by a user. How do I thumbnail the image inside given dimensions proportionally and fill the "blank" areas with a color? Well, the answer is here :)
  3. The code is for Imagick 2.1.0 but adapting to older versions should not be hard.

 

翻译如下:

我知道现在离我的博客更新有一段时间了,这主要是因为最近发生了很多事。最近我找了份新工作,使我从芬兰搬到了伦敦,工作也非常忙,所以我在这里仅仅只发一个示例。同时我很高兴听到很多网站使用了imagick,我也希望如果您的项目不是很保密的话,是否能够发个链接及少量的注释以说明您的站点是如何使用Imagek的。顺便说一声,这完全是我个人的兴趣。

OK,上重头菜。今天的例子起源于一个用户的问题,那就是如果在为一张图片按比例做缩略图的时候,怎么为留空的地方用其他颜色填充。这个例子就是我的回复。

代码是FOR ImagicK 2.1.0版本,当然要改成以前的老版本代码也不是件难事。

下面就是代码了:

PHP代码
  1. <?php  
  2. /* Define width and height of the thumbnail */ 
  3. /* 定义缩略图的长宽*/
  4. $width = 100;  
  5. $height = 100; 

  6. /* Instanciate and read the image in */ 
  7. /*创建一个对象,同时读回源图*/
  8. $im = new Imagick( "test.png" );  
  9. /* Fit the image into $width x $height box 
  10. The third parameter fits the image into a "bounding box" */ 
  11. /*按照比例进行缩放*/
  12. $im->thumbnailImage( $width$height, true );  
  13. /* 按照缩略图大小创建一个有颜色的图片 */  
  14. $canvas = new Imagick();  
  15. $canvas->newImage( $width$height'pink''png' );  
  16. /* 取得缩图的实际大小 */  
  17. $geometry = $im->getImageGeometry();  
  18. /* 计算高度 */  
  19. $x = ( $width - $geometry['width'] ) / 2;  
  20. $y = ( $height - $geometry['height'] ) / 2;  
  21. /* 合并图片  */  
  22. $canvas->compositeImage( $im, imagick::COMPOSITE_OVER, $x$y );  
  23. /* 输出图片*/  
  24. header( "Content-Type: image/png" );  
  25. echo $canvas;  
  26. ?>  

 膘叔,上图片

源图:

大小: 320.67 K
尺寸: 300 x 198
浏览: 15 次
点击打开新窗口浏览全图

缩图:

大小: 35.55 K
尺寸: 100 x 100
浏览: 22 次
点击打开新窗口浏览全图

 

posted on 2008-08-04 19:21 简单人生 阅读(832) 评论(1)  编辑 收藏 引用
Comments
只有注册用户登录后才能发表评论。