dongyuchen9276 2011-12-14 10:39
浏览 43
已采纳

在php中从头创建带有文本的透明png

All the examples I've found on the web seem to create pngs with text from an existing png. Is it possible to create a transparent png from scratch and then add text?

The code ive got so far follows (but it doesnt work. just outputs a blank image source)

<?php
    $width = 150;
    $height = 30;
    $text = "My Text";
    $fontsize = 5;

    $im = imagecreate($width, $height);
    $transcolor = imagecolortransparent($im);

    imagestring($im, $fontsize, 0, 0, $text, $transcolor);

    header('Content-type: image/png');
    imagepng($im);
    imagedestroy($im);
?>
  • 写回答

2条回答 默认 最新

  • dtr87341 2011-12-14 10:42
    关注
    <?php
        $font = 25;
        $string = 'My Text';
        $im = @imagecreatetruecolor(strlen($string) * $font / 1.5, $font);
        imagesavealpha($im, true);
        imagealphablending($im, false);
        $white = imagecolorallocatealpha($im, 255, 255, 255, 127);
        imagefill($im, 0, 0, $white);
        $lime = imagecolorallocate($im, 204, 255, 51);
        imagettftext($im, $font, 0, 0, $font - 3, $lime, "droid_mono.ttf", $string);
        header("Content-type: image/png");
        imagepng($im);
        imagedestroy($im);
    ?>
    

    Use imagestring instead of imagettftext if you don't want custom font.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置