Asked  7 Months ago    Answers:  5   Viewed   39 times

I am creating a lightweight, single-file database administration tool and I would like to bundle some small icons with it. What is the best way to embed images in a HTML/PHP file?

I know a method using PHP where I would call the same file with a GET parameter that would output hardcoded binary data with the correct header, but that seems a bit complicated.

Can I use something to pass the image directly in a CSS background-image declaration? This would allow me to utilize the CSS sprite technique.

Browser support isn't an issue here, so more exotic methods are welcome also.


Does someone have a link/example to how to generate Data URL's properly with PHP? I'd figure echo 'data:image/png;base64,'.base64_encode(file_get_contents('image.png')) would suffice but I could be wrong.



A solution to embed an image directly in an HTML page would be to use the data URI scheme

For instance, you could use some portion of HTML that looks like this :

<img src="data:image/png;base64,
vr4MkhoXe0rZigAAAABJRU5ErkJggg==" alt="Red dot" />

There are other solutions on the wikipedia page I linked to :

  • including the image as a CSS rule
  • Using some Javascript.

But note those solutions will not work on all browsers -- up to you to decide whether this is acceptable or not, in your specific situation.

Edit : to answer the question you asked about "how to generate Data URL's properly with PHP", take a look a bit lower in the wikipedia page about the Data URI scheme, which gives this portion of code (quoting) :

function data_uri($file, $mime) 
  $contents = file_get_contents($file);
  $base64   = base64_encode($contents); 
  return ('data:' . $mime . ';base64,' . $base64);

<img src="<?php echo data_uri('elephant.png','image/png'); ?>" alt="An elephant" />
Wednesday, March 31, 2021
answered 7 Months ago

Given all your data downloaded to my computer, I can convert it to png using ImageMagick Q16 Mac OSX Sierra that calls Inkscape 0.92.4 as the SVG renderer.

convert right.inside.svg x.png

enter image description here

You have not shown the correct result. So I do not know if this is correct or not. But it does appear to be correct given the images in the assets folder.

However, the mask and other image needs to be accessible from whomever is trying to view the svg file. So either the data needs to be downloaded or perhaps you need to provide the URL to those files on some server that has the svg file.

Saturday, May 29, 2021
answered 5 Months ago

As you are aware, everything passed as email message has to be textualized.

  • You must create an email with a multipart/mime message.
  • If you're adding a physical image, the image must be base 64 encoded and assigned a Content-ID (cid). If it's an URL, then the <img /> tag is sufficient (the url of the image must be linked to a Source ID).

A Typical email example will look like this:

Subject: A simple example
Mime-Version: 1.0
Content-Type: multipart/related; boundary="boundary-example"; type="text/html"

Content-Type: text/html; charset="US-ASCII"

... text of the HTML document, which might contain a URI
referencing a resource in another body part, for example
through a statement such as:
<IMG SRC="" ALT="IETF logo">

Content-Location: CID:somethingatelse ; this header is disregarded
Content-ID: <>
Content-Type: IMAGE/GIF
Content-Transfer-Encoding: BASE64

wbGljYXRpb24gcHJvaGliaXRlZC4A etc...


As you can see, the Content-ID: <> ID is matched to the <IMG> at SRC="". That way, the client browser will render your image as a content and not as an attachement.

Hope this helps.

Tuesday, June 1, 2021
answered 5 Months ago

What you need to do is encode the file to Base64, and include it like this:

<img src="data:image/gif;base64,R0lGODlhUAA..(the rest of your base64 encoded file)..">

Wednesday, August 11, 2021
Jeremy Pare
answered 3 Months ago

Well, at least for images you could use curl... As I've pointed out in the comments, you may create a php file (say, my.php) containing something like the following:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$picture = curl_exec($ch);
//Display the image in the browser
header('Content-type: image/jpeg');
echo $picture;

and than just write:

 <img src='my.php'>

P.S. Although I believe it is NOT the best way to do things, it looks like it solves the problem. No more private data in img src. I have never anything alike with video formats, but as for images it seems quite easy. You can read more about curl here:

Thursday, August 19, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :