我发现这个在线脚本会从图像中创建一个缩略图,但缩略图图像的质量很差,我怎样才能提高图像的质量。
是否有更好的方法来创建缩略图,如果是这样,您可以指导我如何使用PHP创建缩略图的教程。
以下是代码。
<?php
function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth)
{
$srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");
$origWidth = imagesx($srcImg);
$origHeight = imagesy($srcImg);
$ratio = $origWidth / $thumbWidth;
$thumbHeight = $origHeight / $ratio;
$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight);
imagejpeg($thumbImg, "$thumbDirectory/$imageName");
}
createThumbnail("images", "pic.jpg", "images/thumbs/", 180);
?>
答案 0 :(得分:1)
使用imagecopyresampled()代替imagecopyresized()。检查PHP的doc以了解此函数的用法。
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight);