Jump to content

trying to load images from mySQL


sputnik577

Recommended Posts

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.
[code]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;
}[/code]
Link to comment
Share on other sites

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="...">  ;)
Link to comment
Share on other sites

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....
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.