By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,606 Members | 3,802 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,606 IT Pros & Developers. It's quick & easy.

Loading png images into a mysql table/database

P: n/a
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

Dec 22 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a

eholz1 schrieb:
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.
Just a small performance note:

If you use blobs and various other types, MySQL is likely to fall back
in table-scan mode which might slow down your queries very much. A
table-scan needs to read the full table data, at least much more, than
an index.

Even if you use some indexes, MySQL might not use them - the exact
behaviour depends on your indexes and queries.

Are there good reasons to keep image data in tables and not in file
systems - other than simpler coding, e.g.?

Dec 22 '06 #2

P: n/a
Very True, I use to run a photo hosting web site and I experimented
with this in the beginning and oh man once there was 100,000+ images in
there the MySQL DB slowed down quite significantly. Almost 3 gigs of
data in a MySQL table is not the way to do it, I have found the file
system does a much better job holding and serving images.

What I ended up doing is just creating a simple mod 30 hash folder
setup to store all the actual photos and then have a table to store all
the attributes of the photos. This improved the performance very
significantly over the pure MySQL setup. Also separating the photos
into 30 different folders helped in case I actually had to do any
folder scans, it would not need to scan though all the photos just
1/30th of them.

I hope this helps.
Nick D.

On Dec 21, 10:57 pm, "seaside" <seaside...@mac.comwrote:
eholz1 schrieb:
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.Just a small performance note:

If you use blobs and various other types, MySQL is likely to fall back
in table-scan mode which might slow down your queries very much. A
table-scan needs to read the full table data, at least much more, than
an index.

Even if you use some indexes, MySQL might not use them - the exact
behaviour depends on your indexes and queries.

Are there good reasons to keep image data in tables and not in file
systems - other than simpler coding, e.g.?
Dec 22 '06 #3

P: n/a
Hello Nick and Seaside (an den See!!),

The only reason I have for putting the image data in the table is for
learning and fun(??).
but it does seem like a better idea (now) to use a file system scheme,
like Nick mentions.

I will do a little research on the term "mod 30 hash folder" (i am
fairly new to this!!), and see what I can do with it. I may need to
get back and ask more questions.

Thanks for the good info,

eholz1 aka ewholz
Nick DeNardis wrote:
Very True, I use to run a photo hosting web site and I experimented
with this in the beginning and oh man once there was 100,000+ images in
there the MySQL DB slowed down quite significantly. Almost 3 gigs of
data in a MySQL table is not the way to do it, I have found the file
system does a much better job holding and serving images.

What I ended up doing is just creating a simple mod 30 hash folder
setup to store all the actual photos and then have a table to store all
the attributes of the photos. This improved the performance very
significantly over the pure MySQL setup. Also separating the photos
into 30 different folders helped in case I actually had to do any
folder scans, it would not need to scan though all the photos just
1/30th of them.

I hope this helps.
Nick D.

On Dec 21, 10:57 pm, "seaside" <seaside...@mac.comwrote:
eholz1 schrieb:
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.Just a small performance note:
If you use blobs and various other types, MySQL is likely to fall back
in table-scan mode which might slow down your queries very much. A
table-scan needs to read the full table data, at least much more, than
an index.

Even if you use some indexes, MySQL might not use them - the exact
behaviour depends on your indexes and queries.

Are there good reasons to keep image data in tables and not in file
systems - other than simpler coding, e.g.?
Dec 22 '06 #4

P: n/a
eholz1 wrote:
Hello Nick and Seaside (an den See!!),

The only reason I have for putting the image data in the table is for
learning and fun(??).
but it does seem like a better idea (now) to use a file system scheme,
like Nick mentions.
I'd say go for the filesystem based solution too.

....but good on you for trying the DBMS route yourself before asking for
help.

Have you tried anything other than viewing the PNG in a browser? This is a
fairly easy think to try first - but if it doesn't work, try downloading
the file instead - does it have the same size? Does it load as a PNG image
from the filesystem? What's the diff like?

What you described should be completely reversible - if it works for JPEG,
it should work for PNG.

HTH

C.
Dec 22 '06 #5

P: n/a

seaside schrieb:
eholz1 schrieb:
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.

Just a small performance note:
Additionally, here is a bit more on MySQL:

- The query cache of MySQL is OFF by default. On a production system,
you should turn it on,
Since this might return significant better performance.

- You should keep all your queries in a separate module and call
function of this module from everywhere in your web-app.

Note, that MySQL only uses the query cache, if queries are absolutely
identical. Even if one character is lower-case in one query and
upper-case in the other, MySQL assumes them to be different. Same
applies to white-space.

Dec 23 '06 #6

P: n/a
eholz1,
What I mean by the "mod 30" hash folder is basically what I did was
setup 30 folders numbered (0-29) inside a directory. Then when ever a
photo is uploaded it would go to a temporary area then its primary key
would be modded by 30 to get a number between 0 and 29 then the photo
would be placed in that folder. So like this:

1. User uploads photo to temp directory.
2. Info is grabbed from the photo.
3. The information is inserted into the database.
4. The primary key is modded by 30, so $folder = ($primary_key % 30);
5. The photo is moved into that $folder.
6. A thumbnail is also created in the $folder/thumbs/ directory.

The things that would be stored in the database about the image are the
filename, hash folder so it does not have to be recalculated, original
width, original height, thumbnail width, thumbnail height, filetype,
filesize, and other things related to the user but nothing really too
crazy is stored in the database.

I hope this helps.
Nick D.

