PHP如何制作一张简单的海报图

创建了一张800x600像素的纯白海报图,

并在中央添加了一个黑色“你好”的文本,字体为微软雅黑,字体大小为40。

你可以根据自己的需求修改代码。

注意,在使用GD库渲染中文字符时,不仅要指定字体文件路径和字体大小,

还需要通过imagettfbbox()函数获得文字宽高,以便于在图像中居中绘制。

<?php

// 创建画布
$width = 800;
$height = 600;
$im = imagecreatetruecolor($width, $height);

// 设置背景色
$bgColor = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $bgColor);

// 设置字体路径和字体大小
$fontFile = 'msyh.ttf';
$fontSize = 40;

// 添加文本
$textColor = imagecolorallocate($im, 0, 0, 0);
$text = '你好';
$textWidth = imagettfbbox($fontSize, 0, $fontFile, $text)[2] - imagettfbbox($fontSize, 0, $fontFile, $text)[0];
$textHeight = imagettfbbox($fontSize, 0, $fontFile, $text)[3] - imagettfbbox($fontSize, 0, $fontFile, $text)[5];
$x = ($width - $textWidth) / 2;
$y = ($height - $textHeight) / 2 - 30; // 为了让字体居中,往上偏移一点
imagettftext($im, $fontSize, 0, $x, $y, $textColor, $fontFile, $text);

// 保存图像
$imageFile = 'poster.png';
imagepng($im, $imageFile);

// 销毁图像
imagedestroy($im);

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五六碗瓶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值