提高PHP GD生成图像的质量 [英] improve quality of PHP GD generated images
问题描述
我将开始使用GD库在PHP中构建地图生成器。我使用lib生成了一些图像,但它们没有很好的质量。我只是想知道有一些方法可以提高图像的质量。
i am going to start building a map generator in PHP using the GD library. i generated some images using the lib but they don't have a good quality. I just want to know that is there some way to improve the quality of the images.
生成的图像是:
和代码i make是:
and the code i made is:
<?php
$canvas = imagecreate(800, 350);
imagecolorallocate($canvas, 255, 255, 255);
$pink = imagecolorallocate($canvas, 255, 105, 180);
$white = imagecolorallocate($canvas, 255, 255, 255);
$green = imagecolorallocate($canvas, 132, 135, 28);
imagestring( $canvas, 20, 290, 25, "Quality is not the best :(", $green );
function drawlinebox($x1, $y1, $x2, $y2, $height, $color){
global $canvas;
imagesetthickness ( $canvas, 1 );
for ($i=1; $i < $height; $i++){
imageline( $canvas, $x1, $y1, $x2, $y2, $color );
$y1++; $y2++;
}
}
drawlinebox(20, 20, 780, 300, 30, $green);
drawlinebox(20, 300, 780, 20, 30, $pink);
header('Content-Type: image/jpeg');
imagejpeg($canvas);
imagedestroy($canvas);
?>
推荐答案
imagejpeg
[docs ] 对图像质量进行了论证。默认为 75
,但您可以将其增加到最高 100
。例如:
imagejpeg
[docs] takes an argument for image quality. It defaults to 75
, but you can increase it up to a maximum of 100
. For example:
imagejpeg($canvas, NULL, 90);
然而,f或者生成的图形有很多连续的颜色和清晰的线条,JPEG可能不是最好的选择。 PNG更适合这些类型的图像,并且可能会以更小的尺寸为您提供完美的质量。 imagepng
[ docs ] 有几个选项,但默认值应该没问题:
However, for generated graphics with lots of continuous colours and sharp lines, JPEG is probably not the best choice. PNGs are more well-suited to these sorts of images, and will probably give you perfect quality at a smaller size. imagepng
[docs] has a few options, but the defaults should be fine:
header('Content-Type: image/png');
imagepng($canvas);
您正在使用 imagecreate
[ docs ] 首先制作图像。这会创建一个基于托盘的图像:只能使用有限数量的颜色的图像。这与GIF或8位PNG的较低质量相匹配,但由于您不使用这些格式,您应该使用 imagecreatetruecolor
[ docs ] 。到目前为止,您的图像非常简单,这可能没什么区别,但是如果您生成更复杂的图像则会很重要。
You're using imagecreate
[docs] to make the image in the first place. This creates a "pallet-based" image: one that can only use a limited number of colours. This matches the lower quality of a GIF or an 8-bit PNG, but because you're not using those formats you should use imagecreatetruecolor
[docs] instead. So far, your image is very simple and this might not make a difference, but it will matter if you're generating more complicated images.
如果您进行了这两项更改,您的图像一定会有完美的品质。
If you make these two changes, your images will be sure to have perfect quality.
这篇关于提高PHP GD生成图像的质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!