retrieving images from database and thumbnailing
Posted: Fri Jul 25, 2003 9:35 pm
I'm new at php/mySql and i'm trying to create a site with images stored in
the mySql database. I looked around for code/tutorials and patched a few together to get the following code:
Unfortunately this gives me all sorts of errors. I have been able to just download images from the db and display them(at full size), as well as using the thumbnail script with images on the server (but not in a db). Basically my problem is in the transition between retrieving the image (stored as a BLOB in the mySql table) and resizing it. I am assuming it has something to do with the file being stored as a binary file, but i have no clue how to bridge the gap. It's quite possible the solution is very simple, but like I said i'm a n00b.
any ideas????
i realize that it might be easier to just store the images on the server and put file paths in the db, but thats not any fun nw is it?
the mySql database. I looked around for code/tutorials and patched a few together to get the following code:
Code: Select all
<?php
#------------------------------------------------------------------
# DATABASE IMAGE THUMBNAILER
#
# Retrieves specified image from mysql database, resizes it, and
# outputs it as an image file.
#------------------------------------------------------------------
#------------------------------------------------------------------
# HEADER
#------------------------------------------------------------------
header("Content-type: image/jpeg");//header
#------------------------------------------------------------------
# DATA FIELDS
#------------------------------------------------------------------
$max_width = 485; // Max Thumbnail width
$max_height = 368; // Max Thumbnail height
$id = $_GET['id']; //Get ID of photo
if($id == null){ $id = 1; } //make sure there is a value for ID
#------------------------------------------------------------------
# MSQL DATABASE
#------------------------------------------------------------------
# connect to database #
include 'mysql_config.php';//include mysql login information
$dbcx = mysql_connect($localhost,$username,$password)
or die("There was an error connecting to your database!<BR>");
@mysql_select_db($image_db,$dbcx)
or die("there was an error selecting the database!<BR>" . mysql_error());
# query database #
$result = mysql_query("SELECT image FROM images WHERE id = $id")
or die("there was an error reading data!<BR>" . mysql_error());
$row=mysql_fetch_object($result);
#------------------------------------------------------------------
# IMAGE PROCESSING
#------------------------------------------------------------------
# create image file from database file #
$srcimage = imagecreatefromjpeg($row->image);
# obtain height & width information #
$width = imageSX($srcimage);
$height = imageSY($srcimage);
$w_h_ratio = $width / $height;//width-to-height ratio
$h_w_ratio= $height / $width;//height-to-width ratio
# determine how large thumbnail should be #
if (($w_h_ratio * $max_height) <= $max_width) {
$new_width = $w_h_ratio * $max_height;
$new_height = $max_height;
} else {
$new_width = $max_width;
$new_height = $h_w_ratio * $max_width;
} //end if/else
# create thumbnail image #
$destimage = imagecreatetruecolor($new_width,$new_height); //create blank truecolor image
imagecopyresized($destimage,$srcimage,0,0,0,0,$new_width,$new_height,$width,$height);
// copy original image into blank image (resized)
ImageJPEG($destimage, '', 100); // display image
ImageDestroy($destimage);
?>any ideas????
i realize that it might be easier to just store the images on the server and put file paths in the db, but thats not any fun nw is it?