thinkphp中实现文字生成透明背景png图片效果

thinkphp是国内常见的PHP开发框架之一,通过基于thinkPHP可以根据需求定制出不同的功能点。本篇文章就来介绍基于thinkPHP框架开发中遇到的常见问题解决方案。

对于任意网站来说登录可以说是鉴定用户极为重要的操作流程,常见的用户登录关于验证码的设置也是必不可少的流程。

那么基于thinkphp可以如何来实现将文字或数字生成图片呢?

文中给出详细的代码流程:

public function index(){
$image = imagecreatetruecolor(200, 200);
imagesavealpha($image, true);
//设置透明度
$bg = imagecolorallocatealpha($image , 0 , 0 , 0 , 127);
//填充
imagefill($image , 0 , 0 , $bg);
$textColor = imagecolorallocate($image, 0, 0, 0);
$fontFile = ROOT_PATH . 'public' . DS . 'static/test/字体文件.ttf';
// 输出图像
// header('Content-type: image/png');
$filename = ROOT_PATH . 'public' . DS . 'static/test/' . time() . '.png';
imagettftext($image, 156.6, 0, 50, 190, $textColor, $fontFile, '正');
imagepng($image,$filename);
imagedestroy($image);
}

代码解析:

    1、imagecreatetruecolor创建指定尺寸画布,并设置png背景透明

    2、imagettftext填充文字颜色,字体、文字位置等信息。

    3、imagepng在指定位置生成png图片

    4、imagedestroy销毁图片数据。

六月初字帖坊小程序 你想要的字帖模板及工具,这里都有!