Loading png images into a mysql table/database



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: Loading png images into a mysql table/database
    ... I have decided to use PHP and MySQL. ... I can load jpeg files into the table ... I do a select from the database, and then echo the image (with ... header(Content Type: image/jpeg) ...
    (comp.lang.php)
  • Re: 4.1 build errors
    ... I re-untarred and built it & it built fine. ... But it doesn't seem to spit out a png image to a PHP page, ... A command fileyielded a png image on a PHP page using cygwin's ...
    (comp.graphics.apps.gnuplot)
  • Re: Show w-mail address as an Image
    ... "Carlos Marangon" wrote in message ... > but to avoid span the e-mail must be shown as a png image. ... or blank line before first <?php tag ...
    (comp.lang.php)
  • Re: Hot I can generate `Faces
    ... imagine that I want to create an aplication, i.e. in PHP which convert ... an PNG image in a image string that Face use, ...
    (comp.emacs)
  • Re: How to insert a .php image?
    ... Here you can see the contents of my .php file and also ... >maybe a php image 'gallery'...or png image?? ...
    (microsoft.public.frontpage.client)