指尖上的记忆指尖上的记忆
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub

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);//输出