php通过Grafika实现九宫格组图

什么是Grafika?

Grafika是一个PHP图像处理库,功能强大。

独特的功能

  • 智能裁剪
  • 动画GIF支持
  • 5种缩略图模式
  • 图像比较
  • 图像高级过滤功能
  • 图像混合
  • 规范化的API

安装

Grafika的官网

在项目根目录中,在命令行中输入:

composer require kosinix/grafika:dev-master --prefer-dist

创建编辑器

编辑器用于操纵图像。推荐的方法是使用Grafika :: createEditor()。它会自动选择最佳编辑器。它将检查Imagick是否可用。如果没有,它将回归使用GD。

use Grafika\Grafika; // 引入包

$editor = Grafika::createEditor(); // 创建编辑器

编辑器包含所有的图片处理方法。

下面是我生成合成图的实际代码:

public function images()
{
  try {
  $editor = Grafika::createEditor();
  $imageBack = Grafika::createBlankImage(750, 750);//创建一个750*750的空白图像

  $image1 = $image2 = $image3= $image4 = '';
  $editor->open($image1, 'E:/goodsPic/1.jpeg');//打开1.jpeg并且存放到$image1
  $editor->resizeFill($image1, 375, 375);//居中剪裁。就是把较短的变缩放到200px,然后将长边的大于200px的部分居中剪裁掉,图片不会变形
  $editor->blend($imageBack, $image1, 'normal', 1, 'top-left');//将两个图像合成在一起,第一个图像作为基础,第二个图像在顶部。支持多种混合模式
  $editor->save($imageBack, 'E:/goodsPic/all.jpg');//imageBack保存为all.jpg

  $editor->open($image2, 'E:/goodsPic/2.jpg');
  $editor->resizeFill($image2, 375, 375);
  $editor->blend($imageBack, $image2, 'normal', 1, 'top-right');
  $editor->save($imageBack, 'E:/goodsPic/all.jpg');

  $editor->open($image3, 'E:/goodsPic/3.jpg');
  $editor->resizeFill($image3, 375, 375);
  $editor->blend($imageBack, $image3, 'normal', 1, 'bottom-left');
  $editor->save($imageBack, 'E:/goodsPic/all.jpg');

  $editor->open($image4, 'E:/goodsPic/4.jpg');
  $editor->resizeFill($image4, 375, 375);
  $editor->blend($imageBack, $image4, 'normal', 1, 'bottom-right');
  $editor->save($imageBack, 'E:/goodsPic/all.jpg');
  } catch (Exception $e){
    echo $e->getMessage();
  }
  exit('run over');
}

最后生成一张合成图:



猜你喜欢

转载自www.cnblogs.com/designers/p/9922852.html
今日推荐