标签:中文字体 ota this reg one str 计算 kpi 字符
php imagick 文字居中的方法
<pre>
public function getwenzinfo($nickName)
{
$nickNamelen = mb_strlen($nickName, ‘utf-8‘);
$wzallkd = 0;
$wzallkdlist = array();
$wzallkdlist[] = 0;
$perzifulist = array();
for ($i = 0; $i < $nickNamelen; $i++) {
$perzifu = mb_substr($nickName, $i, 1, ‘utf-8‘);
$perzifulist[] = $perzifu;
if (preg_match("/^[A-Za-z0-9]+$/", $perzifu) == false) {
// echo ‘是中文‘;
$wzkd = 26;
$wzallkd += 26;
} else {
// echo ‘不是中文‘;
$wzkd = 17;
$wzallkd += 15;
}
$wzallkdlist[] = $wzkd;
}
/*因为字符串宽度是 字符本身宽度+字符之间的间隔-最后的字符串间隔*/
$wzallkd=$wzallkd-2;
array_pop($wzallkdlist);
$wenziinfo[‘wzallkd‘] = $wzallkd;
$wenziinfo[‘wzallkdlist‘] = $wzallkdlist;
$wenziinfo[‘perzifulist‘] = $perzifulist;
return $wenziinfo;
}
$text = new \Imagick();
$text->newImage(750, 1206, ‘none‘);
$text->setImageFormat(‘png‘);
$draw1 = new \ImagickDraw();
$draw1->setFont(__DIR__ . ‘/../../../Public/tianyangzhongqiu/ziti/fanzhengdahei.ttf‘); //设置中文字体 可以解决中文乱码问题
/*$draw->setTextAlignment(1);*///1 从文字的左下方开始计算的 2 从文字中下来计算 3 从文字右下来计算 默认为1
$xingzuocolor = $this->getxingzuocolor($xingzuo);
$draw1->setFillColor(new \ImagickPixel($xingzuocolor));
$draw1->setFontSize(27);
$draw1->setTextAlignment(1);
$nickName = ‘啦啊‘;
$wenziinfo = $this->getwenzinfo($nickName);
$wzallkd = $wenziinfo[‘wzallkd‘];
$wzallkdlist = $wenziinfo[‘wzallkdlist‘];
$perzifulist = $wenziinfo[‘perzifulist‘];
$wenzizuobiao = (750 - $wzallkd) / 2;
foreach ($wzallkdlist as $k => $v) {
$wenzizuobiao += $v;
$draw1->annotation($wenzizuobiao, 240, $perzifulist[$k]);
}
$draw1->annotation(167, 290, $chengji);
$text->drawImage($draw1);
</pre>
ps:就是先设置宽度 然后打字 就这样文字宽度我来定 就不会出错了
标签:中文字体 ota this reg one str 计算 kpi 字符
原文地址:https://www.cnblogs.com/newmiracle/p/11853363.html