//创建画布
$posterIm = imagecreatetruecolor($dstW, $detH);
$white = imagecolorallocate($posterIm, 255, 255, 255);
imagefill($posterIm, 0, 0, $white);
//合并并头像
imagecopyresampled ($posterIm , $titleIm , 0 , 0 , 0 , 0 , $dstW , $titleH , $dstW , $titleH);
//合并消息
if(!empty($message)) {
imagecopyresampled ($posterIm , $messageIm , 0 , $detH_ONE , 0 , 0 , $dstW , $messageH , $dstW , $messageH);
}
if(!empty($imgs)) {
$imgIm = $this->_addImg($imgs);
imagecopyresampled ($posterIm , $imgIm , 0 , $detH_TOW , 0 , 0 , $dstW , $imH , $dstW , $imH);
}
//合成标题以及用户信息
private function _addTile($title, $avtarUrl, $nickeName, $time, &$titleH, $appid) {
//过滤表情
$title = preg_replace("#(\\\ud[0-9a-f]{3})#i", " ", $title);
$nickeName = (mb_strlen($nickeName, 'utf-8') > 8) ? mb_substr($nickeName,0, 8).'...' : $nickeName;
$titleColor = [80,60,40];
$fontType = $fontType = self::POWER_FONT;
$imContent = $this->addText(self::PSOTER_W, $titleH, 33, $titleColor, 585, 2, $title, $fontType, 16, 40);
$im = $imContent['dstIm'];
$titleH = $titleH - $imContent['dstImH']; //减去裁剪的高度
$avtar=DiscuzUser::getBigUcAvatar($avtarUrl);
//合入头像
// $avtarUrl = str_replace("https://", "http://", $avtarUrl);
$imInfo = getimagesize($avtar);
$mime = explode('/', $imInfo['mime']);
//头像圆形合成
$avatarIm = $this->radiusImg($avtar, $mime[1], 60, [120, 120]); //传递的路径
imagecopyresampled ($im , $avatarIm , 42 , 194-$imContent['dstImH'] , 0 , 0 , 60 , 60 , 60 , 60 );
//写入文字
if($appid == PREGNANCYLITE_APPID) { //区分色调
$nicNameColor = ImageColorAllocate($im,255,138,181);
} else {
$nicNameColor = ImageColorAllocate($im,103,196,171);
}
$timeColor = ImageColorAllocate($im,191,183,180);
imagettftext($im, 20, 0, 123, 238-$imContent['dstImH'], $nicNameColor, $fontType, $nickeName);
imagettftext($im, 18, 0, 440, 237-$imContent['dstImH'], $timeColor, $fontType, date("Y-m-d H:i:s", $time) );
return $im;
}
//文字合成
public function addText($desImW, $desImH, $fontSize, $textRgb, $textW, $textRow, $textContent, $fontFile, $space, $textTop = 0) {
$dstIm = imagecreatetruecolor($desImW, $desImH);
$white = imagecolorallocate($dstIm, 255, 255, 255);
imagefill($dstIm, 0, 0, $white);
$col = ImageColorAllocate($dstIm, 0, 0, 0);
//文字换行
$content = '';
$length = mb_strlen($textContent, 'utf-8');
//将文字放入一个数组中
for ($i = 0; $i < $length; $i++) {
$letter[] = mb_substr($textContent, $i, 1);
}
$n = 1;
foreach ($letter as $l) {
if($n > $textRow) {
break;
}
$teststr = $content . " " . $l;
$testbox = imagettfbbox($fontSize, 0, $fontFile, $teststr);
// 判断拼接后的字符串是否超过预设的宽度
if(($testbox[2] - $testbox[0] > $textW) && ($content !== "")) {
$content .= "\n";
$n = $n + 1;
}
$content .= $l;
}
//文字设置换行后 数组
$test = explode("\n", $content);
$test = array_slice($test,0, $textRow);
//如果超过限定的行数,替换成省略号
if($n > $textRow) {
$lastText = $test[count($test)-1];
$lastLeng = mb_strlen($lastText) - 4;
$test[count($test)-1] = mb_substr($lastText, 0, $lastLeng).'...';
}
//生成文字到画布中
foreach ($test as $key => $value) {
$textbox = imagettfbbox($fontSize, 0, $fontFile, $value);
$txtHeight = $textbox[1] - $textbox[7];
if(!isset($y)){
$y = $textbox[1] - $textbox[7] + $textTop;
}
imagettftext($dstIm, $fontSize, 0, 43, $y, $col, $fontFile, $value);
$y = $y + $txtHeight + $space; // 加2为调整行距
}
$cutH = ($textRow - $key-1)*($txtHeight+$space); //裁剪
$croped = imagecreatetruecolor($desImW, $desImH - $cutH);
imagecopy($croped, $dstIm, 0, 0, 0, 0, $desImW, $desImH - $cutH);
return [
'dstIm' => $croped,
'dstImH' => $cutH ,
];
}