I have to create a program that stores images inside of a MySQL DB. I will be using PHP for this. I need to know how I can insert images as binary into MySQL and then display them again. I can't figure out how to do it using MySQL. I think the data is in there but then it doesn't display. I only see a broken link. Can anyone point me in the right direction?
Storing Binary
Started by
Guest_NeedHelp_*
, Jun 26 2006 04:49 PM
3 replies to this topic
#1
Guest_NeedHelp_*
Posted 26 June 2006 - 04:49 PM
Guest_NeedHelp_*
|
|
|
#2
Posted 27 June 2006 - 07:06 AM
NeedHelp, I've done this before. The table needs to be a blob.
This is the code to insert into DB
add.php (Where the user inserts data to be submitted and browses for pictures)
insert.php file
This is the code to insert into DB
add.php (Where the user inserts data to be submitted and browses for pictures)
<form enctype="multipart/form-data" action="insert.php" method="POST" name=changer> <input type="hidden" name="MAX_FILE_SIZE" value="102400"> <input type="file" name="photo" accept="image/jpeg"> <input type="Submit" value="Submit">
insert.php file
<?php
// Connect to the DB somwhere up here
// Read our photo
$sPic="";
if (!empty($_FILES['photo']['name'])) {
$sPic = addslashes(FormatImage($_FILES['photo']['name'], $_FILES['photo']['tmp_name']));
}
$query = "INSERT INTO name (tb_name) VALUES ('$spic')";
$results = mysql_query($query) or die ("Error " . mysql_error());
mysql_close();
// This function formats the image and can resize if desired
function FormatImage($sPhotoFileName, $sTempFileName) {
//$sPhotoFileName = $_FILES['photo']['name']; // get client side file name
if ($sPhotoFileName) // file uploaded
{ $aFileNameParts = explode(".", $sPhotoFileName);
$sFileExtension = end($aFileNameParts); // part behind last dot
if ($sFileExtension != "jpg"
&& $sFileExtension != "JPEG"
&& $sFileExtension != "JPG")
{ die ("Choose a JPG for the photo");
}
$nPhotoSize = $_FILES['photo']['size']; // size of uploaded file
if ($nPhotoSize == 0)
{ die ("Sorry. The upload of $sPhotoFileName has failed.
Search a photo smaller than 100K, using the button.");
}
if ($nPhotoSize > 102400)
{ die ("Sorry.
The file $sPhotoFileName is larger than 100K.
Advice: reduce the photo using a drawing tool.");
}
}
// read photo
//$sTempFileName = $_FILES['photo']['tmp_name']; // temporary file at server side
$oTempFile = fopen($sTempFileName, "r");
$sBinaryPhoto = fread($oTempFile, fileSize($sTempFileName));
// Try to read image
$nOldErrorReporting = error_reporting(E_ALL & ~(E_WARNING)); // ingore warnings
$oSourceImage = imagecreatefromstring($sBinaryPhoto); // try to create image
//error_reporting($nOldErrorReporting);
if (!$oSourceImage) // error, image is not a valid jpg
{ die ("Sorry.
It was not possible to read photo $sPhotoFileName.
Choose another photo in JPG format.");
}
$nWidth = imagesx($oSourceImage); // get original source image width
$nHeight = imagesy($oSourceImage); // and height
// create small thumbnail
$nDestinationWidth = $nWidth;
$nDestinationHeight = $nHeight;
//$oDestinationImage = imagecreatetruecolor($nDestinationWidth, $nDestinationHeight);
$oDestinationImage = imagecreate($nDestinationWidth, $nDestinationHeight);
// below resizes the image
imagecopyresized(
$oDestinationImage, $oSourceImage,
0, 0, 0, 0,
$nDestinationWidth, $nDestinationHeight,
$nWidth, $nHeight); // resize the image
ob_start(); // Start capturing stdout.
imageJPEG($oDestinationImage); // As though output to browser.
$sBinaryThumbnail = ob_get_contents(); // the raw jpeg image data.
ob_end_clean(); // Dump the stdout so it does not screw other output.
// Return our image
return $sBinaryThumbnail;
}
?>
Void
#3
Posted 27 June 2006 - 07:13 AM
This is the code to view an image that has been stored in a database. You have to use a seperate php and call it like an image.
Here is the HTML where it calls the php image
^^ This can be stuck in any HTML page.
You can remove the ID from this if you only want to select the one image or whatever. Here is the viewpicture
viewpicture.php
Here is the HTML where it calls the php image
<img src="viewpicture.php?id=<? print "$id"; ?>" />
^^ This can be stuck in any HTML page.
You can remove the ID from this if you only want to select the one image or whatever. Here is the viewpicture
viewpicture.php
<?php
// Connect to DB up here
// Select our picture and display it
$query = "SELECT * FROM db WHERE ID='$id'";
// Run our query and display the image
$results = mysql_query($query);
$row = mysql_fetch_array($results);
$sJpg = $row[##] // Replace ## with the row number
// Send header info
header("Content-type: image/jpeg"); // act as a jpg file to browser
// Show it
print $sJpg;
?>
Void
#4
Guest_Jordan_*
Posted 27 June 2006 - 01:09 PM
Guest_Jordan_*
Nice post void! I'm copying this to the PHP section as well.


Sign In
Create Account

Back to top










