php将文字在图片上居中:
主要通过 imagettftext 实现,以下是示例代码,实际使用可以根据具体需求修改:
$srcImg = imagecreatefrompng($BackgroundImage);
$NewpriceColor = imagecolorallocate($srcImg, 231, 71, 57);//设置颜色为红色
$Newprice = 10.00;
$fontBox = imagettfbbox(55, 0, $Galyon, $Newprice);//获取文字所需的尺寸大小
// 居中算法
// ceil((700 - $fontBox[2]) / 2) 宽度
// ceil(($height - $fontBox[1] - $fontBox[7]) / 2) 高度
imagettftext($srcImg, 55, 0, ceil((700 - $fontBox[2]) / 2), 395, $NewpriceColor, $Galyon, $Newprice . "元");
imagesavealpha($srcImg,true);
// imagepng($srcImg,__RUN__ . 'thumb.png');
header("Content-type:image/png");//png格式
imagepng($srcImg);//输出
