|
|
|
1) MySQL Table
To create thumbnails online out of MySQL-BLOB's i'm using the following table-structure:
| CREATE TABLE tbl_files (
file_ID int(11) NOT NULL auto_increment,
file_date date default NULL,
file_title varchar(50) default NULL,
file_text varchar(255) default NULL,
file_data longblob,
file_type varchar(50) default NULL,
PRIMARY KEY (file_ID),
UNIQUE KEY id (file_ID)
) TYPE=MyISAM; | |
The image-data itself is stored in file_data, the image-Type in file_type.
The other fields contain additional information about the file - these fields are optional. There are a lot of tutorials and samples how to get images and other files uploaded and stored into a BLOB-field, so i will not describe this here.
2) getthumb.php file
You need to have gd-library installed!!!
You will need a php-file which will be used everytime you want to create a thumbnail.
This file is called with an url-argument named ID (see 3)).
| <?php
// Place the code to connect your Database here
// DATABASE CONNECTION
global $id;
// Check if ID exists
if(!is_numeric($id)) die("No image with the ID: ".$id);
// Get data from database
$dbQuery = "SELECT file_data, file_type ";
$dbQuery .= "FROM tbl_files ";
$dbQuery .= "WHERE file_ID = $id ";
$dbQuery .= "LIMIT 1";
$result = mysql_query($dbQuery);
// read imagetype + -data from database
if(mysql_num_rows($result) == 1) {
$fileType = mysql_result($result, 0, "file_type");
$fileContent = mysql_result($result, 0, "file_data");
header("Content-type: $fileType");
// get originalsize of image
$im = imagecreatefromstring($fileContent);
$width = imagesx($im);
$height = imagesy($im);
// Set thumbnail-width to 100 pixel
$imgw = 100;
// calculate thumbnail-height from given width to maintain aspect ratio
$imgh = $height / $width * $imgw;
// create new image using thumbnail-size
$thumb=ImageCreate($imgw,$imgh);
// copy original image to thumbnail
ImageCopyResized($thumb,$im,0,0,0,0,$imgw,$imgh,ImageSX($im),ImageSY($im));
// show thumbnail on screen
$out = ImagejpeG($thumb);
print($out);
// clean memory
imagedestroy ($im);
imagedestroy ($thumb);
}
?> | |
3) Calling getthumb.php from any other file
To show a thumbnail from an image (for example with file_ID = 17) you just have to
place the following code in your page:
| | <img src='getthumb.php?id=17> | |
I hope this will help anyone to get the problem solved. Please contact me for mistakes or else.
... and sorry for my bad english :-)
tommi |
|
| Functions for loading images into a MySQL database and displaying them. Categories : Graphics, HTML and PHP, MySQL, PHP, Databases | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | Automatically printing the contents of an sql table in MySQL. Categories : MySQL, PHP, HTML and PHP, Databases | | | Pull Down Surfing - Surf on Change Categories : Java Script, MySQL, HTML and PHP, PHP, Databases | | | Dynamically generated pop-ups (Select items) Categories : PHP, HTML and PHP, MySQL, Databases | | | Alternating background color for HTML table rows Categories : PHP, Databases, MySQL, HTML and PHP | | | Editing the virtusertable and sendmail.cw via PHP3.0 and Mysql Categories : MySQL, HTML and PHP, PHP, Databases | | | This function will populate the options in a drop down HTML select list
in a form from a database query.
Categories : MySQL, General SQL, PHP, HTML and PHP, Databases | | | How can i Preload a 'SELECT MULTIPLE'? Categories : HTML and PHP, PHP, MySQL, Databases | | | Record Set Paging with PHP (RSP) Categories : PHP, MySQL, Navigation, Databases, HTML and PHP | | | dynamic table columns Categories : PHP, HTML and PHP, Arrays, Databases, MySQL | | | The simple counter with use MySql and gd. Categories : MySQL, HTTP, Graphics, PHP, Databases | | | mySQL/PHP/search with multientry
form and table output with colored rows Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases | | | Paginating the mySQL data Categories : PHP, Algorithms, Databases, MySQL, HTML and PHP | |
| | | | sandra vega wrote : 1055
Hi,
your script was helpfull, but I still have a problem:
the thumbnail shows itself with the wrong set of colors. Any tip?
Thanks a lot!
Sorry for my english, too. Maybe you can talk spanish, so:
Tu código me resultó muy bueno, pero la imagen thumbnail que consigo aparece con una profundidad de color mucho menor a la de la imagen original (digamos, como si a una foto color la quisiera transformar en un gif de 8 colores o algo así)
¿tendrás idea de por qué?
Gracias!
Sandra
| | | | nebojsa tomcic wrote :1067
use function ImageCreateTrueColor() instead of ImageCreate()
| |
|
|
|