1

Hello following this answer, i'm trying to add a text to a image using GD Library, sadly i'm unable to do it, this is my code:

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


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

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

  // Set Path to Font File
  $font_path = 'denmark.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);
?> 

This code will Output the image without the text in it.

The serra.jpg and denmark.ttf files are both in the same folder as the script.

Running the gd_info() function on my server i got the following:

GDINFO

Any ideas on what could possible be going on?

7
  • Does $font_path exist? Check that with file_exists(). Commented Feb 4, 2016 at 17:23
  • Yes, running if(!file_exists($font_path)) { die("No font"); }, returned nothing. So i believe the file does exist. Commented Feb 4, 2016 at 17:26
  • What are the dimensions for serra.jpg? Are you sure the coordinates for the text isn't out of the image? Commented Feb 4, 2016 at 17:29
  • serra.jpg is 2272x1704, i did played with the position of the text, to see if that was the problem, but no lucky there. Commented Feb 4, 2016 at 17:34
  • @André Did you check this imagetext Commented Feb 4, 2016 at 17:39

4 Answers 4

1

Go through the following steps:

  • if you are working in Unix-based OS check permissions for 'denmark.ttf' file. Make sure that it accessible by your php script
  • change font path as follows: $font_path = './denmark.ttf';
Sign up to request clarification or add additional context in comments.

Comments

0

Get a solution,Check this code.

imagejpeg($jpg_image,'newimage.jpeg');

You are not save your image.

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


  // Create Image From Existing File
  $source="mk.jpg";
  $jpg_image = imagecreatefromjpeg($source);

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

  // Set Path to Font File
  $font_path = 'ASMAN.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,'newimage.jpeg');

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

Comments

0

I was having some weird problem with the $font_path.

So reading the http://php.net/manual/en/function.imagettftext.php

I saw that i have to add putenv... before the $font_path variable like this:

  putenv('GDFONTPATH=' . realpath('.'));
  $font_path = 'denmark.ttf';

Now is working fine.

Also i tested the solution presented by Roman, and by just changing the font_path to:

$font_path = './denmark.ttf';

The script worked fine!

Thanks for the help guys!

2 Comments

you can put your font in same folder and call it from same directory
I am notice one thing you are not generate new image you just send out put to the browser temporary
0
$font_path = 'denmark.ttf';

Should be

$font_path = 'denmark';   //no need to incluce .ttf when not using backward slash.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.