Jump to content

displaying an image from a MYSQL DB


webguync

Recommended Posts

Well, assuming you have the filename stored in the database...do this:

 

<?php

//get the image from the DB
$query = mysql_query("SELECT image FROM images WHERE ...")or die(mysql_error());
$row = mysql_fetch_assoc($query);

//display the image
echo "<img src='path/to/image/".$row['image']."'>";

?>

poco I think that he is talking more along the lines of storing images as binary via a BLOB field. if so let me know I have a script to do so.

 

Well, they really didn't say any of that...so I had to start somewhere. Webguync, you need to be a lot more specific if cooldude is right about what you want.

[code]I use 2 files to do this, you can see how the table is structured based on the query structure
This is the viewer
view.php?image_id=Numeric&width=Numeric
[code]
<?php
    // again we check the $_GET variable
    if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])) {
        $sql = "SELECT ImageType, ImageTitle, ImageCat, Height, Width, ImageName FROM Images WHERE ImageID=".$_GET['image_id'];
connectSQL();
        $result = mysql_query($sql)  or die("Invalid query: " . mysql_error());
         while($row=mysql_fetch_array($result)) {
            echo "<h3>".$row['ImageTitle']."</h3>";
		echo "<img src=\"image.php?image_id=".$_GET['image_id']."&width=".$_GET['width']."\" alt=\"".$row['ImageName']."\" />";
		echo "<br/>Image Name: ".$row['ImageName'];
		echo "<br/>Category: ".$row['ImageCat'];
		echo "<br/>Image Type: ".$row['ImageType'];
		echo "<br/>Width: ".$row['Width']."px Height: ".$row['Height']."px";
	}
    }
    else {
        echo 'File not selected';
    }
?>

A file Image.php is also used here is the file

<?php
$id = $_GET['image_id'];
if(!empty($_GET['width'])){
$size = $_GET['width'];
}
else{
$size = 500;
}
if($id) {
connectSQL();
    $query = "select Image, ImageType from Images where ImageID = $id";
    $result = mysql_query($query) or die(mysql_error());
    $data = mysql_result($result,0,"Image");
    $type = mysql_result($result,0,"ImageType");
$type = "image/".$type;
    Header("Content-type: ".$type);   
    $src = imagecreatefromstring($data);
    $width = imagesx($src);
    $height = imagesy($src);
    $aspect_ratio = $height/$width;

    $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);
     // determine image type and send it to the client   
    if ($type == "image/pjpeg" || $type == "image/jpg" || type == "image/jpeg") {   
      imagejpeg($img);
    } else if ($type == "image/x-png" || $type = "image/png") {
      imagepng($img);
    } else if ($type == "image/gif") {
      imagegif($img);
    }
    imagedestroy($img);
}
?>

 

 

 

Hope this gives you some ideas, this version accepts anything GD can spit back out via creating and matches image type which is very nice.  I can explain it better if you have questions, the table structure can be derived from it pretty easily.[/code][/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.