2009-10-07 15 views
7
私は次の関数

画像PHPに問題のサイズを変更 - gdが醜いリサイズ画像

/*creates thumbnail of required dimensions*/ 
function createThumbnailofSize($sourcefilepath,$destdir,$reqwidth,$reqheight,$aspectratio=false) 
{ 
    /* 
    * $sourcefilepath = absolute source file path of jpeg 
    * $destdir = absolute path of destination directory of thumbnail ending with "/" 
    */ 
    $thumbWidth = $reqwidth; /*pixels*/ 
    $filename = split("[/\\]",$sourcefilepath); 
    $filename = $filename[count($filename)-1]; 
    $thumbnail_path = $destdir.$filename; 
    $image_file = $sourcefilepath; 

    $img = imagecreatefromjpeg($image_file); 
    $width = imagesx($img); 
    $height = imagesy($img); 

    // calculate thumbnail size 
    $new_width = $thumbWidth; 
    if($aspectratio==true) 
    { 
     $new_height = floor($height * ($thumbWidth/$width)); 
    } 
    else 
    { 
     $new_height = $reqheight; 
    } 

    // create a new temporary image 
    $tmp_img = imagecreatetruecolor($new_width, $new_height); 

    // copy and resize old image into new image 
    imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

    // save thumbnail into a file 

    $returnvalue = imagejpeg($tmp_img,$thumbnail_path); 
    imagedestroy($img); 
    return $returnvalue; 
} 

を使用して、私のPHPスクリプトから一定の高さと幅のサムネイルを作成してい

を作成し、私は次のパラメータ

createThumbnailofSize($sourcefilepath,$destdir,48,48,false); 
でこの関数を呼び出します

問題は、結果として得られる画像が非常に品質が悪いことです.Adobe Photo Shopで同じ操作を実行すると、変換は良好です。なぜそうですか?私は出力イメージの品質を変更するための品質パラメータを見つけることができません。

答えて

25

imagecopyresized()の代わりにimagecopyresampled()を使用してください。

+2

imagecopyresampled()は、2次立方体のサイズ変更アルゴリズム – Jacco

+0

を使用しています。ありがとう、この関数を試してみましょう –

+2

その他の重要な点:いくつかのサムネイルが完全に黒い場合。この問題は、いくつかの余分な文字を追加するカメラや携帯電話から発生する可能性があります。この問題を回避するには、これを追加します: 'ini_set(" gd.jpeg_ignore_warning "、1);'。次に、この警告「JPEGデータの破損:マーカーの前に2つの余分なバイト0xd9」が表示されますが、サムネイルは正しく生成されます。 – Toto

1

それはあなたがimagejpeg使用して画像を保存するときに、品質パラメータを与える必要が後にある画質であれば($ tmp_img、$ thumbnail_pathを、100)//デフォルト値は75

/*creates thumbnail of required dimensions*/ 
function 
createThumbnailofSize($sourcefilepath,$destdir,$reqwidth,$reqheight,$aspectratio=false) 
{ 
    /* 
    * $sourcefilepath = absolute source file path of jpeg 
    * $destdir = absolute path of destination directory of thumbnail ending with "/" 
    */ 
    $thumbWidth = $reqwidth; /*pixels*/ 
    $filename = split("[/\\]",$sourcefilepath); 
    $filename = $filename[count($filename)-1]; 
    $thumbnail_path = $destdir.$filename; 
    $image_file = $sourcefilepath; 

$img = imagecreatefromjpeg($image_file); 
$width = imagesx($img); 
$height = imagesy($img); 

// calculate thumbnail size 
$new_width = $thumbWidth; 
if($aspectratio==true) 
{ 
    $new_height = floor($height * ($thumbWidth/$width)); 
} 
else 
{ 
    $new_height = $reqheight; 
} 

// create a new temporary image 
$tmp_img = imagecreatetruecolor($new_width, $new_height); 

// copy and resize old image into new image 
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

// save thumbnail into a file 

$returnvalue = imagejpeg($tmp_img,$thumbnail_path,100); 
imagedestroy($img); 
return $returnvalue; 

}

です
+0

Qualityパラメータを増やすだけでは役に立ちません。再サンプリングすると品質が向上する – rjha94

1

Gdの代わりにImageMagick(http://us3.php.net/manual/en/book.imagick.php)を使用することもできます。私は2,3日前にJavaと同じ問題を抱えていました。 Java Advanced Imagesの代わりにImageMagickを使用すると、という巨大なの品質の違いが発生します。

0

Image_Transform PEAR packageをご覧ください。それはあなたのための低レベルの詳細の多くを処理し、イメージを作成し、痛みのないmaniuplating作る。また、GDまたはImageMagickライブラリを使用することもできます。私はいくつかのプロジェクトで大きな成功を収めています。

0

php.Thumbnailerを試しましたか?

$thumb=new Thumbnailer("photo.jpg"); 
$thumb->thumbSquare(48)->save("thumb.jpg"); 

結果写真は48x48pxになります。簡単? :)

+0

リンクが壊れていると思う –

関連する問題