【问题标题】:PHP jpeg image created from file is low quality从文件创建的 PHP jpeg 图像质量低
【发布时间】:2020-08-14 22:06:06
【问题描述】:

此代码从数组中随机生成图像。它工作正常,但图像被严重压缩。有没有办法保持质量?

$img = array('1.jpg', '2.jpg', '3.jpg' ); // Adds images into an array
$i = rand(0, count($img)-1); // Generates a random number (max being the last elements index from the array)
$chosenimg = "$img[$i]"; // Sets the variable $chosenimg to equal to whatever the random image was
$image = imagecreatefromjpeg($chosenimg);
imagealphablending($image, false);
imagesavealpha($image, true);
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);

【问题讨论】:

  • 图像质量的默认值为 75。要将其更改为 100(最佳质量),您需要将 imagejpeg($image); 更改为 imagejpeg($image, NULL, 100);

标签: php image compression


【解决方案1】:

检查official documentation

bool imagejpeg ( resource $image [, mixed $to [, int $quality ]] )

因此您必须将imagejpeg($image); 更改为imagejpeg($image, null, 100); 以获得最佳质量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 2017-09-16
    • 2021-07-03
    • 2014-04-09
    • 2013-07-18
    相关资源
    最近更新 更多