使用Intervention\Image创建多行文本图片代码如下:
<?php
namespace app\index\controller;
use app\index\model\ChapterModel;
use Intervention\Image\ImageManager;
// include composer autoload
require '../vendor/autoload.php';
class TestController extends Base
{
public function test()
{
//基本参数
$chapter_id = 259;
// 设置基本属性
$x = 15;
$y = 15;
$font_size = 24; // 文字大小
$width = 640; //设置图片宽
$font_height = 15; // 文字行距
$font_path = 'C:/Windows/Fonts/msyh.ttc'; // 设置字体路径
$section_imgs = $this->sectionImgsCreate($chapter_id, $x, $y, $font_size, $width, $font_height, $font_path);
dd($section_imgs);
}
/**
* 生成章节内容图片集
* @param int $chapter_id 章节id
* @param int $x 文字坐标
* @param int $y 文字坐标
* @param int $font_size 字体大小
* @param int $width 整体宽度
* @param int $font_height 段落间距
* @param string $font_path 字体路径
* @param int $user_id 用户id
* @return array $img 图片集
*/
public function sectionImgsCreate($chapter_id, $x, $y, $font_size, $width, $font_height, $font_path, $user_id = 0)
{
// 根目录
$root_path = app()->getRootPath() . 'public/';
// 图片管理器
$imageManager = new ImageManager();
// 获取章节内容
$chapter_text = (new ChapterModel)->find($chapter_id)->toArray();
// 匹配p标签内容
preg_match_all('/<p(.*?)>(.*?)<\/p>/', $chapter_text['content'], $p_txt);
// 拼凑生成图片text字符串
$p_txt = array_chunk($p_txt[2], 4);
// 生成章节标题图片
// 创建空白画布
$img = $imageManager->canvas($width, $font_size+$font_height);
$title = '第'. $chapter_text['cocc_chapterid'] . '章 ' . $chapter_text['chapter_name'];
// 插入文字
$this->text_img($img, $title, $width/2, $y, $font_size, $font_path, 'top', 'center');
$prefix = $user_id . '_' . $chapter_id . '_' . time() . rand(0,999);
$title_img_url = 'storage/Intervention/' . $prefix . '_title.png';
$imgs['title_img_url'] = $title_img_url;
//保存生成新图片
$img->save($root_path . $title_img_url);
// 生成章节内容图片
foreach ($p_txt as $txt_key => $txt) {
$txt_line =[];
/***********************************生成图片*******************************************/
// 设置基本属性
$line_y = $y;
$font_max_len = (int)($width-($x*2))/$font_size; // 每行最大文字长度
$first_line_indent = ' '; //设置首行缩进
foreach ($txt as $txt_p) {
$txt_line = array_merge($txt_line, $this->mbStrSplit($first_line_indent . $txt_p, $font_max_len));
}
$lines = $txt_line;
$height = ($font_size+$font_height)*count($lines); //设置图片高(文字大小+行距)*行数+行距
// 创建空白画布
$img = $imageManager->canvas($width, $height);
foreach ($lines as $line)
{
// 插入文字
$this->text_img($img, $line, $x, $line_y, $font_size, $font_path, 'top');
$line_y += $font_height + $font_size;
}
$line_url = 'storage/Intervention/'. $prefix . '_' . $txt_key .'.png';
array_push($imgs, $line_url);
//保存生成新图片
$img->save($root_path . $line_url);
}
return $imgs;
}
/**
* 中文自动换行
*/
private function mbStrSplit ($string, $len=1) {
$start = 0;
$strlen = mb_strlen($string);
$array =[];
while ($strlen) {
$array[] = mb_substr($string,$start,$len,"utf-8");
$string = mb_substr($string, $len, $strlen,"utf-8");
$strlen = mb_strlen($string);
}
return $array;
}
/**
* 插入图片文字
* @param object $img (Intervention/ImageManager对象)
* @param string $txt 文字
* @param int $x 坐标
* @param int $y 坐标
* @param int $font_size 字体大小
* @param int $font_path 字体路径
* @param int $valign 垂直对齐
* @param int $align 水平对齐
* @param int $color 字体颜色
*/
private function text_img(&$img, $txt, $x, $y, $font_size, $font_path = null, $valign = null, $align = null, $color = null)
{
$img->text($txt, $x, $y, function($font) use ($font_size, $font_path, $valign, $align, $color){
if ($font_path) $font->file($font_path);
if ($font_size) $font->size($font_size);
if ($color) $font->color($color);
if ($align) $font->align($align);
if ($valign) $font->valign($valign);
});
}
}