title: Storing image in database directly or as base64 data?
body: <p>The common method to store images in a database is to convert the image to <code>base64</code> data before storing the data. This process will increase the size by 33%. Alternatively it is possible to directly store the image as a <code>BLOB</code>; for example:</p>
  
  <pre><code>$image = new Imagick("image.jpg");
  $data = $image-&gt;getImageBlob();
  $data = $mysqli-&gt;real_escape_string($data);
  $mysqli-&gt;query("INSERT INTO images (data) VALUES ('$data')");
  </code></pre>
  
  <p>and then display the image with</p>
  
  <pre><code>&lt;img src="data:image/jpeg;base64,' .  base64_encode($data)  . '" /&gt;
  </code></pre>
  
  <p>With the latter method, we save 1/3 storage space. Why is it more common to store images as <code>base64</code> in MySQL databases?</p>
  
  <p><strong>UPDATE:</strong> There are many debates about advantages and disadvantages of storing images in databases, and most people believe it is not a practical approach. Anyway, here I assume we store image in database, and discussing the best method to do so.</p>