WEB php/mysql people v. retrieving imagedata from blob fields

Discussion in 'OT Technology' started by Mikey, Mar 8, 2009.

  1. Mikey

    Mikey This one, this form I hold now, so Wide eyed and h

    Joined:
    Oct 18, 2001
    Messages:
    23,118
    Likes Received:
    0
    Location:
    Baltimore
    so this shitty image gallery i used about 5 years ago stored all of the images in blob fields in the mysql DB. I've tried 5 different attempts to script this data into a image/jpeg header format output to grab all of the images, but it won't work!!!! Is there any easier way?
     
  2. 95vr4

    95vr4 OT Supporter

    Joined:
    Oct 6, 2004
    Messages:
    2,513
    Likes Received:
    0
    Location:
    Weddington, NC
    How are you doing it now?


    Like this?
    Code:
    //example image tag
    <img src="getimage.php?ImgID=1">
    
    //getimage.php
    if($ImgID != 0)
    {
        $Query = "Select Image from Images where ImgID = $ImgID";
        mysql_connect($DBHOST, $DBUSER, $DBPASS);
        mysql_select_db($DATABASE);
        $resultset = mysql_query($Query);
        if(mysql_affected_rows() > 0)
        {
            header("Content-type:  image/jpeg");
            print(mysql_result($resultset, 0, "Image"));
        }
    }
     
  3. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,767
    Likes Received:
    6
    can't you make a variable and use the select funtion to grab all the shit in your columns.

    then simply do a <img src="<?php echo $variable;?> />

    and do a loop

    edit: oh i missed the header part
     
  4. 95vr4

    95vr4 OT Supporter

    Joined:
    Oct 6, 2004
    Messages:
    2,513
    Likes Received:
    0
    Location:
    Weddington, NC
    that's why you call the image like <img src="getimage.php?ImgID=1">

    getimage.php script adds the correct header
     
  5. Shampoo

    Shampoo Rinse & Repost

    Joined:
    May 5, 2004
    Messages:
    60,183
    Likes Received:
    0
    Location:
    California
    Why don't you use a good one?
     
  6. Mikey

    Mikey This one, this form I hold now, so Wide eyed and h

    Joined:
    Oct 18, 2001
    Messages:
    23,118
    Likes Received:
    0
    Location:
    Baltimore
    Yea, i was attempting to use a script similar to the above with this option and for some reason something on my host is blocking me from doing it. The pain in the ass part is, I don't even need to do it on the site for any reason, I just want to download the images to my drive so I can place them in the new gallery. I was using this.....

    Code:
    <?php
    
    $dbServer = "host";
    
    $dbDatabase = "db";
    
    $dbUser = "user";
    
    $dbPass = "pass";
    
    
    
    $sConn = mysql_connect($dbServer, $dbUser, $dbPass)
    
    or die("Couldn't connect to database server");
    
    
    
    $dConn = mysql_select_db($dbDatabase, $sConn)
    
    or die("Couldn't connect to database $dbDatabase");
    
    
    $IMG_ID=$_GET['id'];
    
    $query = "SELECT imagedata from table_imagedata where iid=" . $_GET['id'];
    
    $res=mysql_query($query);
    while($o=mysql_fetch_object($res)){
      //output jpg version of the image
      header('content-type: image/jpeg');
      imagejpeg(imagecreatefromstring($o->imagedata));
      exit()
    }
    ?>
    Kept returning "server not available"
     
  7. Mikey

    Mikey This one, this form I hold now, so Wide eyed and h

    Joined:
    Oct 18, 2001
    Messages:
    23,118
    Likes Received:
    0
    Location:
    Baltimore
    a good gallery? I'm switching to a new one, but need to get all of the data from the old gallery.
     
  8. 95vr4

    95vr4 OT Supporter

    Joined:
    Oct 6, 2004
    Messages:
    2,513
    Likes Received:
    0
    Location:
    Weddington, NC
    Again, I don't think you can set the header as jpeg on the page itself, which is why I'm suggesting calling the image as an external php script.


    This is what you need to do.

    Code:
    <?php
    
    $dbServer = "host";
    
    $dbDatabase = "db";
    
    $dbUser = "user";
    
    $dbPass = "pass";
    
    
    $sConn = mysql_connect($dbServer, $dbUser, $dbPass)
    
    or die("Couldn't connect to database server");
    
    
    $dConn = mysql_select_db($dbDatabase, $sConn)
    
    or die("Couldn't connect to database $dbDatabase");
    
    $query = "SELECT iid from table_imagedata";
    
    $res=mysql_query($query);
    while($o=mysql_fetch_object($res)){
    	echo "<img src=getimage.php?id=".$o['iid'].">";
    	echo "<br />";
    }
    
    
    ?>

    getimage.php
    Code:
    $imgID=$_GET['ID'];
    
       $query = "SELECT imagedata from table_imagedata where iid=" . $imgID;
    
        mysql_connect($DBHOST, $DBUSER, $DBPASS);
        mysql_select_db($DATABASE);
        $resultset = mysql_query($Query);
        if(mysql_affected_rows() > 0)
        {
            header("Content-type:  image/jpeg");
            print(mysql_result($resultset, 0, "imagedata"));
        }
    
     
  9. 95vr4

    95vr4 OT Supporter

    Joined:
    Oct 6, 2004
    Messages:
    2,513
    Likes Received:
    0
    Location:
    Weddington, NC
    If all else fails, send me an sql dump of the table and I'll get em for u
     
  10. Mikey

    Mikey This one, this form I hold now, so Wide eyed and h

    Joined:
    Oct 18, 2001
    Messages:
    23,118
    Likes Received:
    0
    Location:
    Baltimore

    This returned a full count of red X's, which is definitely closer. For some reason it isn't grabbing the iid
     
  11. 95vr4

    95vr4 OT Supporter

    Joined:
    Oct 6, 2004
    Messages:
    2,513
    Likes Received:
    0
    Location:
    Weddington, NC
    try changing mysql_fetch_object to mysql_fetch_array
     
  12. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    are you certain that the images are JPEGS?

    Couldn't you just copy the blob data into an empty file, and save as a .JPEG?
     
  13. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    also in the code you posted, you are looping the header()-- move it just in front of the loop.
     
  14. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    If you have GD installed, try this as well:

    Code:
    //getimage.php
    
    $ImgID = isset($_GET['id']) ? intval($_GET['id']) : 0;
    if($ImgID != 0)
    {
        $Query = "Select Image from Images where ImgID = $ImgID";
        mysql_connect($DBHOST, $DBUSER, $DBPASS);
        mysql_select_db($DATABASE);
        $resultset = mysql_query($Query);
        if(mysql_affected_rows() > 0)
        {
            $sBinaryPhoto=  mysql_result($resultset, 0, "Image");
            $oSourceImage = imagecreatefromstring($sBinaryPhoto);
    
            if (!$oSourceImage) 
            { 
                  die ("Image creation failed."); 
            } 
    
            header("Content-type:  image/jpeg");
            
            imageJPEG($oDestinationImage);
    
            die('');
    
        }
    }
     

Share This Page