更改PHP GD库以提高图像质量

We implemented a parametric image generator that creates images that contain texts with dynamic sizes using the PHP GD Library.

In order to create a printable image, we need the increase the image quality to 500 DPI.

Can anyone give us guidance on how to extend this?

imagejpegDocs 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 imagecreatedocs 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 imagecreatetruecolordocs 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.