Jump to content


Photo

trying to load images from mySQL


  • Please log in to reply
5 replies to this topic

#1 sputnik577

sputnik577
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 September 2006 - 05:30 PM

How can i load a picture from a mySQL database using php. The picture is uploaded in a table and then dynamically loaded to a php page. It's attribute in the database table is longblob.
while(list($name, $type, $size, $path) = mysql_fetch_array($imgResult))
   {
     
    $im = open_image($path);

if ($im === false) {
        die ('Unable to open image');
}

header ('Content-Type: image/jpeg');
imagejpeg($path);
echo 'Opened image';

function open_image($path) {
        # JPEG:
        $im = @imagecreatefromjpeg($path);
        if ($im !== false) { return $im; }

        # GIF:
        $im = @imagecreatefromgif($path);
        if ($im !== false) { return $im; }

        # PNG:
        $im = @imagecreatefrompng($path);
        if ($im !== false) { return $im; }

        # GD File:
        $im = @imagecreatefromgd($path);
        if ($im !== false) { return $im; }

        # GD2 File:
        $im = @imagecreatefromgd2($path);
        if ($im !== false) { return $im; }

        # WBMP:
        $im = @imagecreatefromwbmp($path);
        if ($im !== false) { return $im; }

        # XBM:
        $im = @imagecreatefromxbm($path);
        if ($im !== false) { return $im; }

        # XPM:
        $im = @imagecreatefromxpm($path);
        if ($im !== false) { return $im; }

        # Try and load from string:
        $im = @imagecreatefromstring(file_get_contents($path));
        if ($im !== false) { return $im; }

        return false;
}


#2 Balu

Balu
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 18 September 2006 - 05:38 PM

Hi,
I am new in PHP, so I don't realy understand what you mean with that script, but when I uploading some images (by FTP functions), i put to MySQL DB only path, where the required picture is located. I also store an orgiginal filename (wallpaper3.jpg) , actual filename (file0018.jpg) which shold be generated by PHP and ID. Than just load info from DB and use <IMG SRC="...">  ;)

#3 ci5ic

ci5ic
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 18 September 2006 - 05:51 PM

I agree...

What I do is write a script that uploads the image to a specific directory, renaming it using php's date() function. Then, I insert into a database the new name of the uploaded image, as well as any other data I want to associate with the image (comments, who uploaded the image, the upload date, file size, etc.).

Then, when I want to display the image, I run a MySQL query for the information, and use the image name in the database to display the image, like such:

$imagefile = mysql_result($result,$i,"imagefile");

print "<IMG SRC='../images/$imagefile'>";

very simple....


#4 sputnik577

sputnik577
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 September 2006 - 06:40 PM

i've tried that, i've made the attribute a varchar and that would point to the file but the server said it could not find the image on the server.

#5 sputnik577

sputnik577
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 September 2006 - 07:32 PM

so now i've changed the attribute to mediumblob and i'm not getting anything.

I don't know what else to do.

Greg

#6 sputnik577

sputnik577
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 September 2006 - 08:25 PM

anybody anything, take a look at my code and see if you can see any mistakes





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users