Php 质量更好的缩略图

Php 质量更好的缩略图,php,image,thumbnails,Php,Image,Thumbnails,我有一个脚本可以成功地创建缩略图,但它们的渲染质量很低。脚本中是否有我可以更改的内容,以提高缩略图的质量 function createThumbnail($filename) { $final_width_of_image = 200; $path_to_image_directory = 'images/fullsized/'; $path_to_thumbs_directory = 'images/thumbs/'; if(preg_match('/[.

我有一个脚本可以成功地创建缩略图,但它们的渲染质量很低。脚本中是否有我可以更改的内容,以提高缩略图的质量

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;   
}
函数create缩略图($filename){
图像的最终宽度=200;
$path_to_image_目录='images/fullsize/';
$path_to_thumbs_目录='images/thumbs/';
if(preg_匹配('/[.](jpg)$/',$filename)){
$im=imagecreatefromjpeg($path\u to\u image\u directory.$filename);
}else if(preg_匹配('/[.](gif)$/',$filename)){
$im=imagecreatefromgif($path\u to\u image\u directory.$filename);
}else if(preg_匹配('/[.](png)$/',$filename)){
$im=imagecreatefrompng($path\u to\u image\u directory.$filename);
}
$ox=imagesx($im);
$oy=imagesy($im);
$nx=图像的$final\u width\u;
$ny=地板($oy*($final_width_of_image/$ox));
$nm=ImageCreateTureColor($nx,$ny);
imagecopyresized($nm、$im、0,0,0、$nx、$ny、$ox、$oy);
如果(!file_存在($path_到_thumbs_目录)){
如果(!mkdir($path\u to\u thumbs\u directory)){
死(“出现问题,请再试一次!”);
}
}
imagejpeg($nm,$path\u to\u thumbs\u directory.$filename);
$tn='';
$tn.='
祝贺您。您的文件已成功上载,缩略图已创建。“; echo$tn; }
我对这段代码唯一的建议是:

  • 增加图像的
    $final\u width\u以生成更大的缩略图
  • 将第三个
    quality
    参数添加到
    imagejpeg
    ;据了解,它的范围从0到100,其中100是最好的质量
  • 不要在缩略图中使用JPEG

  • 使用
    imagecopyresampled
    获得更好的像素插值算法

imagecopyresampled
提供了更好的结果(并且确实使用了前面提到的
imagejpeg
的质量参数。

可以尝试使用
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);
}

谢谢!我把它改成了
imagejpeg($nm,$path_to_thumbs_directory.$filename,100);
我看到了质量上的一点差异,但是
imagecopyresampled
确实起到了作用!:)脚本实际上使用了零来裁剪原始图像。更正,它没有裁剪图像。只需更改宽度:(
imagecopyresampled
太棒了。