从文件创建的PHP jpeg图像质量低

时间:2021-09-27 21:21:00

This script is used to randomly select a background whenever you access the site. I do this by setting background-image in my stylesheet to this php file

此脚本用于在您访问站点时随机选择背景。我这样做是通过在我的样式表中将background-image设置为这个php文件

It works fine, but the image is compressed heavily. Is there a way to keep the quality?

它工作正常,但图像被大量压缩。有没有办法保持质量?

<?php

  $img = array('dx1.jpg', 'dx2.jpg', 'dx3.jpg' ); // Adds the names of all images into an array

  $i = rand(0, count($img)-1); // Generates a random number (max being the last element's index from the array)

  $chosenimg = "$img[$i]"; // Sets the variable $chosenimg to equal to whatever the random image name was (from the above array)

  $image = imagecreatefromjpeg($chosenimg);

  imagealphablending($image, false);
  imagesavealpha($image, true);

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


  imagejpeg($image);
  imagedestroy($image);

?>

1 个解决方案

#1


2  

Checking the official documentation :

查看官方文档:

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

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

so you have to change imagejpeg($image); with imagejpeg($image, null, 100); to get the best quality.

所以你必须改变imagejpeg($ image); with imagejpeg($ image,null,100);获得最好的质量。

#1


2  

Checking the official documentation :

查看官方文档:

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

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

so you have to change imagejpeg($image); with imagejpeg($image, null, 100); to get the best quality.

所以你必须改变imagejpeg($ image); with imagejpeg($ image,null,100);获得最好的质量。