0

I found this topic: convert text to image in php

and I tested the code that Sourabh Shankar provide.

When I paste this code in a new empty php file and I adjust the font path, then it works perfect!

But when I put a echo before or after the header('Content-type: image/png'); line, then it all fails

Can someone provide me a code of how to call this image creator in a HTML file:

example:

<span>Here comes the image</span>
<?php [call function??] ?>
<span>and here we continue with some HTML</span>

Do I need to create a php function and if so:

what do I excactly return? if I return this:

imagepng($im);

then he wont execute the

imagedestroy($im);

I'm not sure how to do this

Community
  • 1
  • 1
Ralph Schipper
  • 701
  • 2
  • 12
  • 24

2 Answers2

1

The PHP code you linked to works as a stand-alone file, so that when it is requested in a browser it will send back an image. Setting the header with

header('Content-type: image/png');

means that the code will return content which the browser interprets as an image file, rather than a HTML file.

You couldn't use PHP to directly output a file into HTML. You would need to use that code in a separate file and link it to the src tag of an image in your code.

joshkokay
  • 46
  • 4
1

You need get image from other script via http.

<span>Here comes the image</span>
<img src="/path/to/image_handler.php"/>
<span>and here we continue with some HTML</span>

where image_handler.php - your php script:

<?php
…
header('Content-type: image/png');
imagepng($im);

NOTE: never output something before call header

Gedweb
  • 697
  • 1
  • 6
  • 22