Re: Loading png images into a mysql table/database



And hackajar - thanks for the handy sample code!!!
Maybe I get smart now!!!

ewholz,


hackajar@xxxxxxxxx wrote:
Code to Import images , with HTML goodness

===SNIP START===
<table>
<tr>
<td Colspan="2"><h3>Insert Image</h3></td>
</tr>
<tr><form method="post" ENCTYPE="multipart/form-data" name="theForm">
<td>Image File</td>
<td><input type=file size="40" name=image></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit"
value="Upload"></td>
</tr>
</form>
</table>
<?php
if(!$_FILES)die();
require_once('./pathtodatabaseconnect.php');

$iname = $_FILES['image']['name'];

//Suck that file into a buffer
ob_start();
$contents = readfile($_FILES['image']['tmp_name']);
$dump = ob_get_contents();
ob_end_clean();


$data = unpack("H*hex", $dump);
$buf = $data['hex'];

//Check to see what we got it the dB already
$query = "select id from $database.imagetable where id = '$iname'";
$result = mysql_query($query) or die();
$line = mssql_fetch_array($result);

if($line == "") {
$query = "insert into $database.imagestable (id, data) values
('$iname', 0x$buf)";
$status = "inserted into";
} else {
$query = "UPDATE $database.robert.imagetable set data = 0x$buf where
id = '$iname'";
$status = "updated in";
}
mysql_query($query) or die("error during query: $query");

?>
<dd>Image was <?echo $status;?> database
<dd>Image Uploaded
<dd><img src="GetImage.php?image=<?=$iname;?>">

</body>
</html>
===SNIP END===

Code to read images to screen

NOTE: This code is expecting to be called from an IMG tag: <img
src="phpfile.php?image=1">
===SNIP START===
<?php
require_once('./pathtodatabaseconnect.php');
function getImage($data) {
if(substr($data, 0, 3) == "GIF") return "gif";
if(substr($data, 0, 2) == "BM") return "bitmap";
if(substr($data, 6, 4) == "JFIF") return "jpeg";
if(substr($data, 1, 3) == "PNG") return "png";
if(substr($data, 0, 2) == "II" || substr($data, 0, 2) == "MM") return
"tiff";
return 1;
}
extract($_GET);
$query = "SELECT data from $database.imagetable where id = '$image'";
$result = mysql_query($query) or die();
$data = mysql_result($result,0);
$len = strlen($data);
$type = getImage($data);
header("Content-type: $type");
header("Content-length: $len");
echo $data;
?>
===SNIP END===

NOTE: Loading images from a dB is really lame! I tried this (as above
code shows) and while it does work, it is extreamly slow and taxing on
your server unessecarly. I would suggest have a directory called
"images" and having a list of that directory in the database, that
would be much faster!

Example:
===SNIP START===
<img src="getImage.php?image=1">
<?php //this is are fake "getImage.php" file ;)
require_once('pathtodatabasefile.php');
function getImage($data) {
if(substr($data, 0, 3) == "GIF") return "gif";
if(substr($data, 0, 2) == "BM") return "bitmap";
if(substr($data, 6, 4) == "JFIF") return "jpeg";
if(substr($data, 1, 3) == "PNG") return "png";
if(substr($data, 0, 2) == "II" || substr($data, 0, 2) == "MM") return
"tiff";
return 1;
}
$query="SELECT name from $database.imagetable WHERE value =
".$_GET['image'].";";
$name = mysql_result(mysql_query($query),0);
$buf=readfile("./images/$name");
$len=strlen($buf);
$type=getImage($buf);
header("Content-type: $type");
header("Content-length: $len");
echo $buf;
===SNIP END===

Wow, hope you can digest all that!

Cheers,
hackajar (at not spam, please don't spam) gmail (PLEASE NO SPAM) com
eholz1 wrote:
Hello Members,

I am setting up a photo website. I have decided to use PHP and MySQL.
I can load jpeg files into the table (medium blob, or even longtext)
and get the image(s) to display without a problem. I am using
chunk_split(data) and the base64_encode and base64_decode on the files.

I do a select from the database, and then echo the image (with
header(Content Type: image/jpeg)
and the decoded image displays fine. Yes, I have tried header(Content
Type: image/png), without
success.

My problem is png images. Is there something that I need to do
different in terms on reading/encodeing/decodeing the png image? I am
putting its "binary" data into the db table.
But when I do a select on a png image - it never displays in the
browser.

I am not using file/path names as references, but actually putting the
image in the db.

Here is a sample of code I use to insert the data into the table, and
to select the code from the table.

load image(s):
<?php
while ($file = readdir($dir_handle))
{
$filetyp = substr($file, -3);
if ($filetyp == 'png' OR $filetyp == 'jpg')
{
$handle = fopen($path . "/" . $file,'r');
$file_content = fread($handle,filesize($path . "/" . $file));
fclose($handle);

$encoded = chunk_split(base64_encode($file_content));
$sql = "INSERT INTO images SET sixfourdata='$encoded'";
mysql_query($sql);
}
}
?>

display images:

<?php
$result = @mysql_query("SELECT * FROM images WHERE id=" . $img . "");

if (!$result)
{
echo("Error performing query: " . mysql_error() . "");
exit();
}

while ( $row = mysql_fetch_array($result) )
{
$imgid = $row["id"];
$encodeddata = $row["sixfourdata"];
}
?>

<?php
//echo base64_decode($encodeddata);
echo $encodeddata;
?>

This process seems to always work with jpegs.

Thanks

ewholz

.



Relevant Pages

  • Re: readdir() doesnt maintain order
    ... What do you need to have after foreach() to ensure that the following TWO statements are inside the loop? ... In my attempts using readdir it would read then echo, ... //Open images directory ...
    (comp.lang.php)
  • Re: [PHP] corrupt image when viewed using PHP
    ... You can find different solutions at the PHP Manual web site; ... We have written an app in PHP4 that receives images from mobile phones that are taken with the camera, we have found a problem that some of the images ... are corrupt and we have determined that it is because of Nokia VGA cameras that have sent the image, basically the jpegs are fine but the extra info that comes with the jpeg is corrupt, therefore PHP wont open the image, with all other photos its fine. ... We also found another possible solution being that we use ImageMagick to convert the jpeg to a jpeg, ...
    (php.general)
  • RE: [PHP] Problems with images..
    ... Subject: SV: [PHP] Problems with images.. ... Thank you for your wonderful sarcasm Todd. ... As for my solution not working--for BLOBs, ...
    (php.general)
  • Re: Creating captions for jpegs
    ... extracted by another program like PHP and displayed with the image. ... naming conventions, simple indexed data files, peeking at the EXIF data ... easy re-ordering, adding/deleting the images. ... little) and without any server side programming. ...
    (comp.graphics.apps.photoshop)
  • Hide the real URL
    ... I want to hide the real URL to my images by masking it with PHP ... echo ""; ...
    (php.general)