473,394 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
2 8674
JKing
1,206 Expert 1GB
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
Thanks JKing.. Thanks lot.. it works as a charm..
Oct 13 '10 #3

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

Similar topics

8
by: hemashiki | last post by:
hi i need help..how can i store image in mysql database and i want to retrive that image can anyone suggest plz
0
sashi
by: sashi | last post by:
BLOB - Retieve image stored in database Dim CN As New ADODB.Connection Dim RS As ADODB.Recordset Dim DataFile As Integer, Fl As Long, Chunks As Integer Dim Fragment As Integer, Chunk() As...
2
by: wingleader | last post by:
Hi! I'm looking for ways of uploading image file to mysql database. I know, that it should be possible, but don't know how to do it.... could someone help me?
2
by: SarangMaskikar | last post by:
hi how to save tif image in mysql database in vb.net plz guide me Sarang.
1
by: berry | last post by:
Hi all, I am scratching my head on how to store image that user upload to the MySQL database in vb6? Please HELP me!! Thank you very much..
3
rajiv07
by: rajiv07 | last post by:
Hi to all, We can store image in mysql using MEDIUMBLOG data type.I want to know how to insert or select and store an image data type using perl. Is any idea Please Thanks. Regards ...
1
by: jarremw | last post by:
hello all, i am making a database to hold some scanned pdf's so i was wondering how would i go about saving them in the mysql database? would i put the file in the database or just link to it? so if...
2
by: WellCom34 | last post by:
hey guys need a little help. i tried to save image into my database, it saves without error but when i tried to check if it saves in mysql, the data about my picture id save, but the image i save...
2
by: yusufjammy | last post by:
How to insert image in mysql database via php and how to retrieve in php ?
1
by: Gunasegar | last post by:
I need to retrieve an image from mysql database, where it is already stored as longblob format pls help me
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.