By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,827 Members | 813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,827 IT Pros & Developers. It's quick & easy.

How to create thumbnail for image stored in mysql database in php

P: 10
Greeting to all
am trying to create thumbnail from image stored in mysql database..

this is the coding am using to create the thumbnail...

this works like charm.. but the problem is that the image quality is not as in the original image..

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.  
  3.   // Place the code to connect your Database here 
  4.   // DATABASE CONNECTION 
  5. include('config.php');
  6.  
  7. $id = $_GET['id'];
  8.  
  9.   // Check if ID exists 
  10.   if(!is_numeric($id)) die("No image with the ID: " .$id); 
  11.  
  12.   // Get data from database 
  13.   $dbQuery = "SELECT image, file_name "; 
  14.   $dbQuery .= "FROM imagesdata "; 
  15.   $dbQuery .= "WHERE id = $id "; 
  16.   $dbQuery .= "LIMIT 1"; 
  17.  
  18.   $result = mysql_query($dbQuery); 
  19.  
  20.   // read imagetype + -data from database 
  21.   if(mysql_num_rows($result) == 1) { 
  22.     $file_Type = mysql_result($result, 0, "file_name"); 
  23.     $fileContent = mysql_result($result, 0, "image"); 
  24.  
  25.     //$fileType = str_replace(".","",strtolower(substr( $file_Type,strrpos( $file_Type,"."))));
  26.  
  27.     //$filename = $file_Type;
  28.  
  29.     header("Content-type: $fileType"); 
  30.  
  31.     // get originalsize of image 
  32.     $im = imagecreatefromstring($fileContent);
  33.     $width  = imagesx($im); 
  34.     $height = imagesy($im);
  35.  
  36.     // Set thumbnail-width to 100 pixel 
  37.     $imgw = 150; 
  38.  
  39.     // calculate thumbnail-height from given width to maintain aspect ratio 
  40.     $imgh = $height / $width * $imgw; 
  41.  
  42.     // create new image using thumbnail-size 
  43.     $thumb=imagecreatetruecolor($imgw,$imgh); 
  44.     $filename = addslashes (file_get_contents($fileContent));
  45.     $image_name= stripslashes($fileContent);
  46.     // copy original image to thumbnail 
  47.     imagecopyresampled($thumb,$im,0,0,0,0,$imgw,$imgh,ImageSX($im),ImageSY($im)); 
  48.  
  49.     // show thumbnail on screen 
  50.     $out = imagejpeg($thumb); 
  51.     print($out); 
  52.  
  53.     // clean memory 
  54.     imagedestroy ($im); 
  55.     imagedestroy ($thumb); 
  56.   } 
  57. ?>
  58.  
i tried to change $out = imagejpeg($thumb); to $out = imagejpeg($thumb, $fileType, 100); but it doesnt print the image...

pls help me getrit of this problem or suggest me some other coding for the above..

Thanks in Advance...

Regards,
Guna
Oct 13 '10 #1
Share this Question
Share on Google+
2 Replies


JKing
Expert 100+
P: 1,206
The second parameter is for filename not filetype. If you are outputting the jpeg directly you don't need to declare a filename and should leave it NULL.

Here is an example
Expand|Select|Wrap|Line Numbers
  1. $out = imagejpeg($thumb, NULL, 100); 
  2.  
Oct 13 '10 #2

P: 10
Thanks JKing.. Thanks lot.. it works as a charm..
Oct 13 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.