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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kevin7
Forum Commoner
Posts: 96
Joined: Fri May 21, 2004 6:54 am

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

Post 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:
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
kevin7
Forum Commoner
Posts: 96
Joined: Fri May 21, 2004 6:54 am

Post by kevin7 »

ooo... it's work~!!! thx~
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply