前言
最近接了一個需求,需要自動生成圖片,圖片分爲三個部分,如下顯示
Head 和 Footer 是固定的,Body 部分根據文字生產適應高度的圖片,下面上代碼, 代碼裏有詳盡註釋,代碼裏有一些特殊邏輯,不過也簡單,爲大家提供個思路。
代碼
<?php
// 圖片頭
$image = ['./img/head.jpg'];
// 初始高度
$height = 0;
// 頁面寬度
$width = 1000;
// 創建圖片的方法
function createImg($string)
{
mb_internal_encoding("UTF-8"); // 設置編碼
// 初始化字符串, 這個字符串是用來做換行的
$content = "";
// 字體
$font = "./img/11.otf";
// 文字的寬度
$width=980;
//字體大小
$size = 35;
// 行數
$line_num = 0;
// 將字符串拆分成一個個單字 保存到數組 letter 中
for ($i = 0; $i < mb_strlen($string); $i++) {
$letter[] = mb_substr($string, $i, 1);
}
foreach ($letter as $l) {
$teststr = $content . " " . $l;
$fontBox = imagettfbbox($size, 0, $font, $teststr);
// 判斷拼接後的字符串是否超過預設的寬度
// 注意看後邊這兩個判斷條件, 第一個判斷條件是根據字符串長短分隔一條中的換行, 第二個條件是根據特殊字符 ■ 來做每條信息的換行.
if ((($fontBox[2] > $width) && ($content !== "")) || $l == '■') {
$content .= "\n";
$line_num += 1;
}
$content .= $l;
}
// 根據 行數 生成 白色背景圖片
$im = imagecreatetruecolor(1000, $line_num * 85);
// 將背景設爲白色
$red = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $red);
Imagejpeg($im, './img/bg.jpg', 1000);
// exit();
// 文字圖片後邊的背景
// todo: 背景圖片高度根據文字高度來做, 這樣生成的圖片寬高就適合
// 現在的背景圖是提前做好的, 不適合文字的長短
$backgroundPath = './img/bg.jpg';
$img = imagecreatefromjpeg($backgroundPath); //背景圖
// 加上時間, 字體變紅
$date = date('Y年m月d日', time()) ;
// 生成星期幾
$weekarray=array("日","一","二","三","四","五","六"); //先定義一個數組
$date .= " 星期".$weekarray[date("w")];
$red = imagecolorallocate($img, 255, 0, 0);
imagettftext ( $img, 32, 0, 300, 50, $red, $font, $date );
// 編輯
//設置字體顏色
$black = imagecolorallocate($img, 0, 0, 0);
//字體
imagettftext ( $img, $size, 0, ceil(($width - $fontBox[2]) / 2), 100, $black, $font, $content );
// 生成圖片的地址
$filePathDir = './img/';
if (!is_dir($filePathDir)) {
mkdir($filePathDir, 0777, true);
}
// 隨機生成一個文件名
$filePath = $filePathDir . time() . rand(0, 999) . '.jpg';
// 生成
imagejpeg($img, $filePath);
return $filePath;
}
// 文章標題數組
$info_title = [
'1.原創|食品過敏原標識管理介紹 (國外篇)',
'2.11111',
'3.22222',
'3.33333',
'4.4444',
'5.5555',
'2.6666',
'6.7777',
'6.8888',
'6.99999',
];
// 組合成一個字符串
$info_title = '■ '.implode('■ ', $info_title);
// 開始生成
$fontpath = createImg($info_title);
// 注意看這塊, 圖片拼接時 是按順序來的
// 1 是生成的文字圖片
$image[1] = $fontpath;
// 2 是 尾圖
$image[2] = './img/foot.jpg';
// 前面需要的圖片都弄好了, 開始聚合成一張
// 獲取圖片基本信息
foreach ($image as $k => $v) {
$source[$k]['source'] = Imagecreatefromjpeg($v);
$res_image[$k] = getimagesize($v);
}
//獲取新畫布的總高度
foreach ($res_image as $k => $v) {
$new_height = $v[1] * $width / $v[0];###我的新畫布固定寬度爲1000
$height += $new_height;###計算新畫布的高度
$res_image[$k]['height'] = $new_height;
//等比例縮放
$image_p[$k] = imagecreatetruecolor($width, $new_height);
imagecopyresampled($image_p[$k], $source[$k]['source'], 0, 0, 0, 0, 1000, $new_height, $v[0], $v[1]);
}
//創建一個新的畫布
$new_image = imagecreatetruecolor($width, $height);
//向畫布添加圖片
$dst_x = 0;
$dst_y = 0;
foreach ($res_image as $k => $v) {
imagecopy($new_image, $image_p[$k], $dst_x, $dst_y, 0, 0, $v[0], $v[1]);###參數挺多可以參照官方文檔呦http://php.net/manual/zh/book.image.php
$dst_y += $v['height'];
}
//添加地址
$date = date('Ymd', time());
$dir = './' . $date;
// 生成唯一文件名
$name = uniqid();
//生成圖片
$res_data = Imagejpeg($new_image, './img/' . $name . '.jpg', 1000);