retrieving images from database and thumbnailing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amdayton
Forum Newbie
Posts: 3
Joined: Fri Jul 18, 2003 3:35 am

retrieving images from database and thumbnailing

Post by amdayton »

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:

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);
?>
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? :wink:
amdayton
Forum Newbie
Posts: 3
Joined: Fri Jul 18, 2003 3:35 am

Post by amdayton »

nevermind, i figured it out :D -- i had to use createimagefromstring() instead of createimagefromjpeg()
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Re: retrieving images from database and thumbnailing

Post by pootergeist »

amdayton wrote: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?
It might not be fun, sure is sensible though.

Your way: with db
every image needs a http request, an active db connection, a query and select - then passes the data to GD
GD works by recompiling the binary as a proprietary GD format string - this string generally runs about ten times the jpeg size for 85% compressed images - ie a 30kb jpeg makes a 300kb .gd string (which can be compressed as .gd2 which incidentally is a lossless compression algorythm). This string is then interpreted and output with its own header as a nice thumbnail.

Imagine you had a page with 10 thumbnails - all of 50kb images - we are now talking 10 database calls, 50 mb of string manipulations (50kb * 10 thumbs * 10) to output 10 thumbs of maybe 5kb each

Imagine you had 20 page views - so far we're at 1gb of string manipulation and we're still talking about a small site.

---------------------------------

Alternatively - the not so fun way [cachéd thumbnails]

created once - simply called through a http request within a img src tag
no processing, no db connections (of which you probably have a default max 300 concurrent anyway), no server resource hogginess.
tylerdurden
Forum Commoner
Posts: 66
Joined: Mon Jul 28, 2003 11:52 am
Location: Austria

Re: retrieving images from database and thumbnailing

Post by tylerdurden »

pootergeist wrote:Your way: with db
every image needs a http request, an active db connection, a query and select - then passes the data to GD
GD works by recompiling the binary as a proprietary GD format string - this string generally runs about ten times the jpeg size for 85% compressed images - ie a 30kb jpeg makes a 300kb .gd string (which can be compressed as .gd2 which incidentally is a lossless compression algorythm). This string is then interpreted and output with its own header as a nice thumbnail.

Imagine you had a page with 10 thumbnails - all of 50kb images - we are now talking 10 database calls, 50 mb of string manipulations (50kb * 10 thumbs * 10) to output 10 thumbs of maybe 5kb each

Imagine you had 20 page views - so far we're at 1gb of string manipulation and we're still talking about a small site.
I agree, creating thumbnails on the fly is madness. Why don't your store the thumbnail in the DB as well?
Post Reply