Connecting Tech Pros Worldwide Forums | Help | Site Map

display image from mysql(blob data)

Newbie
 
Join Date: Dec 2007
Posts: 11
#1: Jul 4 '08
HI all,
I am able to upload the image as blob to mysql. but while displaying the image i cant display it properly .
The code used for uploading image to mysql inserts data into mysql table.The uploading code is:
Expand|Select|Wrap|Line Numbers
  1. MYSQL_CONNECT("localhost","root","");
  2.     mysql_select_db("sample");
  3.  
  4.     $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
  5.  
  6.     $result=MYSQL_QUERY("INSERT INTO PHOTO(description,bin_data,filename,filesize,filetype) ".
  7.         "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");
  8.  
The code used for displaying the image is as follows
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     MYSQL_CONNECT("localhost","root","");
  3.     mysql_select_db("sample");
  4.  
  5. $id=2;
  6.  
  7.  $query = "select filename,filetype,filesize,bin_data from PHOTO where id=$id";
  8.  
  9. $result = mysql_query($query) or die('Error, query failed');
  10. //$ROW = mysql_fetch_assocc($result);
  11. list($name, $type, $size, $content) = mysql_fetch_array($result);
  12.  
  13. echo '<meta http-equiv="Content-Type" content="img/jpg" />';
  14. echo $content;
  15. exit;
  16.  
  17. ?>
  18.  
This code displays some random characters all over the browser but not the image. I think i have been missing some statements .
Thank you
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#2: Jul 4 '08

re: display image from mysql(blob data)


You have to set the headers properly.

Read this tutorial. (scroll down to the part about downloading.)
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 407
#3: Jul 4 '08

re: display image from mysql(blob data)


this might help u

[PHP]$query = "SELECT Doc,Doc1,Doc2,Name,Name1,Name2 FROM xyz WHERE id = '".$id."'";

$result = mysql_query($query) or die('Error, query failed');
list($Doc,$Doc1,$Doc2,$Name,$Name1,$Name2) = mysql_fetch_array($result);

//header("Content-length: $size");
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
//header("cache-Control: no-cache, must-revalidate"); // HTTP/1.1 changed on 27th july commented this and next line and addedprevious 3 lines for IE caching prob
//header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=$Name");
echo $Name;
echo $Doc;
[/PHP]
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#4: Jul 4 '08

re: display image from mysql(blob data)


Quote:

Originally Posted by pradeepjain

this might help u

[PHP]$query = "SELECT Doc,Doc1,Doc2,Name,Name1,Name2 FROM xyz WHERE id = '".$id."'";

$result = mysql_query($query) or die('Error, query failed');
list($Doc,$Doc1,$Doc2,$Name,$Name1,$Name2) = mysql_fetch_array($result);

//header("Content-length: $size");
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
//header("cache-Control: no-cache, must-revalidate"); // HTTP/1.1 changed on 27th july commented this and next line and addedprevious 3 lines for IE caching prob
//header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=$Name");
echo $Name;
echo $Doc;
[/PHP]

He's not trying to download a pdf.
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 407
#5: Jul 8 '08

re: display image from mysql(blob data)


Hey really srry ...i did not notice tht,,this is wht wll work for him

[PHP]
error_reporting(E_ALL);
$link = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());

mysql_select_db("xyz") or die(mysql_error());

$sql = "SELECT Pic FROM abc WHERE ID='".$_GET['Id']."'";
//'".$_GET['Id']."'";

$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());

header("Content-type: image/jpeg");
$test=mysql_result($result,0);

$desired_width = 85;
$desired_height = 110;

$im = imagecreatefromstring($test);
$new = imagecreatetruecolor($desired_width, $desired_height);

$x = imagesx($im);
$y = imagesy($im);

imagecopyresampled($new, $im, 0, 0, 0, 0, $desired_width, $desired_height, $x, $y);

imagedestroy($im);
header("cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('Content-type: image/jpeg');
imagejpeg($new, NULL, 85);

imagedestroy($new);
// echo $test;

mysql_close($link);
[/PHP]
Reply