Image cannot be displayed because it contains an error in php gd

This Link I follow the creation of the image, however, my GD is installed and working correctly, since I tested from the very first example, but this code breaks and the flag " Image cannot be displayed because it contains an 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); ?> 

The things I tried and googled, but without a solution, are the following : (

  • Checked white / empty space before php tag, if any.
  • Remove all code and spaces before the header () .
  • Changed imagecreate () to imagecreatetruecolor () , as in the comments in the code.
  • Check font_path and set the font path according to the comments.
  • and this .

My PHP Version - PHP Version 5.3.8

Still can't find the problem :(

enter image description here

Error

Error

edits -

version 1.0

This is what I get when saving.

enter image description here

Additional Image Information -

enter image description here

when saving the page - this is the name of the save file - 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 @cryptic and @user1711126

The solution is

The font file is actually missing, we need to put our .ttf file in a folder for this code to work or set the path for it to work.

+4
source share
3 answers

Save the image file with errors and open it in a text editor, for example, in notepad or equivalent, and see if there are any PHP errors in it. An error occurs and text is output that distorts the file. Do this and you will find what the error is.

+4
source

I seriously doubt your font path, please double check the font and file in the same directory, and if so, change the path as follows:

 $font_path = './arial.ttf'; 

Edit (as per chat discussion)

you need a ttf file to use the imagettftext () function, put the arial.ttf file in your folder where your file is located

+2
source

I think @cryptic is correct, you need to use the real path for the font in order to use something like

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

and make sure the font exists in the directory.

+2
source

All Articles