环境要求
- PHP >= 5.3,推荐php7
- GD库 >= 2.0版本
- Imagick最好(不强求)>=3.3.0 , ImageMagick >= 6.5.3
开发前准备
- 安装PHP图像处理库
composer require kosinix/grafika
- 准备一张证件照(一寸照)
下载地址:https://github.com/kxsv/id-photo-demo/blob/main/images/test.jpeg - 准备设计好的6寸模板图
下载地址:https://github.com/kxsv/id-photo-demo/blob/main/images/print-bg/295-413.png
代码实现
<?php
require 'vendor/autoload.php';
use Grafika\Grafika;
use Grafika\Color;
$id_photo = __DIR__ . '/test.jpeg'; //一寸证件照
$print_bg = __DIR__ . "/295-413.png"; //获取排版背景图
try {
$editor = Grafika::createEditor(['Imagick']);
$editor->open($image1, $print_bg);
$editor->open($image2, $id_photo);
$editor->blend($image1, $image2, 'normal', 1, 'top-left', 120, 180);
$editor->blend($image1, $image2, 'normal', 1, 'top-left', 435, 180);
$editor->blend($image1, $image2, 'normal', 1, 'top-left', 750, 180);
$editor->blend($image1, $image2, 'normal', 1, 'top-left', 1065, 180);
$editor->blend($image1, $image2, 'normal', 1, 'top-left', 1380, 180);
$editor->blend($image1, $image2, 'normal', 1, 'top-left', 120, 613);
$editor->blend($image1, $image2, 'normal', 1, 'top-left', 435, 613);
$editor->blend($image1, $image2, 'normal', 1, 'top-left', 750, 613);
$editor->blend($image1, $image2, 'normal', 1, 'top-left', 1065, 613);
$editor->blend($image1, $image2, 'normal', 1, 'top-left', 1380, 613);
$save_path = __DIR__ . '/res-print.jpeg'; //排版后保存的目录
$editor->save($image1, $save_path);
exit('排版完成');
} catch (Exception $e) {
var_dump($e->getMessage());
}
查看排版照
完整代码下载
https://github.com/kxsv/id-photo-demo