首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP-GD图像调整大小会损失图像质量

PHP-GD图像调整大小会损失图像质量
EN

Stack Overflow用户
提问于 2012-06-12 23:56:44
回答 3查看 244关注 0票数 0

我写了这段代码,如果找不到请求的图像,它会动态地重新调整图像的大小,然后存储它。

唯一的问题是输出的jpg图像质量较低。

我想知道我是否需要改变一些东西来提高图像质量。

代码语言:javascript
复制
if (isset($_GET['size'])) {
    $size = $_GET['size'];
}

if (isset($_GET['name'])) {
    $filename = $_GET['name'];
}

$filePath = "files/catalog/" . urldecode($filename);

// Content type

header('Content-Type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filePath);

if ($_GET["name"] && !$size) {
    $newwidth  = $width * $percent;
    $newheight = $height * $percent;

} else if ($_GET["name"] && $size) {
    switch ($size) {
        case "thumbs":
            $newwidth  = 192;
            $newheight = 248;
            break;
        case "large":
            $newwidth  = 425;
            $newheight = 550;
            break;
    }
}

$resizedFileName = $filename; 
$resizedFileName = str_replace(".jpg", "", $resizedFileName) . ".jpg";
$resizedFilePath = "files/catalog/" . urldecode($resizedFileName);


if (!file_exists($resizedFilePath)) {
    // Load
    $thumb  = imagecreatetruecolor($newwidth, $newheight);
    $source = imagecreatefromjpeg($filePath);

    // Resize
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    // Output
    imagejpeg($thumb, $resizedFilePath);
    //file_put_contents($, $binarydata);

    $imageContent = file_get_contents($resizedFilePath);
    echo $imageContent;

} else {
    $imageContent = file_get_contents($resizedFilePath);
    echo $imageContent;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-06-12 23:59:29

除了imagecopyresized(),您必须使用imagecopyresampled(),并且imagejpeg()函数有第三个可选参数,您可以指定quality。

票数 1
EN

Stack Overflow用户

发布于 2012-06-13 00:00:15

可能是因为您使用了错误的imagejpeg(),函数中的第二个变量代表质量百分比!

票数 0
EN

Stack Overflow用户

发布于 2012-06-13 00:00:16

你想要imagecopyresampled()。调整大小的工作方式是丢弃不必要的像素。resampled()将求出平均值,并产生更平滑的结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11000327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档