Dynamic Thumbnail From Database Solution!
| From: | Jason Bell | Date: | Tue, 24 Jul 2001 00:37:56 +0000 |
| Subject: | Dynamic Thumbnail From Database Solution! | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-59281@lists.php.net to get a copy of this message | ||
Ok, since I've posted questions about this on a few occasions, I thought I'd be kind, and
share the solution I have found to creating dynamic thumbnails from images stored within a MySQL
database. For the sake of brevity, I will only show an example for jpeg...
To begin with, add your image to your Database. I opted to only pretreat my binary data with the
addslasshes command.
Once your image is in the database, you need to be able to pull it out in a normal fashion. The
getpic.php script handles this:
<?php
if($id) {
mysql_connect("$DBHOST","$DBUSER","$DBPASS");
mysql_select_db("$DBNAME");
$query = "select image,type from images where id=$id";
$result = mysql_query($query);
$data = mysql_result($result,0,"image");
$type = mysql_result($result,0,"type");
Header( "Content-type: $type");
echo $data;
};
?>
Using getpic.php in conjuction with the <IMG> tag can display the fullsize picture.
Now, to the fun part! Dynamicly creating a thumbnail from the fullsize binary within your database.
To create the thumbnail, I have a script called mkthumb.php:
<?php
$src = imagecreatefromjpeg("http://www.barkingrat.com/photo/getpic.php?id=$id");
$twidth = imagesx($src)/3;
$theight = imagesy($src)/3;
$img = imagecreate($twidth,$theight);
imagecopyresized($img,$src,0,0,0,0,$twidth,$theight,imagesx($src),imagesy($src));
imagejpeg($img);
imagedestroy($img);
?>
You use mkthump.php in the same exact way that you use getpic.php. Now, obviously, mkthumb.php
needs to be modified to handle filetypes other than jpeg. That should be a simple task, I just
haven't done it yet.... should be as simple as checking the file type (I insert the type into a
column when I store the original image)
this works..... if anyone can think of a better way to do anything I have done, feel free to add
your 2 cents.... I'm certainly open to it. :)
Jason Bell