title: restore base64_encode image to a file system from database
body: <p>I'm saving some of my image in to mysql database using <code>base64_encode</code>.
  now I want to restore them back to file system.</p>
  
  <p>How can I do that?</p>
  
  <p>Edit...!</p>
  
  <p>Ok, I did not explain enough.</p>
  
  <p>I use this code to encode my image and save them in to a blob table:</p>
  
  <pre><code>function base64_encode_image ($imagefile) {
  $imgtype = array('jpg', 'gif', 'png');
  $filename = file_exists($imagefile) ? htmlentities($imagefile) : die('Image file name does not exist');
  $filetype = pathinfo($filename, PATHINFO_EXTENSION);
  if (in_array($filetype, $imgtype)){
      $imgbinary = fread(fopen($filename, "r"), filesize($filename));
  } else {
      die ('Invalid image type, jpg, gif, and png is only allowed');
  }
  return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
  }
  </code></pre>
  
  <p>and use this code to show my image in browser:</p>
  
  <pre><code>if (!isset($_GET['id']) &amp;&amp; !ctype_digit($_GET['id'])){
      die('Error');
  } else {
      require_once( addslashes(dirname(dirname(__FILE__)) . '/config.php') );
      require_once( addslashes(dirname(__FILE__) . '/Functions.php'));
      $row = mysql_fetch_array(mysql_query ("SELECT `id`,`cover_small` FROM `om_manga` WHERE `Active` = '1' AND `id` = '".sql_quote($_GET['id'])."'"));
      if (isset($row['id'])){
          header("Content-type: image/jpeg");
          readfile($row['cover_small']);
      } else {
          die('Error');
      }
  }
  </code></pre>
  
  <p>Now i want them back to a jpg file.</p>
  
  <p>The size of all those image are less then 3kb.</p>