手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表Tag:imagemagick

利用imageMagicK制作有背景色的缩略图

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

原文网址: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
浏览: 1599 次
点击打开新窗口浏览全图

缩图:

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

 

Tags: php, imagemagick, magicwand, thumbnail

imageMagick的一些资料

最近需要使用imagemagic,实在不熟,在GOOGLE上找了点资料,记录下来,以备用。

  1. http://hi.baidu.com/tomshi/blog/item/6bae4034e75c884f241f146d.html(Imagemagick的用法)
  2. http://www.imagemagick.org/Usage/annotating/#wmark_image(官网的一些操作介绍)
  3. http://cn.php.net/manual/en/book.imagick.php(PHP手册中的介绍,这是通过DLL操作的,好象是,没有研究过)

» 阅读全文

Tags: php, imagemagick, magicwand