gpt4 book ai didi

php - 如何使用 PHP GD 库向图像添加文本

转载 作者:IT王子 更新时间:2023-10-29 00:07:41 25 4
gpt4 key购买 nike

我在 image_creator 中有图像创建代码。

<?php
header("Content-Type: image/jpeg");
$im = ImageCreateFromGif("photo.gif");
$black = ImageColorAllocate($im, 255, 255, 255);
$start_x = 10;
$start_y = 20;
Imagettftext($im, 12, 0, $start_x, $start_y, $black, 'verdana.ttf', "text to write");
Imagejpeg($im, '', 100);
ImageDestroy($im);
?>

图片输出文件是image.php,代码如下

<html>
<head>
</head>
<body>
<img src="http://localhost/image_creator.php"/>
</body>

</html>

当我运行 image.php 时,我只得到一个空白页面。为什么会这样?

最佳答案

使用它向图像添加文本(从 PHP for Kids 复制)

<?php
//Set the Content Type
header('Content-type: image/jpeg');

// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('sunset.jpg');

// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);

// Set Path to Font File
$font_path = 'font.TTF';

// Set Text to Be Printed On Image
$text = "This is a sunset!";

// Print Text On Image
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);

// Send Image to Browser
imagejpeg($jpg_image);

// Clear Memory
imagedestroy($jpg_image);
?>

关于php - 如何使用 PHP GD 库向图像添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13267846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com