PHP生成带文字圆角图片

使用PHP GD库函数生成带文字图片,CSS3的border-radius为元素添加圆角边框。

image.php

<?php
    //1.创建画布
    $img = imagecreatetruecolor(120, 120);

    //2.背景颜色
    $color = imagecolorallocate($img, 0, 184, 212);

    //3.填充背景颜色
    imagefill($img, 0, 0, $color);

    //4.写字符串(字母)
    //$str = substr(str_shuffle('ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'), 0, 4);
    //imagestring($img, 5, 5, 5, $str, $red);

    //5.写字符串
    $text = '会';
    if (preg_match('/^[\x{4e00}-\x{9fa5}]/u', $text)) { //中文
        $left = 20;
        $top = 88;
        $size = 60;
    } else if (preg_match('/^[a-zA-Z]$/', $text) || preg_match('/^[0-9]$/', $text)) { //数字和字母
        $left = 26;
        $top = 95;
        $size = 72;
    }
    $text_color = imagecolorallocate($img, 255, 255, 255);
    imagettftext($img, $size, 0, $left, $top, $text_color, './msyh.ttf', $text);

    //6.输出图片
    header('content-type:image/png');
    imagepng($img);
    //imagepng($img, date("Ymd_His") . '.png');

    //7.销毁画布
    imagedestroy($img);

index.php

<image style="border-radius:20px;" src="image.php" />

预览

image

上一篇 PHP实现3DES加解密
下一篇 Laravel快速集成EasyWeChat
目录
文章列表
1 Java处理UFT-8编码文件出现\ufeff的解决方法
Java处理UFT-8编码文件出现\ufeff的解决方法
2
Spring Boot定时任务运行一段时间后自动关闭的解决办法
Spring Boot定时任务运行一段时间后自动关闭的解决办法
3
Shadowsocks + Privoxy 搭建 http 代理服务
Shadowsocks + Privoxy 搭建 http 代理服务
4
Groovy代码示例 - collect()方法
Groovy代码示例 - collect()方法
5
设计模式(11)命令模式
设计模式(11)命令模式
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。