Mecha CMS

Mecha CMS blog and documentation.

Inline Image to Base64 Converter

Making simple online tool with Mecha.

Requires eval plugin.

Create a new page then add this content:

title: Image to Base64 Converter
description: Convert image file to base64 string.
type: HTML

<form action="" method="post" enctype="multipart/form-data">
  <p><input name="_image" type="file" class="input block"></p>
  <p><button type="submit" class="button">Convert</button></p>

if ($blob = HTTP::files('_image')) {
    if (!empty($blob['error'])) {
        echo '<p style="color:red;">Error.</p>';
    } else if (strpos($blob['type'], 'image/') !== 0) {
        echo '<p style="color:red;">Images only!</p>';
    } else {
        $base64 = To::base64(File::open($blob['tmp_name'])->read());
        $base64 = From::HTML('data:' . $blob['type'] . ';base64,' . $base64);
        echo '<p><img alt="" onclick=";" title="View URL" src="' . $base64 . '"></p>';