t1lib(Type1 字體)jpeg-6bGD(編譯時自動偵測 jpeg)--with-gd --with-jpeg-dir --with-t1libmake clean && make && make install,重啟 Apache/PHP-FPMphpinfo() 確認 GD Support: enabled、JPEG Support: enabled現代發佈版:apt install php-gd / yum install php-gd / Docker docker-php-ext-install gd 一步到位。
| GD 版本 | GIF | PNG |
|---|---|---|
| ≤ 1.6 | ✅ 創建 | ❌ |
| > 1.6 | ❌ (專利) | ✅ 創建 |
<?php
header('Content-Type: image/png');
$im = imagecreate(250, 250);
$sky = imagecolorallocate($im, 136, 193, 255);
imagefill($im, 0, 0, $sky);
imagepng($im);
imagedestroy($im);
<?php
header('Content-Type: image/png');
$src = imagecreatefrompng('test.png');
$dst = imagecreatetruecolor(35, 35);
imagecopyresampled($dst, $src, 0,0, 0,0, 35,35, imagesx($src), imagesy($src));
imagepng($dst);
imagedestroy($src);
imagedestroy($dst);
imagecopyresampled():重採樣,質量高(雙線性插值)。imagecopyresized():最近鄰插值,速度快、質量低。imagecreatetruecolor() 支援 24bit+Alpha,imagecreate() 僅 256 色。imagealphablending($im,false); imagesavealpha($im,true); + imagecolorallocatealpha($im,0,0,0,127)。imagettftext() 需 FreeType,支援 TTF/OTF、抗鋸齒、旋轉。imagewebp($im, 'out.webp', 85) 體積減 30%+。