PHP中的图像调整大小问题 - gd会创建丑陋的调整大小的图像

时间:2009-10-07 19:22:21

标签: php image thumbnails

我正在使用以下函数

从我的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);

但问题是由此产生的图像质量非常差,当我与Adobe Photo shop执行相同的操作时,它会执行良好的转换。为什么会这样?我无法找到任何质量参数,通过它我可以改变输出图像的质量。

5 个答案:

答案 0 :(得分:25)

答案 1 :(得分: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;

}

答案 2 :(得分:1)

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

答案 3 :(得分:0)

您可能还想查看Image_Transform PEAR package。它为您处理了许多低级细节,使得创建和maniuplating图像变得无痛。它还允许您使用GD或ImageMagick库。我在几个项目中使用它取得了巨大的成功。

答案 4 :(得分:0)

尝试使用php.Thumbnailer

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

结果照片为48x48px。容易吗? :)