简体   繁体   English

PHP中的图像调整大小问题-gd创建难看的调整大小图像

[英]Image resize issue in PHP - gd creates ugly resized images

I am creating thumbnails of fixed height and width from my PHP script using the following function 我正在使用以下功能从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;
}

and I call this function with following parameters 我用以下参数调用此函数

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

but the problem is the resulting image is of very poor quality, when I perform the same operation with Adobe Photo shop, it performs a good conversion.. why it is so? 但是问题是生成的图像质量很差,当我使用Adobe Photo shop执行相同的操作时,它转换效果很好。为什么会这样呢? I am unable to find any quality parameter, through which I change the quality of output image.. 我找不到任何质量参数,可以通过这些参数来更改输出图像的质量。

if it is image quality you are after you need to give the quality parameter when you save the image using imagejpeg($tmp_img,$thumbnail_path,100) //default value is 75 如果是图像质量,则在使用imagejpeg($ tmp_img,$ thumbnail_path,100)保存图像时需要提供quality参数后,//默认值为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;

} }

You could also consider using ImageMagick ( http://us3.php.net/manual/en/book.imagick.php ) instead of Gd. 您也可以考虑使用ImageMagick( http://us3.php.net/manual/en/book.imagick.php )代替Gd。 I had the same problem just a couple of days ago with Java. 就在几天前,我遇到了同样的问题。 Going for ImageMagick instead of Java Advanced Images resultet in a huge quality difference. 选择ImageMagick而不是Java Advanced Images会导致巨大的质量差异。

You might also want to take a look at the Image_Transform PEAR package . 您可能还想看看Image_Transform PEAR包 It takes care of a lot of the low-level details for you and makes creating and maniuplating images painless. 它为您解决了许多底层细节,使创建和处理图像变得轻松自如。 It also lets you use either GD or ImageMagick libraries. 它还允许您使用GD或ImageMagick库。 I've used it with great success on several projects. 我已经在多个项目中成功使用了它。

tried using the php.Thumbnailer ? 尝试使用php.Thumbnailer吗?

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

Result photo will be 48x48px. 结果照片将为48x48px。 Easy right? 容易吧? :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM