Page 1 of 1

Create a Thumbnail dynamicly from an image in a mysql db

Posted: Thu Nov 23, 2006 7:25 am
by amir
I'm trying to dynamicly create a thumbnail to display to a page. However the images are stored in a mysql database as a blob. How would I be able to do this. Below is the code I use to display an image from the db at normal size.

The table images has the following fields

Code: Select all

id (primary key)
productid (foreign key)
size
type
content (blob field)
any help would be very much appreciated.

Code: Select all

<?php
if(isset($_GET['id']))
{
    include '../config.php';
    include 'opendb.php';

    $id      = $_GET['id'];
    $query   = "SELECT name, type, size, image FROM images WHERE id = '$id'";
    $result  = mysql_query($query) or die('Error, query failed2');
    list($name, $type, $size, $content) = mysql_fetch_array($result);

    header("Content-Disposition: attachment; filename=$name");
    header("Content-length: $size");
    header("Content-type: $type");
    echo $content;

    include 'closedb.php';    
    exit;
}

?>

Code: Select all

<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

Code: Select all

<?php
include '../config.php';
include 'opendb.php';

$query  = "SELECT id, name FROM images";
$result = mysql_query($query) or die('Error1, query failed1');
if(mysql_num_rows($result) == 0)
{
    echo "Database is empty <br>";
}  
else
{
    while(list($id, $name) = mysql_fetch_array($result))
    {
?>
    <a href="download.php?id=<?php echo $id;?>"><?php echo $name;?></a> <br>
<?php        
    }
}
include 'closedb.php';
?>

Code: Select all

</body>
</html>
TIA!

Posted: Thu Nov 23, 2006 8:21 am
by volka
If you want to use the gd extension to create thumbnail imagecreatefromstring can create an image resource from the blob data.

p.s.: there's no need to split the script into

Code: Select all

and [syntax] blocks.

Code: Select all

<?php
if(isset($_GET['id']))
{
    include '../config.php';
    include 'opendb.php';

    $id      = $_GET['id'];
    $query   = "SELECT name, type, size, image FROM images WHERE id = '$id'";
    $result  = mysql_query($query) or die('Error, query failed2');
    list($name, $type, $size, $content) = mysql_fetch_array($result);

    header("Content-Disposition: attachment; filename=$name");
    header("Content-length: $size");
    header("Content-type: $type");
    echo $content;

    include 'closedb.php';    
    exit;
}

?>
<html>
	<head>
		<title>Download File From MySQL</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	</head>
	<body>
<?php
include '../config.php';
include 'opendb.php';

$query  = "SELECT id, name FROM images";
$result = mysql_query($query) or die('Error1, query failed1');
if(mysql_num_rows($result) == 0)
{
    echo "Database is empty <br>";
}  
else
{
    while(list($id, $name) = mysql_fetch_array($result))
    {
?>
    <a href="download.php?id=<?php echo $id;?>"><?php echo $name;?></a> <br>
<?php        
    }
}
include 'closedb.php';
?>
	</body>
</html>