4

This is the Link I am following to create an image, however my GD is installed and working correctly as i tested with the very first example but this code breaks and flag "Image can not be displayed because it contains error" .

CODE -

<?php
//phpinfo();

//Report any errors
ini_set("display_errors", "1");
error_reporting(E_ALL); 

//Set the content type
header('content-type: image/png');

//Create our basic image stream 300x300 pixels
$image = imagecreate(300, 300);
//$image = imagecreatetruecolor(300, 300);
//Set up some colors, use a dark gray as the background color
$dark_grey = imagecolorallocate($image, 102, 102, 102);
$white = imagecolorallocate($image, 255, 255, 255);

//Set the path to our true type font 
//$font_path = 'advent_light';
//$font_path = 'advent_light.ttf';
//$font_path = 'arial';
$font_path = 'arial.ttf';

//Set our text string 
$string = 'Swapnesh!';

//Write our text to the existing image.
imagettftext($image, 50, 0, 10, 160, $white, $font_path, $string);

//Create our final image 
imagepng($image);

//Clear up memory 
imagedestroy($image);
?>

Things I tried and googled but with no solution are as follows - :(

  • Checked white/blank spaces before php tag if any.
  • Remove all code and spaces before header() method.
  • Changed imagecreate() to imagecreatetruecolor() as in comments in the code.
  • Checked font_path and set font path as per comments as well.
  • Followed this & this.

My PHP version - PHP Version 5.3.8

Still unable to locate the problem :(

enter image description here

Error

Error

EDITS-

version 1.0

this is what im getting on saving.

enter image description here

More Info about image --

enter image description here

on saving the page -- this is the name of file saving -- create_png.php.png

version 1.1

<br />
<b>Warning</b>:  imagettftext() [<a href='function.imagettftext'>function.imagettftext</a>]: Invalid font filename in <b>C:\xampp\htdocs\Core\CoreFiles\create_png.php</b> on line <b>20</b><br />
<br />
<b>Warning</b>:  imagettftext() [<a href='function.imagettftext'>function.imagettftext</a>]: Invalid font filename in <b>C:\xampp\htdocs\Core\CoreFiles\create_png.php</b> on line <b>23</b><br />

Thanks to @cryptic and @user1711126

Solution --

Font file actually missing, we need to put our .ttf file under the folder to make this code working or set path to make it work.

Community
  • 1
  • 1
swapnesh
  • 26,318
  • 22
  • 94
  • 126

3 Answers3

4

Save the image file that errors and open it in a text editor like notepad or equivalent and see if any PHP errors are inside it. An error is occurring and outputting text then which corrupts the file. Do that and you will find what the error is.

kittycat
  • 14,983
  • 9
  • 55
  • 80
  • @swapnesh No, save the image to your computer hard drive, then right click the file on your hard drive and open it in a program like notepad or some text editor. And look for text that would indicate a PHP error. If you can't do that then please make the file accessible for us to access so that we can examine the file. The easiest way to open the file as text document is rename it from create_png.php.png to create_png.php.txt and opening it. – kittycat Dec 26 '12 at 05:34
  • @cryptic..awesome something more meaningful lastly..but still i need ur help ..please follow my new edit :) – swapnesh Dec 26 '12 at 05:35
  • @swapnesh the value of $font_path make it the FULL path to the font file such as.. C:\xampp\htdocs\Core\CoreFiles\arial.ttf for example. – kittycat Dec 26 '12 at 05:39
  • @swapnesh Please see the documentation for the argument `fontfile` at http://us3.php.net/manual/en/function.imagettftext.php#refsect1-function.imagettftext-parameters – kittycat Dec 26 '12 at 05:40
  • I tried this, but no success still :( -- putenv('GDFONTPATH=' . realpath('.')); $font = 'arial'; && putenv('GDFONTPATH=' . realpath('.')); $font = 'arial.ttf'; – swapnesh Dec 26 '12 at 05:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21697/discussion-between-swapnesh-and-cryptic) – swapnesh Dec 26 '12 at 05:42
2

I seriously doubted your font path, please double check your font and file in same directory and if it is, then change path like this,

$font_path = './arial.ttf';

Edit (As per Discussion in chat)

you must need an ttf file to use imagettftext() function place the arial.ttf file in your folder where your file reside

senK
  • 2,782
  • 1
  • 27
  • 38
2

I think @cryptic is right you need to use the real path for font use something like

$font_path = basename(dirname(__FILE__)).'/arial.ttf'; 

and please make sure the font exists in the directory.