【问题标题】:Image resize issue in PHP - gd creates ugly resized imagesPHP 中的图像调整大小问题 - gd 创建难看的调整大小的图像
【发布时间】:2010-12-04 18:06:07
【问题描述】:

我正在使用以下函数从我的 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 执行相同的操作时,它执行了很好的转换.. 为什么会这样?我找不到任何质量参数,通过它我可以改变输出图像的质量..

【问题讨论】:

    标签: php image thumbnails


    【解决方案1】:

    【讨论】:

    • imagecopyresampled() 使用双三次调整算法
    • 非常感谢,让我试试这个功能
    • 其他要点:如果某些缩略图完全是黑色的。问题可能来自某些添加了一些额外字符的相机和手机。为避免此问题,请添加:ini_set("gd.jpeg_ignore_warning", 1);。然后你只会得到这个警告Corrupt JPEG data: 2 extraneous bytes before marker 0xd9 但缩略图会正确生成。
    【解决方案2】:

    如果是图像质量,则需要在使用 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;
    

    }

    【讨论】:

    • 只是增加质量参数没有帮助。重采样提高质量
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        尝试使用php.Thumbnailer

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

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

        【讨论】:

          猜你喜欢
          • 2017-12-09
          • 1970-01-01
          • 2012-08-12
          • 2010-11-03
          • 1970-01-01
          • 1970-01-01
          • 2013-04-04
          • 2011-02-18
          • 1970-01-01
          相关资源
          最近更新 更多