手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆

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

首页 > PHP >

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

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

缩图:

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

 




本站采用创作共享版权协议, 要求署名、非商业和保持一致. 本站欢迎任何非商业应用的转载, 但须注明出自"易栈网-膘叔", 保留原始链接, 此外还必须标注原文标题和链接.

Tags: php, imagemagick, magicwand, thumbnail

« 上一篇 | 下一篇 »

只显示10条记录相关文章

使用PHP得到所有的HTTP请求头 (浏览: 62781, 评论: 3)
我为什么会选用phpstorm (浏览: 52684, 评论: 5)
快速生成目录树 (浏览: 46833, 评论: 7)
通过file_get_contents来Post数据的实例 (浏览: 46401, 评论: 5)
PHP导入导出Excel方法 (浏览: 45218, 评论: 3)
PHP的XSS攻击过滤函数 (浏览: 42719, 评论: 2)
PHP中Eval的作用 (浏览: 41659, 评论: 4)
超详细:在Mac OS X中配置Apache + PHP + MySQL (浏览: 40379, 评论: 1)
PHP常见错误(二) (浏览: 39778, 评论: 1)
PHP sendmail (浏览: 37963, 评论: 7)

1条记录访客评论

飞猪啊,这文章很棒

Post by BoPo on 2008, August 10, 1:22 PM 引用此文发表评论 #1


发表评论

评论内容 (必填):