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

roy*_*hew 13 html php gd image

我在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);
?> 
Run Code Online (Sandbox Code Playgroud)

图像输出的文件是image.php,下面是代码

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

</html>
Run Code Online (Sandbox Code Playgroud)

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

Akh*_*N S 44

使用它来向图像添加文本(从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);
    ?> 
Run Code Online (Sandbox Code Playgroud)

  • 你刚从http://www.phpforkids.com/php/php-gd-library-adding-text-writing.php粘贴它,试着指出问题而不是 (13认同)
  • 请同样引用您的消息来源......否则只是抄袭.我在引文中添加了. (5认同)

小智 5

这里的问题是, $black = ImageColorAllocate($im, 255, 255, 255);//<== 这不是黑色,它是白色的 // 对于黑色,它应该是这样的,

$black = ImageColorAllocate($im, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)