On Dec 22, 4:03 pm, "eholz1" <ewh...@gmail.comwrote:
Hello Nick and Seaside (an den See!!),

The only reason I have for putting the image data in the table is for
learning and fun(??).
but it does seem like a better idea (now) to use a file system scheme,
like Nick mentions.

I will do a little research on the term "mod 30 hash folder" (i am
fairly new to this!!), and see what I can do with it. I may need to
get back and ask more questions.

Thanks for the good info,

eholz1 aka ewholz

Nick DeNardis wrote:
Very True, I use to run a photo hosting web site and I experimented
with this in the beginning and oh man once there was 100,000+ images in
there the MySQL DB slowed down quite significantly. Almost 3 gigs of
data in a MySQL table is not the way to do it, I have found the file
system does a much better job holding and serving images.
What I ended up doing is just creating a simple mod 30 hash folder
setup to store all the actual photos and then have a table to store all
the attributes of the photos. This improved the performance very
significantly over the pure MySQL setup. Also separating the photos
into 30 different folders helped in case I actually had to do any
folder scans, it would not need to scan though all the photos just
1/30th of them.
I hope this helps.
Nick D.
On Dec 21, 10:57 pm, "seaside" <seaside...@mac.comwrote:
eholz1 schrieb:
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.Just a small performance note:
If you use blobs and various other types, MySQL is likely to fall back
in table-scan mode which might slow down your queries very much. A
table-scan needs to read the full table data, at least much more, than
an index.
Even if you use some indexes, MySQL might not use them - the exact
behaviour depends on your indexes and queries.
Are there good reasons to keep image data in tables and not in file
systems - other than simpler coding, e.g.?
Dec 23 '06 #7

P: n/a
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
Dec 25 '06 #8

P: n/a
ewholz wrote:

Thanks Nick, I will give this a try. I like it, and it is much easier
than loading the binary image data in the db. (although loading binary
images was educational and fun!!!)

Thanks,

ewholz
!
Nick DeNardis wrote:
eholz1,
What I mean by the "mod 30" hash folder is basically what I did was
setup 30 folders numbered (0-29) inside a directory. Then when ever a
photo is uploaded it would go to a temporary area then its primary key
would be modded by 30 to get a number between 0 and 29 then the photo
would be placed in that folder. So like this:

1. User uploads photo to temp directory.
2. Info is grabbed from the photo.
3. The information is inserted into the database.
4. The primary key is modded by 30, so $folder = ($primary_key % 30);
5. The photo is moved into that $folder.
6. A thumbnail is also created in the $folder/thumbs/ directory.

The things that would be stored in the database about the image are the
filename, hash folder so it does not have to be recalculated, original
width, original height, thumbnail width, thumbnail height, filetype,
filesize, and other things related to the user but nothing really too
crazy is stored in the database.

I hope this helps.
Nick D.

On Dec 22, 4:03 pm, "eholz1" <ewh...@gmail.comwrote:
Hello Nick and Seaside (an den See!!),

The only reason I have for putting the image data in the table is for
learning and fun(??).
but it does seem like a better idea (now) to use a file system scheme,
like Nick mentions.

I will do a little research on the term "mod 30 hash folder" (i am
fairly new to this!!), and see what I can do with it. I may need to
get back and ask more questions.

Thanks for the good info,

eholz1 aka ewholz

Nick DeNardis wrote:
Very True, I use to run a photo hosting web site and I experimented
with this in the beginning and oh man once there was 100,000+ images in
there the MySQL DB slowed down quite significantly. Almost 3 gigs of
data in a MySQL table is not the way to do it, I have found the file
system does a much better job holding and serving images.
What I ended up doing is just creating a simple mod 30 hash folder
setup to store all the actual photos and then have a table to store all
the attributes of the photos. This improved the performance very
significantly over the pure MySQL setup. Also separating the photos
into 30 different folders helped in case I actually had to do any
folder scans, it would not need to scan though all the photos just
1/30th of them.
I hope this helps.
Nick D.
On Dec 21, 10:57 pm, "seaside" <seaside...@mac.comwrote:
eholz1 schrieb:
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.Just a small performance note:
If you use blobs and various other types, MySQL is likely to fall back
in table-scan mode which might slow down your queries very much. A
table-scan needs to read the full table data, at least much more, than
an index.
Even if you use some indexes, MySQL might not use them - the exact
behaviour depends on your indexes and queries.
Are there good reasons to keep image data in tables and not in file
systems - other than simpler coding, e.g.?
Dec 25 '06 #9

P: n/a
And hackajar - thanks for the handy sample code!!!
Maybe I get smart now!!!

ewholz,
ha******@gmail.com 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
Dec 26 '06 #10

P: n/a
ha******@gmail.com 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


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');
Include a type calumn
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:
(Top posting fixed)

It was probably slow becasue your code is so inefficient.

Some suggestions:

1. Include the type of file as a column of the table.
2. Include the length as another column.
3. Don't use file names. Rather, use an autoincrement column for an id.
Make that id the primary key and load the image by id.
4. There is no need to use ob_start/ob_end_clean. Rather, fopen() the
file (in binary mode) and fread() it into the buffer.

These changes will speed things up a lot.

Also, your content-type is incorrect. It should be "image/gif",
"image/bitmap", etc.

I use images from a database all the time, and they work fine. They
also have the advantage that if you're using innodb tables, you can
guarantee the integrity of the database (what happens if an image file
is deleted, for instance?)

P.S. Please don't top post.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Dec 27 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.