python实现代码请参考我的另一篇博文:https://blog.csdn.net/JineD/article/details/106622398
<?php
/**
* Created by graph-design-api.
* AUTHOR: daiguojin
* Date: 2020/9/16 9:40
*/
//header("Content-type: image/png");
// The text to draw
$text = 'ABeeZee';
// Replace path by your own font path
$fontFile = 'ABeeZee.ttf';
$fontSize = 24;
$pos = imagettfbbox($fontSize, 0, $fontFile, $text);
$width = $pos[2] - $pos[0];
$height = $pos[1] - $pos[7];
// Create the image
$im = imagecreatetruecolor($width, $height);
//imagealphablending($im, false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
//imagesavealpha($im, true); //这里很重要