Home     Articles & Projects     Products & Web Services

PHP image uploader base64

Hi I am wondering is it possible to upload a picture (jpg) and convert it to base64?
Also is base64 quicker to load then just an image in a db?

Hi Russell, Certainly

Hi Russell,

Certainly possible - and probably a good idea (data is only about 33% larger than original). After upload, simply load the JPEG image into a string; encode with base64_encode() and then insert into the your image field (as BLOB) in the database (don't forget to use mysql_escape_string() of course).

On the way out; your image serving script simply needs to send the correct Content-Type header; and then use base64_decode and then print the output... e.g.

image.php

<?php
  header
("Content-Type: image/jpeg");
 
// db connection code here 
 
$result = mysql_query("SELECT jpeg_base64 FROM table WHERE  id='".mysql_escape_string($_GET["id"]."'");
 
$row = mysql_fetch_assoc($result);
 
$jpeg_raw = base64_decode($row["jpeg_base64"]);
  print
$jpeg_raw;
  exit();
?>

Cheers,
David.