improve quality of PHP GD generated images

15,954

Solution 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.

Solution 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.

Share:
15,954
Ameer
Author by

Ameer

Updated on June 11, 2022

Comments

  • Ameer
    Ameer almost 2 years

    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:

    the img

    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);
    
    ?>
    
  • nikc.org
    nikc.org over 12 years
    Also, try imagecreatetruecolor instead of imagecreate.
  • Jeremy
    Jeremy over 12 years
    @nikc: Good suggestion, I've added that to my post.
  • Jeremy
    Jeremy over 12 years
    @Ameer: Nice, glad to have been of help. If you think your problem has been solved, you should consider marking the most helpful answer as "accepted" by clicking the grey check mark next to it.
  • Michel
    Michel over 5 years
    imagecreatetruecolor() returns black image :(