PHP实现文字写入图片功能
时间:2022-07-27
本文章向大家介绍PHP实现文字写入图片功能,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
本文实例为大家分享了PHP实现文字写入图片的具体代码,供大家参考,具体内容如下
/**
* PHP实现文字写入图片
*/
class wordsOnImg {
public $config = null;
/**
* @param $config 传入参数
* @param $config['file'] 图片文件
* @param $config['size'] 文字大小
* @param $config['angle'] 文字的水平角度
* @param $config['fontfile'] 字体文件路径
* @param $config['width'] 预先设置的宽度
* @param $config['x'] 开始写入时的横坐标
* @param $config['y'] 开始写入时的纵坐标
*/
public function __construct($config=null){
if(empty($config)){
return 'must be config';
}
$fileArr = explode(".",$config['file']);
$config['file_name'] = $fileArr[0];
$config['file_ext'] = $fileArr[1];
$this- config = $config;
}
/**
* PHP实现图片上写入实现文字自动换行
* @param $fontsize 字体大小
* @param $angle 角度
* @param $font 字体路径
* @param $string 要写在图片上的文字
* @param $width 预先设置图片上文字的宽度
* @param $flag 换行时单词不折行
*/
public function wordWrap($fontsize,$angle,$font,$string,$width,$flag=true) {
$content = "";
if($flag){
$words = explode(" ",$string);
foreach ($words as $key= $value) {
$teststr = $content." ".$value;
$testbox = imagettfbbox($fontsize, $angle, $font, $teststr);
//判断拼接后的字符串是否超过预设的宽度
if(($testbox[2] $width)) {
$content .= "n";
}
$content .= $value." ";
}
}else{
//将字符串拆分成一个个单字 保存到数组 letter 中
for ($i=0;$i<mb_strlen($string);$i++) {
$letter[] = mb_substr($string, $i, 1);
}
foreach ($letter as $l) {
$teststr = $content." ".$l;
$testbox = imagettfbbox($fontsize, $angle, $font, $teststr);
// 判断拼接后的字符串是否超过预设的宽度
if (($testbox[2] $width) && ($content !== "")) {
$content .= "n";
}
$content .= $l;
}
}
return $content;
}
/**
* 实现写入图片
* @param $text 要写入的文字
* @param $flag 是否直接输出到浏览器,默认是
*/
public function writeWordsToImg($text,$flag=true){
if(empty($this- config)){
return 'must be config';
}
//获取图片大小
$img_pathWH = getimagesize($this- config['file']);
//打开指定的图片文件
$im = imagecreatefrompng($this- config['file']);
#设置水印字体颜色
$color = imagecolorallocatealpha($im,0, 0, 255, 75);//蓝色
$have = false;
if(stripos($text,"<br/ ")!== false){
$have = true;
}
if($have){
$words_text = explode("<br/ ",$text);
$words_text[0] = $this- wordWrap($this- config['size'], $this- config['angle'], $this- config['fontfile'], $words_text[0], $this- config['width']); //自动换行处理
$words_text[1] = $this- wordWrap($this- config['size'], $this- config['angle'], $this- config['fontfile'], $words_text[1], $this- config['width']); //自动换行处理
$words_text[2] = $this- wordWrap($this- config['size'], $this- config['angle'], $this- config['fontfile'], $words_text[2], $this- config['width']); //自动换行处理
imagettftext($im, $this- config['size'], $this- config['angle'], $this- config['x'], $this- config['y'], $color, $this- config['fontfile'], $words_text[0]);
imagettftext($im, $this- config['size'], $this- config['angle'], $this- config['x'], $this- config['y']+30, $color, $this- config['fontfile'], " ".$words_text[1]);
imagettftext($im, $this- config['size'], $this- config['angle'], $img_pathWH[0]/2+70, $img_pathWH[1]-80, $color, $this- config['fontfile'], $words_text[2]);
if($flag){
header("content-type:image/png");
imagepng($im);
imagedestroy($im);
}
imagepng($im,$this- config['file_name'].'_1.'.$this- config['file_ext']);
imagedestroy($im);
}
$words_text = $this- wordWrap($this- config['size'], $this- config['angle'], $this- config['fontfile'], $text, $this- config['width']); //自动换行处理
imagettftext($im, $this- config['size'], $this- config['angle'], $this- config['x'], $this- config['y'], $color, $this- config['fontfile'], $words_text);
if($flag){
header("content-type:image/png");
imagepng($im);
imagedestroy($im);
}
imagepng($im,$this- config['file_name'].'_1.'.$this- config['file_ext']);
imagedestroy($im);
}
}
$text = "Dear Kang<br/ If you can hold something up and put it down, it is called weight lifting;if you can hold something up but can never put it down, it's called bueden bearing. Pitifully, most of people are bearing heavy burdens when they are in love.nnBeing nice to someone you dislike doesn't mean you're a hypocritical people. It means you're mature enough to tolerate your dislike towards them.<br/ Mr. Kang";
$data = array(
'file'= '20171226152410.png',
'size'= 12,
'angle'= 0,
'fontfile'= './Font/ChalkboardSE.ttc',
'width'= 270,
'x'= 20,
'y'= 70
);
//使用
$wordsOnImgObj = new wordsOnImg($data);
$wordsOnImgObj- writeWordsToImg($text);
以上就是本文的全部内容,希望对大家的学习有所帮助。
- 使用Identity Server 4建立Authorization Server (4)
- 使用Identity Server 4建立Authorization Server (3)
- 使用Identity Server 4建立Authorization Server (2)
- 使用Identity Server 4建立Authorization Server (1)
- Java之异常处理
- 挖洞经验 | 看我如何利用SAML漏洞实现Uber内部聊天系统未授权登录
- 使用ichartjs生成图表
- 使用angular4和asp.net core 2 web api做个练习项目(四)
- list.add(),向List集合插入对象报空指针异常
- 使用angular4和asp.net core 2 web api做个练习项目(三)
- 即学即用系列一:纯函数
- Java之StringBuffer,StringBuilder,Math,Date,SimpleDateFormat,UUID,File
- React编程思想
- 前台分页,以及类别选择
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- laravel-admin的图片删除实例
- 在laravel-admin中列表中禁止某行编辑、删除的方法
- Laravel的Auth验证Token验证使用自定义Redis的例子
- laravel-admin解决表单select联动时,编辑默认没选上的问题
- laravel-admin 后台表格筛选设置默认的查询日期方法
- Laravel框架控制器的request与response用法示例
- laravel 字段格式化 modle 字段类型转换方法
- laravel-admin 在列表页添加自定义按钮的例子
- laravel利用中间件防止未登录用户直接访问后台的方法
- laravel实现上传图片并在页面显示的例子
- php实现大文件断点续传下载实例代码
- Laravel第三方包报class not found的解决方法
- 使用laravel的migrate创建数据表的方法
- 解决laravel session失效的问题
- php7下的filesize函数