PHP生成一个透明背景的PNG图像(GD+Imagick)

使用GD

    header('Content-Type:image/png');
    $width = 300;
    $height = 300;
    $color = 'ff0000';
    $font_file = 'size.ttf';

    $im = imagecreatetruecolor($width, $height);
    $bg = imagecolorallocatealpha($im, 0, 0, 0, 127);
    imagefill($im, 0, 0, $bg);
    imagefttext($im, 33, 0, 150, 150, $color, $font_file, 'test');
    imagesavealpha($im, true);
    imagepng($im);
    imagedestroy($im);

使用imagick

    $draw = new \ImagickDraw();
    $imagick = new \Imagick();
    $imagick->newImage($width, $height, new \ImagickPixel('none'));
    $draw->setFillColor($color);
    $draw->setFont($font_file);
    $draw->setFontSize($size);
    $draw->setTextEncoding('UTF-8');
    $draw->annotation($x, $y, $text);
    header("Content-Type: image/png");
    echo $imagick->getImageBlob();