【问题标题】:Better quality thumbnails质量更好的缩略图
【发布时间】:2011-03-24 06:34:51
【问题描述】:

我有一个脚本可以成功创建缩略图,但它们的渲染质量很低。我可以在脚本中进行更改以提高缩略图的质量吗?

function createThumbnail($filename) {

    $final_width_of_image = 200;
    $path_to_image_directory = 'images/fullsized/';
    $path_to_thumbs_directory = 'images/thumbs/';

    if(preg_match('/[.](jpg)$/', $filename)) {
        $im = imagecreatefromjpeg($path_to_image_directory . $filename);
    } else if (preg_match('/[.](gif)$/', $filename)) {
        $im = imagecreatefromgif($path_to_image_directory . $filename);
    } else if (preg_match('/[.](png)$/', $filename)) {
        $im = imagecreatefrompng($path_to_image_directory . $filename);
    }

    $ox = imagesx($im);
    $oy = imagesy($im);

    $nx = $final_width_of_image;
    $ny = floor($oy * ($final_width_of_image / $ox));

    $nm = imagecreatetruecolor($nx, $ny);

    imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);

    if(!file_exists($path_to_thumbs_directory)) {
      if(!mkdir($path_to_thumbs_directory)) {
           die("There was a problem. Please try again!");
      }
       }

    imagejpeg($nm, $path_to_thumbs_directory . $filename);
    $tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';
    $tn .= '<br />Congratulations. Your file has been successfully uploaded, and a thumbnail has been created.';
    echo $tn;   
}

【问题讨论】:

    标签: php image thumbnails


    【解决方案1】:

    我可以对这段代码提出的唯一建议是:

    • 增大$final_width_of_image 以制作更大的缩略图。
    • 将第三个quality 参数添加到imagejpeg;根据the PHP manual,它的范围从 0 到 100,其中 100 是最好的质量。
    • 不要在缩略图中使用 JPEG。
    • 使用imagecopyresampled 获得更好的像素插值算法。

    【讨论】:

    • 谢谢!我将其更改为imagejpeg($nm, $path_to_thumbs_directory . $filename, 100); 我看到质量上的差异很小,但imagecopyresampled 真的很重要! :) 该脚本实际上使用零裁剪原始图像。
    • 更正,它不会裁剪图像。只是改变宽度:(
    【解决方案2】:

    imagecopyresampled 提供了更好的结果(如前所述,请务必使用imagejpeg 的质量参数。

    【讨论】:

    • imagecopyresampled 太棒了。
    【解决方案3】:

    也许试试这段代码,它使用ImageCopyResampled

    function createImage($in_filename, $out_filename, $width, $height)
    {
        $src_img = ImageCreateFromJpeg($in_filename);
    
        $old_x = ImageSX($src_img);
        $old_y = ImageSY($src_img);
        $dst_img = ImageCreateTrueColor($width, $height);
        ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $old_x, $old_y);
    
        ImageJpeg($dst_img, $out_filename, 80);
    
        ImageDestroy($dst_img);
        ImageDestroy($src_img);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-20
      • 2012-07-27
      • 2014-04-07
      • 2011-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多