2

So what I want to do is to write a txt with text and image contents, and then put it in a webpage from the string (with php).

Read the file with php and get a line of the content of the txt to transform it to an image and be able to put it on an html tag.

I haven't tried anything yet, because I don't really know how to do this, or even if it's possible.

Possible duplicate: I think that it isn't because I didn't know what Base64 was so that other answer you were supposed to know it, so for doing it easier to search for people I think it's better to leave it (or not I don't really care so much).

  • Possible duplicate of [How to display Base64 images in HTML?](https://stackoverflow.com/questions/8499633/how-to-display-base64-images-in-html) – ArtisticPhoenix Mar 27 '19 at 21:51
  • thanks! I'll try it –  Mar 27 '19 at 21:52
  • Not sure if this is what you are asking but you can take the contents of a image file for example and base64 encode it and put that directly in the `` tag. This link is to the answer (for the duplicate, I posted) that is specific to PHP https://stackoverflow.com/questions/8499633/how-to-display-base64-images-in-html#answer-21606233 – ArtisticPhoenix Mar 27 '19 at 21:53
  • "Read the file with php and get a line of the content of the txt to transform it to an image and be able to put it on an html tag." - so you want to generate images on the fly that simply contain your lines of text from the file? Perhaps as a anti-copy/paste measure? – ivanivan Mar 27 '19 at 23:29

1 Answers1

0

php:

$path = <path to file>;

echo '<img src="data:' . mime_content_type($path) . ';base64,' . base64_encode(file_get_contents($path))" alt="alt_text">';

html:

<img src="data:<mime>;base64,<base64 byte data string>" alt="alt_text">

you can set an image tag to hold raw image byte data, it just has to be in base64 format.

the data:<mime>; part holds the mime, or file content type.

the base64,<base64 encoded string> holds the raw data from the image that you get from using file_get_contents($path) and then encoded in base64 format with base64_encode() function

Zac
  • 799
  • 1
  • 11
  • 31