PHP: Convert Image file content to blob image tag

For the mimetype, see Mimey or another lib like it.
Or this shortcut might work for some file types like png and jpg: $mimetype = "image/".pathinfo($path,PATHINFO_EXTENSION);

$path = //path/to/file.jpg (or whatever)

$mimer = new \Mimey\MimeTypes();
$mimetype = $mimer->getMimeType(pathinfo($path,PATHINFO_EXTENSION));

$source = file_get_contents($path);
$base64 = base64_encode($source);
$blob = 'data:'.$mimetype.';base64,'.$base64;

$html = '<img src="'.$blob.'" alt="Description for differently abled humans." />';
echo $html;

Common Mime Types

Mdn has a list of common mime types.

For your convenience here are the (probably) most common image ones:

  • .gif: image/gif
  • .jpg or .jpeg: image/jpeg
  • .png: image/png