php imagick 文字居中的方法详解编程语言

php imagick 文字居中的方法


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);

ps:就是先设置宽度 然后打字 就这样文字宽度我来定 就不会出错了

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/19139.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论