【问题标题】:make transparent png image by gd用gd制作透明png图片
【发布时间】:2019-11-30 17:27:45
【问题描述】:

我想制作透明的 png 图像,并通过 imagefttext(黑色)在其上打印字符串。 我读了一些例子,我想我应该使用 imagecolortransparent 函数,但它是用黑色背景制作图像。 我该怎么做?

【问题讨论】:

    标签: php gd


    【解决方案1】:

    试试这个

    <?php
    //Canvas size 100x50
    $image = imagecreatetruecolor(100, 50);
    imagealphablending($image, false);
    //Create alpha channel for transparent layer
    $col=imagecolorallocatealpha($image,255,255,255,127);
    //Create overlapping 100x50 transparent layer
    imagefilledrectangle($image,0,0,100, 50,$col);
    //Continue to keep layers transparent
    imagealphablending($image,true);
    //Insert the text
    imagefttext($image,10,0,10,20,0,'octin.ttf','test sting');
    //Keep trnsparent when saving
    imagesavealpha($image,true);
    
    //Save & output
    if(imagepng($image, "test.png", 1)){
       header("Content-Type: image/png");
       readfile('test.png');
    }
    imagedestroy($image);
    ?>
    

    输出 100x50px [test.png]

    哎呀我忘了r...我的错

    【讨论】:

    • 很好,这很棒。我工作了,但我确实必须像这样添加字体 realpath 指令: $fontpath = realpath('.'); putenv('GDFONTPATH='.$fontpath);
    猜你喜欢
    • 2011-08-31
    • 1970-01-01
    • 2013-03-17
    • 2013-07-17
    • 1970-01-01
    • 2015-09-20
    • 2015-04-20
    • 2017-11-13
    • 2012-07-22
    相关资源
    最近更新 更多