Page 1 of 1

hmm... jpeg convertion quality was bad!!!

Posted: Wed Jun 02, 2004 6:42 am
by kevin7
recently, i hv reference a turtorial of learning making thumbnail...
yeah~ it is work... but the color of the picture was lost... (grey-scale?!)...

The following is the picture i convert...
Image

and the following is what i've converted..
Image

hmm... i wonder how can i enhance the "quality"... i new with GD library... :(

the following is the code i learn...

Code: Select all

<? 
header("Content-type: image/jpeg"); 

$new_width=100;  //Image width Change if needed 
$new_height=200;  //Image height Change if needed 

$source_path="";   //Source File path 
$destination_path="thumb/";     //Destination file path 

$db = mysql_connect("localhost", "root") or die("Cannot Connect");   // Database Connection 

mysql_select_db("test",$db) or die("Cannot open database"); //Database Name 

$sql = mysql_query("SELECT * FROM images") or die("Query failed");  //Query 

while ($row = mysql_fetch_array($sql)) 
&#123; 
    echo "$row&#1111;0]<BR>";

    $image_name = $row&#1111;0];  //Image path retrived 

    //Identifying Image type 

    $len = strlen($image_name); 
    $pos =strpos($image_name,"."); 
    $type = substr($image_name,$pos + 1,$len); 

    if ( $type=="jpeg" || $type=="jpg") 
    &#123; 
        thumb_jpeg ($image_name); //Call to jpeg function 
    &#125; 

    echo "<b>Done........</b>"; 
         
&#125; 

//JPEG function 
function thumb_jpeg($image_name) 
&#123; 
    global $source_path; 
    global $destination_path; 

    global $new_width; 
    global $new_height; 

    $destimg=ImageCreate($new_width,$new_height) or die("Problem In Creating image"); 

    $srcimg=ImageCreateFromJPEG($source_path.$image_name) or die("Problem In opening Source Image"); 

    ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die("Problem In resizing"); 

    ImageJPEG($destimg,$destination_path.$image_name,100) or die("Problem In saving"); 
&#125; 



?>
the script... the code must place in the same folder with the picture, and u need to create a mysql table named "images" and a column name "image_path" (as shown in the code)... and the table should insert the name of the jpg picture...

hmm.. can anyone give me the way to enhance the quality?

tq.... :wink: :) :D :lol:

Posted: Wed Jun 02, 2004 7:25 am
by redmonkey
Have you read the FAQs over on the GD site? (I suspect not). The short of it is that if you are using GD2 then you should be using the imagecreatetruecolor() function and not the imagecreate() function.

Posted: Wed Jun 02, 2004 8:30 am
by kevin7
ooo... it's work~!!! thx~

Posted: Wed Jun 02, 2004 10:14 am
by pickle
Also, if you have GD 2.01 or greater, use [php_man]imagecopyresampled[/php_man] rather than [php_man]imagecopyresized[/php_man]. It gives you a much better quality thumbnail. Would you mind posting the improved thumbnail?