display image stored in BLOB field using php – PHP

  php

Q(Question):

hi

i have stored user profile images in an BLOB field.,

and need to display it as an thumbnail image in their profile page.,

and i searched through the net and got the result using header functions as

follows.,


<?
include 'config.php';
function displayimage($data)
{
$type='image/pjpeg';
Header( "Content-type: $type");
$size = 150; // new image width
$src = imagecreatefromstring($data);
$width = imagesx($src);
$height = imagesy($src);
$aspect_ratio = $height/$width;
if ($width <= $size) {
$new_w = $width;
$new_h = $height;
} else {
$new_w = $size;
$new_h = abs($new_w * $aspect_ratio);
}
$img = imagecreatetruecolor($new_w,$new_h);
imagecopyresized($img,$src,0,0,0,0,$new_w,$new_h,$width,$height);
imagejpeg($img);
imagedestroy($img);
}
//displaying image from database
$query = "select content from photo where pid = 1";
$result = @mysql_query($query);
$data = @mysql_result($result,0,"content");
displayimage($data);
?>

which results in displaying a perfect thumb image..

but when i try to display the image after some other execution of php code., i,e

for example if i insert an tag


<? echo "image here";?>

before displaying the image.,

i get an error message

"Warning: Cannot modify header information – headers already sent by (output started at D:\xampp\htdocs………..(the line where the echo statement comes"

how to get over this…

any ideas..

regards
vijay

A(Answer):

You simply cannot do that. When you send binary image data to the browser, that’s all it can take. What are you trying to accomplish here?

A(Answer):

i just want to display the image stored in my database(BLOB) as the profile picture for the users..

A(Answer):

do you know how HTML displays pictures?

A(Answer):

yes ofcourse., it displays images using <img> tag….

A(Answer):

got your way..

sent the variable inside the src tag and displayed the image by printing the image in image.php page…


<img src="image.php?data=1" />

A(Answer):

What Dormilich was actually trying to say is HTML uses something like inline frames to display the images the source attribute doesn’t actually mean the actual source data. You would need to use a PHP file to display the images using GET variables then just place the path to the PHP file.

LEAVE A COMMENT