First off, I want to say that yes, I do know that doing this with file system and just saving the file name/location in the database is the better way to go, and I will probably do that in the final version.
This is mostly an experiment/proof of concept/learning opportunity.
I have a PHP upload form that is working. It takes an image and turns it into a base64 string and puts it into an image blob in the database table.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
// cleaning title field
$title = trim(sql_safe($_POST['title']));
if ($title == '') // if title is not set
$title = '(empty title)';// use (empty title) string
if ($_POST['password'] != $password) // cheking passwors
$msg = 'Error: wrong upload password';
else
{
if (isset($_FILES['photo']))
{
@list(, , $imtype, ) = getimagesize($_FILES['photo']['tmp_name']);
// Get image type.
// We use @ to omit errors
if ($imtype == 3) // cheking image type
$ext="png"; // to use it later in HTTP headers
elseif ($imtype == 2)
$ext="jpeg";
elseif ($imtype == 1)
$ext="gif";
else
$msg = 'Error: unknown file format';
if (!isset($msg)) // If there was no error
{
//$file = File Image yang ingin di encode
//Filetype: JPEG,PNG,GIF
$base64 = "";
$file = $_FILES['photo']['tmp_name'];
if($fp = fopen($file,"rb", 0))
{
$gambar = fread($fp,filesize($file));
fclose($fp);
$base64 = chunk_split(base64_encode($gambar));
}
// Preparing data to be used in query
$q = "INSERT INTO tblCompanyImg (CompanyID, ImgNum, ImgExt, ImgName, ImgImg) Values (1, 1, '$ext', '$title', '$base64')";
$database->query($q);
$msg = "Success: image uploaded:";
}
}
elseif (isset($_GET['title'])) // isset(..title) needed
$msg = 'Error: file not loaded';// to make sure we've using
// upload form, not form
// for deletion
}
}
?>
And I know it is saving it in correctly because I can view the image like this:
<?php
while($row = $database->fetch_array($result))
{
$CompanyImgID = $row["CompanyImgID"];
$CompanyID = $row["CompanyID"];
$ImgName = $row["ImgName"];
// outputing list
echo "<img src='data:image/jpeg;base64," . $row['ImgImg'] . "' />";
}
?>
What I would like to do next is to view the uploaded image within a visual basic program.
I tried this:
Dim sSql As String = "Select * from tblCompanyImg Where CompanyImgID = 2"
Dim rSelect As New ADODB.Recordset
Dim img As Image
Dim imageBytes As Byte()
Dim ms As MemoryStream
With rSelect
.Open(sSql, MyCn, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockOptimistic)
If Not .EOF Then
imageBytes = .Fields!ImgImg.Value
ms = New MemoryStream(imageBytes, 0, imageBytes.Length)
ms.Write(imageBytes, 0, imageBytes.Length)
img = Image.FromStream(ms, True) ' it fails right here: Parameter is not valid '
LogoPictureBox.Image = img
End If
.Close()
End With
but it fails on the img = Image.FromStream(ms, True) line with an error Parameter is not valid.
Is there a better way to read or write this to the database to make it work?
Anyway, here’s a class for you.
MyImage.vb
Then stick this in your web.config
Then calling 2.jpg.db will get your image file.