improve quality of PHP GD generated images

时光毁灭记忆、已成空白 提交于 2019-11-29 19:51:52

问题


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.

The generated image is:

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);

?>

回答1:


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);

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);

 

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.




回答2:


The loss of quality is beacause of JPEG Compression (which is alossy compression algorithm).

If you want best quality use PNG instead of jpeg.

header('Content-Type: image/png');
imagepng($canvas);

And for a map generator I would recommend PNG as there are many solid color areas which will be compressed quite a lot by PNG.

Only think of using JPEG if the size of PNG images is unacceptably large.
In that case as jeremy said use the quality argument of imagejpeg..

imagejpeg($canvas, NULL, $quality);

I would experiment with various qualities to find a suitable size quality trade-off. Personally i have found a quality of 90 to be acceptable in most cases, but you can up it to 100 if you want to.



来源:https://stackoverflow.com/questions/7421200/improve-quality-of-php-gd-generated-images

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!