-4

I would like to turn a .txt file into image like png.

How to make it possible by gd in php? Would you kindly show me how this would be achieved

1 Answers1

1

There is no one-shot-kill function to do this.

PHP GD is a big library. You have to go through a few steps:

  • grab the text content inside the *.txt file and store it in a variable using file_get_contents();
  • create an in-memory (png) image of the appropriate dimensions (this will be tricky) and store the image in a variable with imagecreate();
  • position the text you grabbed from the file somewhere within the image (this is tricky but corresponds with the previous trickiness) using imagettftext() - while making sure you have the *.ttf font files available on the server;
  • save or output the image to using imagepng();

The tricky part I previously referenced is calculating the size of the needed image based on the amount of text in the *.txt file and on the font sizes per letter, per row etc..

The second tricky part is calculating the positioning of the text in the image so as to not overflow the image boundaries.

Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51