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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gunasegar
    New Member
    • Aug 2010
    • 10

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

    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..

    Code:
    <?php 
    
      // Place the code to connect your Database here 
      // DATABASE CONNECTION 
    include('config.php');
    
    $id = $_GET['id'];
    
      // Check if ID exists 
      if(!is_numeric($id)) die("No image with the ID: " .$id); 
    
      // Get data from database 
      $dbQuery = "SELECT image, file_name "; 
      $dbQuery .= "FROM imagesdata "; 
      $dbQuery .= "WHERE id = $id "; 
      $dbQuery .= "LIMIT 1"; 
    
      $result = mysql_query($dbQuery); 
    
      // read imagetype + -data from database 
      if(mysql_num_rows($result) == 1) { 
        $file_Type = mysql_result($result, 0, "file_name"); 
        $fileContent = mysql_result($result, 0, "image"); 
    	
    	//$fileType = str_replace(".","",strtolower(substr( $file_Type,strrpos( $file_Type,"."))));
    
    	//$filename = $file_Type;
    
        header("Content-type: $fileType"); 
    
        // get originalsize of image 
        $im = imagecreatefromstring($fileContent);
        $width  = imagesx($im); 
        $height = imagesy($im);
    	
        // Set thumbnail-width to 100 pixel 
        $imgw = 150; 
    
        // calculate thumbnail-height from given width to maintain aspect ratio 
        $imgh = $height / $width * $imgw; 
    
        // create new image using thumbnail-size 
        $thumb=imagecreatetruecolor($imgw,$imgh); 
    	$filename = addslashes (file_get_contents($fileContent));
    	$image_name= stripslashes($fileContent);
        // copy original image to thumbnail 
        imagecopyresampled($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); 
      } 
    ?>
    i tried to change $out = imagejpeg($thum b); to $out = imagejpeg($thum b, $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
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    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
    Code:
    $out = imagejpeg($thumb, NULL, 100);

    Comment

    • Gunasegar
      New Member
      • Aug 2010
      • 10

      #3
      Thanks JKing.. Thanks lot.. it works as a charm..

      Comment

      Working...