php创建简单的3D文字

/ 设置字体路径(这里使用一个支持中文的字体文件,你需要替换为你的字体文件路径) $font = ‘./zk3.ttf’;

// 设置背景图片路径 $backgroundImagePath = ‘./image.png’;

// 创建一个空白图像,大小与背景图片相同 $backgroundImage = imagecreatefrompng($backgroundImagePath); $imageWidth = imagesx($backgroundImage); $imageHeight = imagesy($backgroundImage); $image = imagecreatetruecolor($imageWidth, $imageHeight);

// 将背景图片复制到新创建的图像上 imagecopy($image, $backgroundImage, 0, 0, 0, 0, $imageWidth, $imageHeight);

// 释放背景图片资源 imagedestroy($backgroundImage);

// 设置文字内容(中文) $text = ‘你好,世界!’;

// 设置文字大小 $textSize = 20;

// 生成随机颜色 function getRandomColor() {    global $image;    return imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); }

// 定义文字颜色(随机) $textColor = getRandomColor();

// 定义描边颜色(随机) $strokeColor = getRandomColor();

// 计算文字的宽度和高度 $textBox = imagettfbbox($textSize, 0, $font, $text); $textWidth = $textBox[2] – $textBox[0]; $textHeight = $textBox[1] – $textBox[7];

// 计算文字的居中位置 $x = ($imageWidth – $textWidth) / 2; $y = ($imageHeight – $textHeight) / 2 + $textHeight;

// 生成立体效果 for ($i = -2; $i <= 2; $i++) {    for ($j = -2; $j <= 2; $j++) {        // 在文字周围绘制具有轻微偏移的副本        $offsetX = $i * 1; // 调整偏移量以获得更好的立体效果        $offsetY = $j * 1; // 调整偏移量以获得更好的立体效果        $color = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));        imagettftext($image, $textSize, 0, $x + $offsetX, $y + $offsetY, $color, $font, $text);    } }

// 在图像中心绘制文字 imagettftext($image, $textSize, 0, $x, $y, $textColor, $font, $text);

// 保存图像到文件 $savePath = ‘./image_3d.png’; // 替换为你的保存路径 imagepng($image, $savePath);

// 释放内存 imagedestroy($image);

echo “Image saved successfully at: $savePath”;

原创文章,作者:guozi,如若转载,请注明出处:https://www.sudun.com/ask/78851.html

(0)
guozi的头像guozi
上一篇 2024年5月30日
下一篇 2024年5月30日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注