how to create thumbnail from image in mysql php?

Guna

New member
Greetings to all..

Am trying to create a thumbnail from image stored in mysql database..

this is my coding for creating thumbnail..

<?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);
}
?>

This coding works like wat i need.. but the problem is the image clarity is not as the original image..

i tried replacing $out = imagejpeg($thumb); with $out = imagejpeg($thumb,$fileType,100); but it prints 1 not the image..

Pls help me to solve this problem
Hi guys i found the answer for the above problem..........

use $out = imagejpg($thumb, NULL, 100);
print $out;

//since the second data is file name of the image, since we are displaying the image directly, no need for image name, just put it as null... thats it
 
Back
Top