test_imagedb_create.php

<? 

$dbserver = "localhost";
      $dbuser   = "root";
      $dbpass   = "";
      $dbname   = "em";
      
$dbconn = @mysql_connect($dbserver,$dbuser,$dbpass) or exit("SERVER Unavailable"); 
@mysql_select_db($dbname,$dbconn) or exit("DB Unavailable"); 

$sql = "SELECT image_type,image FROM em.testblob WHERE id =". $_GET["id"]; 

$result = @mysql_query($sql,$dbconn) or exit("QUERY FAILED!"); 

$contenttype = @mysql_result($result,0,"imgtype"); 
$image = @mysql_result($result,0,"imgdata"); 

header("Content-type: $contenttype"); 
echo $image; 

mysql_close($dbconn); 
?>

MySQL table
db = em
table = testblob

CREATE TABLE testblob (
  image_id tinyint(3) NOT NULL AUTO_INCREMENT,
  image_type varchar(25) NOT NULL,
  image longblob NOT NULL,
  image_size varchar(25) NOT NULL,
  image_ctgy varchar(25) NOT NULL,
  image_name varchar(50) NOT NULL,
  KEY image_id (image_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Listing.php

<?php

include 'dbc.php';

$query  = "SELECT  *  FROM em.testblob";
$data = mysql_query("SELECT * FROM em.testblob") or die(mysql_error()); 

$result = mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_NUM))
{
    echo 
         
         "<img src=\"test_imagedb_create.php?id=".$row[0]."\" width=80 height=65>".
                           
         "<img src=\"test_imagedb_create.php?id=".$row[1]."\" width=80 height=65>".
                                    
         "<img src=\"test_imagedb_create.php?id=".$row[2]."\" width=80 height=65>".
         
         "<img src=\"test_imagedb_create.php?id=".$row[2]."\" width=80 height=65>".
                  
         "<img src=\"test_imagedb_create.php?id=".$row[3]."\" width=80 height=65>".
                  
         "<img src=\"test_imagedb_create.php?id=".$row[4]."\" width=80 height=65>".
                                    
         "<img src=\"test_imagedb_create.php?id=".$row[5]."\" width=80 height=65>".
                 
         "<br>___________________________________________________________<BR /><BR />";    
         

} 


?>

my results are shown in this image link below
http://i30.tinypic.com/o0bcr6.jpg


i can't display the image stored in MySQL database....anyone please guide me. i have been working on this like forever.

anyone can help?

header("Content-type: $contenttype"); 
header("Content-disposition: inline");
header("Content-type: $contenttype"); 
header("Content-disposition: inline");

tried to add in the above into my image processor file, but doesn't works.

the image file is injected successfully into MySQL, i know its good cos i tried testing it with two images of different file size.
from phpymyadmin, i can see the longblob size.

<? $dbserver = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "em";
$dbconn = @mysql_connect($dbserver,$dbuser,$dbpass) or exit("SERVER Unavailable"); 
@mysql_select_db($dbname,$dbconn) or exit("DB Unavailable"); 
$sql = "SELECT image FROM em.testblob WHERE id =".$_GET["id"]; 
$result = @mysql_query($sql,$dbconn) or exit("QUERY FAILED!"); 
$image = @mysql_result($result,0,"imgdata"); 
$output = imagecreatefromstring($image);
header("Content-type: image/png"); 
header("Content-disposition: inline"); 
imagepng($output);
imagedestroy($ouput);
mysql_close($dbconn); ?>

not sure, untested, I only store file system
see aslo imagejpeg, imagegif, imagewbmp functions
if required test original content-type and output using the appropriate function

commented: pls save me... +1
<? $dbserver = "localhost";
$dbuser  = "root";
$dbpass = "";
$dbname = "em";
$dbconn = @mysql_connect($dbserver,$dbuser,$dbpass) or exit ("SERVER Unavailable"); 
@mysql_select_db($dbname,$dbconn) or exit("DB Unavailable"); 
$sql = "SELECT image_type,image FROM em.testblob WHERE id =".$_GET["id"]; 
$result = @mysql_query($sql,$dbconn) or exit("QUERY FAILED!"); 
$image = @mysql_result($result,0,"imgdata"); 
$output = imagecreatefromstring($image);
header("Content-type: image/png"); 
header("Content-disposition: inline"); 
imagepng($output);
imagedestroy($ouput);
mysql_close($dbconn); ?>

this demo outputs everything as png.
if required check original content-type and output that image type, Im lazy

i got this thing to work on php4 before but some functions deprecated and it doesn't works now...duh


before i try your suggestion, can i clear some of my doubts?

sorry my noobiness, but does your method means i need to upload the image file in png format?

or does it convert the existing jpg image to output as png?

i got this thing to work on php4 before but some functions deprecated and it doesn't works now...duh

before i try your suggestion, can i clear some of my doubts?

sorry my noobiness, but does your method means i need to upload the image file in png format?

or does it convert the existing jpg image to output as png?

it outputs the image as png at the moment,
its a demo/ thought provoker not a solution yet
you could use the original content-type as a switch
--ignoring syntax-- I'm sleepy its midnight --

switch($image_type}
case 'image/gif':
 imagegif ();//etc
 break;
case 'image/jpeg' :
 imagejpeg ();//etc
 break;
case 'image/bmp': 
 imagewbmp ();//etc
 break;

thanks, i will try that now.

have a good sleep~

:P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.