加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP图片处理库Grafika详细教程(1):图像基本处理

发布时间:2016-11-16 03:50:32 所属栏目:PHP教程 来源:segmentfault
导读:副标题#e# Grafika是一个PHP图像处理库,是基于Imagick和GD,可以用于改变图片大小,剪裁,比较,添加水印等等功能。还有感知哈希,高级图像过滤,绘制贝塞尔曲线等功能,可谓非常强大。 由于功能太多,所以分成几篇文章写。 《1、图像基本处理》 《2、图像

4)、我们再用一张完全不同的图片测试

PHP图片处理库Grafika详细教程(1):图像基本处理 

  1. use GrafikaGrafika; 
  2. $editor = Grafika::createEditor(); 
  3. $result = $editor->compare('yanying.jpg' , 'yanying-h.jpg'); 
  4. var_dump($result); // int 39  

结果39,越来越大,越来越不像

2、比较图片是否相同

grafika提供方法equal来检查两张图片是否完全相同。这里的检查是一个像素一个像素的检测,所以时间可能会较长。

当然grafika也会预检查,如果两张图片大小不相同,则直接返回false。只有其他都相同后才会进行逐像素检查。

我们这里对比之前创建的一张缩略图,因为大小不一致,所以直接返回false  

PHP图片处理库Grafika详细教程(1):图像基本处理 

  1. use GrafikaGrafika; 
  2. $editor = Grafika::createEditor(); 
  3. $result = $editor->equal('yanying.jpg' , 'yanying-smaller.jpg'); 
  4. var_dump($result); // boolean false  

智能剪裁

智能剪裁是自动识别图像中的重要部分,剪裁时候偏向于保留重点部分。

不过grafika也提供了人为操控位置剪裁,我们先说这个。

基本位置剪裁

基本位置剪裁包含9个位置

  • top-left
  • top-center
  • top-right
  • center-left
  • center
  • center-right
  • bottom-left
  • bottom-center
  • bottom-right

我们这里一起说了,这里我们使用900*600的图片,分成9块  

PHP图片处理库Grafika详细教程(1):图像基本处理 

  1. use GrafikaGrafika; 
  2. $editor = Grafika::createEditor(); 
  3.  
  4. $src = 'yanying.jpg'; 
  5. $editor->open( $image, $src ); 
  6. $editor->crop( $image, 300, 200, 'top-left' ); 
  7. $editor->save( $image, 'result1.jpg' ); 
  8. $editor->free( $image ); 
  9.  
  10. $editor->open( $image, $src ); 
  11. $editor->crop( $image, 300, 200, 'top-center' ); 
  12. $editor->save( $image, 'result2.jpg' ); 
  13. $editor->free( $image ); 
  14.  
  15. $editor->open( $image, $src ); 
  16. $editor->crop( $image, 300, 200, 'top-right' ); 
  17. $editor->save( $image, 'result3.jpg' ); 
  18. $editor->free( $image ); 
  19.  
  20. $editor->open( $image, $src ); 
  21. $editor->crop( $image, 300, 200, 'center-left' ); 
  22. $editor->save( $image, 'result4.jpg' ); 
  23. $editor->free( $image ); 
  24.  
  25. $editor->open( $image, $src ); 
  26. $editor->crop( $image, 300, 200, 'center' ); 
  27. $editor->save( $image, 'result5.jpg' ); 
  28. $editor->free( $image ); 
  29.  
  30. $editor->open( $image, $src ); 
  31. $editor->crop( $image, 300, 200, 'center-right' ); 
  32. $editor->save( $image, 'result6.jpg' ); 
  33. $editor->free( $image ); 
  34.  
  35. $editor->open( $image, $src ); 
  36. $editor->crop( $image, 300, 200, 'bottom-left' ); 
  37. $editor->save( $image, 'result7.jpg' ); 
  38. $editor->free( $image ); 
  39.  
  40. $editor->open( $image, $src ); 
  41. $editor->crop( $image, 300, 200, 'bottom-center' ); 
  42. $editor->save( $image, 'result8.jpg' ); 
  43. $editor->free( $image ); 
  44.  
  45. $editor->open( $image, $src ); 
  46. $editor->crop( $image, 300, 200, 'bottom-right' ); 
  47. $editor->save( $image, 'result9.jpg' ); 
  48. $editor->free( $image );  

看下结果  

PHP图片处理库Grafika详细教程(1):图像基本处理

智能剪裁

原图

PHP图片处理库Grafika详细教程(1):图像基本处理

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读