display image stored in BLOB field using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vjayis
    New Member
    • Mar 2008
    • 134

    display image stored in BLOB field using php

    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.,

    Code:
    <?
    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
    Code:
    <? 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
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    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?

    Comment

    • vjayis
      New Member
      • Mar 2008
      • 134

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

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        do you know how HTML displays pictures?

        Comment

        • vjayis
          New Member
          • Mar 2008
          • 134

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

          Comment

          • vjayis
            New Member
            • Mar 2008
            • 134

            #6
            got your way..

            sent the variable inside the src tag and displayed the image by printing the image in image.php page...
            Code:
            <img src="image.php?data=1" />

            Comment

            • philipwayne
              New Member
              • Mar 2010
              • 50

              #7
              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.

              Comment

              Working...