Inserting images into MySQL using Perl

I had a database that listed all the images on my system. The problem was, I wanted to be able to have a web page that would show thumbnail images of the original. I didn't want to create a second image in the directory. I got to digging around and discovered that most places on the web just dealt with uploading an image, then creating the thumbnail. Nothing seemed to piece everything together.
I'll just include the relevant parts. I used Perl for populating the database. PHP for retrieving the data.
Populating the database:
#!/usr/bin/perl -w
use DBI;      			# for the mysql connectivity
use Image::Magick;   		# for manipulating the images

I'll let you figure out the other stuff.
$width=200;			# max width for thumbnail
$height=200;			# max height for thumbnail
$file='somefile.jpg';		# File you are making the thumbnail of
$image=Image::Magick->new;  	# create the image object
open (IMAGE,$file);		
$image->Read($file);		# read the file into object.
close (IMAGE);
$w_image=$image->Get('width');	# get original image width
$h_image=$image->Get('height'); # get original image height
	or die "unable to connect";

Okay, you now have an object called $image that is your original image. You get the current width and height. You're going to now figure out if it's portrait or landscape, then figure out the resize. Assuming the image is a normal sized image, the following will calculate a new width/height. It won't work too well if you have an oddball sized image
if ($w_orig < $h_orig){ 
	$width=$height/$h_orig*$w_orig;    # new width 
else { 
	$height=$width/$w_orig*$h_orig;    # new height
$image->Scale(width=>"$width",height=>"$height");	# now resize the image

The next part is what I was missing. What to do with '$image'. If you just wanted to display, you could do something like:
 binmode STDOUT;
 $image->Write('jpg: -');
That would display to the browser, but I want to throw the image in the database.
You need to have a 'blob' type for 1 field in your table. I used 'pics' for table name. I had setup a bunch of fields that had various attributes of the original. '$pic_id' was set earlier for the index number of the pic I'm working with. 'pic_thumb' is the blob field that will hold the thumbnail image.
 @blob_image=$image->ImageToBlob();  # this will put your thumbnail in an array.
 @newblob=$dbh->quote(@blob_image);  # this method will add quotes to the blob to keep mysql happy.  
 			             #not sure if you could get away with 'blob_image' on both sides. 
 $query="update pics set pic_thumb=@newblob where pics_index=$pic_id;
That's the basics of adding images to a database.
For viewing in PHP, it's pretty easy.
I have a page, the 'img' tag looks like this:
<img src=pic.php?num=4>
This will call pic.php and ask for image #4.
Do your query for the thumb for that image. then
Header ("Content-type:image/jpeg");
echo $pic;
Alternately, if you want to add
header ('Content-disposition: attachment; filename="filename.jpg"');
Just prior to the echo, then if the person saves the file, it will use filename.jpg instead of 'pic.php'. As part of the query, you could grab the original file name and use that instead of 'filename'

Here's a page for help with PHP/MySQL and javascript with Google maps
If this helped, feel free to drop me an email and let me know.

Donate towards my web hosting bill!
email me: roger AT with any questions