WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Creating thumbnails from MySQL Blobs online
Categories : PHP, MySQL, Graphics, HTML and PHP, Databases Click here to Update Your Picture
Tommi Trinkaus
Date : Aug 07th 2002
Grade : 3 of 5 (graded 12 times)
Viewed : 16366
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Tommi Trinkaus
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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()