更高质量的缩略图 [英] Better quality thumbnails
本文介绍了更高质量的缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个成功创建缩略图的脚本,但它们的渲染质量很低.是否可以在脚本中更改某些内容以提高缩略图的质量?
I have a script that successfully creates thumbnails but they're rendered low quality. Is there something that I can change in the script to make the thumbnails better quality?
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;
}
推荐答案
我只能用这段代码建议:
The only things I can suggest with this code are:
- 增加
$final_width_of_image
以制作更大的缩略图. - 向
imagejpeg
添加第三个quality
参数;根据PHP手册,范围从0到100,100是最好的质量. - 不要将 JPEG 用于缩略图.
- 使用
imagecopyresampled
获得更好的像素插值算法.
- Increase the
$final_width_of_image
to make a larger thumbnail. - Add the third
quality
argument toimagejpeg
; according to the PHP manual, it ranges from 0 to 100 with 100 being the best quality. - Don't use JPEG for your thumbnails.
- Use
imagecopyresampled
to get a better pixel interpolation algorithm.
这篇关于更高质量的缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文