Flying Dutch Boy
Flying Dutch Boy

Reputation: 354

PHP display image from Database(medium blob)

I currently have a database where i can store images in.. But the PHP page i created to display them... doesnt work. But when i use the link as scr for a < img> it shows a empty image frame. if i echo the image directly, i get a lot of strange signs

the way i want to display the images is: < img src="image.php?image="'.$imageID.'/>

the code i use for displaying the image(image.php) is:

 <?php session_start();
 if(!isset($_GET['image']))
 {
     header("HTTP/1.0 404 Not Found");
 }
 else{
     $link = mysql_connect('localhost', '45345345435435345', 'wewfsersdfds');
            if (!$link) {
                echo "error";
                die;
            }

            $db_selected = mysql_select_db(Tha base, $link);
            if (!$db_selected) {
                echo "error";
                die;
            }
 $nummer=(int)trim(stripslashes($_GET['image']));
 $mynr=$nummer;
 $sql="SELECT * FROM Foto WHERE fotonr=$mynr";
 $result=mysql_query($sql);
 if(!$result)
 {
     echo "error".mysql_error();
     header("HTTP/1.0 404 Not Found");
 }
 else
 {

     $foto = mysql_fetch_assoc($result);

     header("Content-type: ".$foto['type']);
     echo $foto['image'];


 }
 }
 ?>

I tried a lot already but it wont work :( i updated the code to the newest version. Made mistakes with the sql(selected nothing/never do mysql_real_escape_string of a int)

Now it shows a straight line of characters, instead of an image, thats at least a improvement...

Can someone help me?

Thanx for your time!

Upvotes: 3

Views: 9716

Answers (3)

Rupesh Arora
Rupesh Arora

Reputation: 577

$sql = "SELECT * FROM Foto WHERE fotonr=$mynr";
        $result = $conn -> query($sql);
        while ($row = $result -> fetch_assoc()) {
        $img= '<img src="data:image/jpeg;base64,'.base64_encode(                        $row['image'] ).'"  width=350px0px height=300px/>';
}
echo $img;

Upvotes: 0

md5madman
md5madman

Reputation: 306

Honestly, I know it can be tricky. I think it has to do with how your storing it, not how your outputting it. I have one example of storage from a recent project I can show.

$fp = fopen($image_path, 'r');
$image = fread($fp, filesize($image_path));
$image = addslashes($image);
fclose($fp);

// save $image into DB.

Not sure if you do similar with file_get_contents. How I output is similar to how you are doing it, only I use a ORM and a framework.

Upvotes: 2

Fletch
Fletch

Reputation: 963

I suspect that your problem is in your conversion - perhaps between strings and images, or perhaps between the different image formats.

Have you tried saving the image gotten through image.php, and comparing the binary against the known-good image file? Perhaps once you do that, the answer will be apparent (e.g. wrong length, corrupted header, etc.)

You may want to consider a different database record. Most db's support storage of binary blob data, which you could more simply return. This would be simpler (and take less space in your db!) than using the php functions to stringify and imagecreatefromstring.

Also, I believe that you're attempting to use imagegif, imagejpeg, imaging to perform image format conversion for you. This isn't how I understand them to work, based on my reading at: http://php.net/manual/en/function.imagecreatefromstring.php. Try storing and retrieving the same format to tease out whether this is your problem.

Try looking at Content-type. I think it's case sensitive. Try Content-Type. http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

Upvotes: 2

Related Questions