/*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で同じ操作を実行すると、変換は良好です。なぜそうですか?私は出力イメージの品質を変更するための品質パラメータを見つけることができません。
imagecopyresampled()は、2次立方体のサイズ変更アルゴリズム – Jacco
を使用しています。ありがとう、この関数を試してみましょう –
その他の重要な点:いくつかのサムネイルが完全に黒い場合。この問題は、いくつかの余分な文字を追加するカメラや携帯電話から発生する可能性があります。この問題を回避するには、これを追加します: 'ini_set(" gd.jpeg_ignore_warning "、1);'。次に、この警告「JPEGデータの破損:マーカーの前に2つの余分なバイト0xd9」が表示されますが、サムネイルは正しく生成されます。 – Toto