如何使用 PHP GD 库向图像添加文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13267846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add text to an image with PHP GD library
提问by roy mathew
I have image creation code in image_creator.
我在 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);
?>
The file for image output is image.php and has below code
图像输出文件是 image.php 并具有以下代码
<html>
<head>
</head>
<body>
<img src="http://localhost/image_creator.php"/>
</body>
</html>
When I run image.php, I just get a blank page. Why is it so?
当我运行 image.php 时,我只得到一个空白页面。为什么会这样?
回答by Akhilraj N S
Use this to add text to image (copied from PHP for Kids)
使用它向图像添加文本(从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);
?>
回答by Ali Nawaz Hiraj
Problem here is,
$black = ImageColorAllocate($im, 255, 255, 255);//<== this not black, its white
//for black it should be like,
这里的问题是,
$black = ImageColorAllocate($im, 255, 255, 255);//<== 这不是黑色,它是白色的 // 对于黑色,它应该是这样的,
$black = ImageColorAllocate($im, 0, 0, 0);

