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);

但问题是生成的图像质量很差,当我用 Adob​​e Photo shop 执行相同的操作时,它执行了很好的转换.. 为什么会这样?我找不到任何质量参数,通过它我改变输出图像的质量..

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

您也可以考虑使用 ImageMagick ( http://us3.php.net/manual/en/book.imagick.php ) 而不是 Gd。几天前,我在使用 Java 时遇到了同样的问题。使用 ImageMagick 而不是 Java Advanced Images 会导致巨大的质量差异。

于 2009-10-07T19:31:51.007 回答
0

您可能还想查看Image_Transform PEAR 包。它为您处理了许多低级细节,并使创建和操作图像变得轻松。它还允许您使用 GD 或 ImageMagick 库。我已经在几个项目中成功地使用了它。

于 2009-10-07T19:33:47.977 回答
0

尝试使用php.Thumbnailer吗?

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

结果照片将为 48x48 像素。容易吧?:)

于 2011-03-04T12:08:41.640 回答