How to embed images in a single HTML / PHP file?

后端 未结 3 609
清酒与你
清酒与你 2020-11-30 06:44

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

3条回答
  •  长情又很酷
    2020-11-30 07:23

    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 :

    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);
    }
    ?>
    
    An elephant
    

提交回复
热议问题