How to add text to an image with PHP GD library

爱⌒轻易说出口 提交于 2019-11-27 07:19:53
Akhilraj N S

Use this to add text to image (copied from 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);
    ?> 

Problem here is, $black = ImageColorAllocate($im, 255, 255, 255); //<== this not black, its white //for black it should be like,

$black = ImageColorAllocate($im, 0, 0, 0);
user3335949

Problem here is

$black = ImageColorAllocate($im, 255, 255, 255);

this not black, its white. For black it should be like,

$black = ImageColorAllocate($im, 0, 0, 0);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!