ryoakg
3/4/2017 - 3:15 PM

inline-image-with-data-uri-scheme.php

<?php
if (PHP_SAPI === 'cli') {
  echo 'Usage:' . PHP_EOL;
  echo "  run: php -S localhost:8080 {$argv[0]}" . PHP_EOL;
  echo '  browse http://localhost:8080/' . PHP_EOL;
  exit;
}

// via http://stackoverflow.com/a/2329400/3585790
function data_uri($file, $mime) {
  $contents = file_get_contents($file);
  $base64   = base64_encode($contents);
  return ('data:' . $mime . ';base64,' . $base64);
}

?>
<html>
  <head>
    <meta charset="UTF-8">
    <title>inline image</title>
    </head>
  <body>
    <img src="<?php echo data_uri('https://upload.wikimedia.org/wikipedia/ja/7/7b/Othello.PNG','image/png'); ?>" alt="othello" />
    <br />
    This image is described in Data URI shceme. See the HTML source.
  </body>
</html>