教你用php将二维码和文字结合到一个背景图片上!
将二维码生成到背景图片上,并且文字生成到背景图上,可以无限制扩增
1.生成前的图片
生成后的图片
class codeImg { private $date,$img,$main,$width,$height,$target,$white; public function constr($source) { $this->date = '' . date('Ymd') . '/'; $this->img = $this->date . md5($source) . '.jpg'; $this->main = imagecreatefromjpeg($source); $this->width = imagesx($this->main); $this->height = imagesy($this->main); $this->target = imagecreatetruecolor($this->width, $this->height); $this->white = imagecolorallocate($this->target, 255, 255, 255); imagefill($this->target, 0, 0, $this->white); imagecopyresampled($this->target, $this->main, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height); } /** * 生成二维码 * @param [type] $website [生成二维码地址] * @param string $filename [生成二维路经名称] * @param string $level [这个参数可传递的值分别是L(QR_ECLEVEL_L,7%)、M(QR_ECLEVEL_M,15%)、Q(QR_ECLEVEL_Q,25%)、H(QR_ECLEVEL_H,30%),这个参数控制二维码容错率,不同的参数表示二维码可被覆盖的区域百分比,也就是被覆盖的区域还能识别;] * @param integer $size [控制生成图片的大小,默认为4] * @param integer $margin [控制生成二维码的空白区域大小] * @return [type] [description] */ public function qrencode($website, $filename = false, $level = "L", $size = 4, $margin = 2) { include "./phpqrcode/qrlib.php"; QRcode::png($website, $filename, $level, $size, 2); } /** * 把二维码图片生成到背景图片上及文字 * @param string $source 背景图片 * @param string $text1 文字描述 * @param string $child1 二维码图 * @param integer $textwidth 文字横向位置 * @param integer $textherght 文字高度 * @param integer $$fontSize 字体大小 * @param integer $cate1,$cate2,$cate3 颜色表 * @param string $font 文字字体 * @return [type] [description] */ public function generateFont($source, $text1, $textwidth, $textherght, $fontSize = 18, $cate1 = 255, $cate2 = 250, $cate3 = 250, $font = './font/fangsong_GB2312.ttf') { $this->constr($source); $fontColor = imagecolorallocate($this->target, $cate1, $cate2, $cate3); //字的RGB颜色 $fontBox = imagettfbbox($fontSize, 0, $font, $text1); //文字水平居中实质 imagettftext($this->target, $fontSize, 0, $textwidth, $textherght, $fontColor, $font, $text1); $this->createImg(); return $this->img; } /** * [generateImg description] * @param string $source 背景图片 * @param string $codeurl 二维码图片 * @param integer $sourcewidth 二维码横向所在位置 * @param integer $sourceheight 二维码高度位置 * @param integer $codewidth 二维码宽度 * @param integer $codeheight 二维码高度 * @return [type] [description] */ public function generateImg($source, $codeurl, $sourcewidth, $sourceheight, $codewidth = 100, $codeheight = 100) { $this->constr($source); $child1 = imagecreatefrompng($codeurl); $codewidth = $codewidth > 0 ? $codewidth :imagesx($child1); $codeheight = $codeheight > 0 ? $codeheight : imagesy($child1); imagecopyresampled($this->target, $child1, $sourcewidth, $sourceheight, 0, 0, $codewidth, $codeheight,imagesx($child1),imagesy($child1)); imagedestroy($child1); $this->createImg(); return $this->img; } function createImg() { @mkdir('./' . $this->date); imagejpeg($this->target, './' . $this->img, 95); imagedestroy($this->main); imagedestroy($this->target); } } $source = "./img/1000.jpg"; $codeImg = new codeImg($source); $website = "http://www.baidu.com"; $codeurl = "./temp/code.png"; $codeImg->qrencode($website, $codeurl); ################################# $text = "开始了开始了hahhah呵呵"; $textwidth = 100; $textherght = 50; $generateFont = $codeImg->generateFont($source, $text, $textwidth, $textherght); ########################################################## $sourcewidth = 200; $sourceheight = 150; $generateImg = $codeImg->generateImg($generateFont, $codeurl, $sourcewidth, $sourceheight); echo "<img src='" . $generateImg . "'>"; ``
更多相关知识,请访问PHP中文网!
以上是教你用php将二维码和文字结合到一个背景图片上!的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

如果您是一位经验丰富的 PHP 开发人员,您可能会感觉您已经在那里并且已经完成了。您已经开发了大量的应用程序,调试了数百万行代码,并调整了一堆脚本来实现操作

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

字符串是由字符组成的序列,包括字母、数字和符号。本教程将学习如何使用不同的方法在PHP中计算给定字符串中元音的数量。英语中的元音是a、e、i、o、u,它们可以是大写或小写。 什么是元音? 元音是代表特定语音的字母字符。英语中共有五个元音,包括大写和小写: a, e, i, o, u 示例 1 输入:字符串 = "Tutorialspoint" 输出:6 解释 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。总共有 6 个元

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

静态绑定(static::)在PHP中实现晚期静态绑定(LSB),允许在静态上下文中引用调用类而非定义类。1)解析过程在运行时进行,2)在继承关系中向上查找调用类,3)可能带来性能开销。

PHP的魔法方法有哪些?PHP的魔法方法包括:1.\_\_construct,用于初始化对象;2.\_\_destruct,用于清理资源;3.\_\_call,处理不存在的方法调用;4.\_\_get,实现动态属性访问;5.\_\_set,实现动态属性设置。这些方法在特定情况下自动调用,提升代码的灵活性和效率。
