7

次の関数を使用して、PHPスクリプトから固定の高さと幅のサムネイルを作成しています

/*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;
}

そして私はこの関数を次のパラメータで呼び出します

createThumbnailofSize($sourcefilepath,$destdir,48,48,false);

しかし、問題は、結果の画像の品質が非常に悪いことです。AdobePhotoショップで同じ操作を実行すると、良好な変換が実行されます。なぜそうなのですか?出力画像の品質を変更するための品質パラメータが見つかりません。

4

5 に答える 5

25

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

于 2009-10-07T19:24:54.813 に答える
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;

}

于 2009-10-07T19:27:59.673 に答える
1

Gdの代わりにImageMagick( http://us3.php.net/manual/en/book.imagick.php )を使用することも検討できます。ほんの数日前にJavaで同じ問題が発生しました。Java Advanced Imagesの代わりにImageMagickを使用すると、品質に大きな違いが生じます。

于 2009-10-07T19:31:51.007 に答える
0

Image_TransformPEARパッケージも確認することをお勧めします。それはあなたのために多くの低レベルの詳細を処理し、画像の作成と操作を簡単にします。また、GDまたはImageMagickライブラリのいずれかを使用できます。私はいくつかのプロジェクトで大成功を収めてそれを使用しました。

于 2009-10-07T19:33:47.977 に答える
0

php.Thumbnailerを使用してみましたか?

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

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

于 2011-03-04T12:08:41.640 に答える