搜索
首页php教程php手册php原生图片合成和文字生成图片

之前有一个我写的帖子是可以拿到远程地址的图片保存本地的
前段时间搞的很火的朋友圈标签,可以使用下面仅仅三个自定义函数即可合成,具体参数石头已经给大家写出,有需要改进的地方请直接留言!

/**
* a.合成图片信息 复制一张图片的矩形区域到另外一张图片的矩形区域
* @param  [type] $bg_image  [目标图]
* @param  [type] $sub_image [被添加图]
* @param  [type] $add_x     [目标图x坐标位置]
* @param  [type] $add_y     [目标图y坐标位置]
* @param  [type] $add_w     [目标图宽度区域]
* @param  [type] $add_h     [目标图高度区域]
* @param  [type] $out_image [输出图路径]
* @return [type]            [description]
*/
function image_copy_image($bg_image,$sub_image,$add_x,$add_y,$add_w,$add_h,$out_image){
if($sub_image){
$bg_image_c = imagecreatefromstring(file_get_contents($bg_image));
$sub_image_c = imagecreatefromstring(file_get_contents($sub_image));
imagecopyresampled($bg_image_c, $sub_image_c, $add_x, $add_y, 0, 0, $add_w, $add_h, imagesx($sub_image_c), imagesy($sub_image_c));
//保存到out_image
imagejpeg($bg_image_c, $out_image, 80);
imagedestroy($sub_image_c);
imagedestroy($bg_image_c);
}

}
/**
* b.生成文字图片并插入广告图中
* @param  [type] $filename    [背景路径]
* @param  [type] $text        [文字内容]
* @param  [type] $font        [文字大小]
* @param  [type] $size        [文字画布的宽]
* @param  [type] $width_f     [文字颜色]
* @param  [type] $red         [红]
* @param  [type] $grn	  [绿]
* @param [type] $blu          [蓝]
*/
function create_text($filename,$text,$font,$size,$width_f,$red,$grn,$blu){
$rot  = 0; // 旋转角度
$width = 0; //宽度
$height = 0; //高度
$offset_x = 0; //x偏移
$offset_y = 0; //y偏移
$bounds = array();
$text = autowrap($size, 0, $font, $text,$width_f); // 自动换行处理
/** [字体大小] [角度] [字体名称] [字符串] [预设宽度] */
// 确定边框高度.
$bounds = ImageTTFBBox($size, $rot, $font, "W");
if ($rot < 0) {
$font_height = abs($bounds[7]-$bounds[1]);
} else if ($rot > 0) {
$font_height = abs($bounds[1]-$bounds[7]);
} else {
$font_height = abs($bounds[7]-$bounds[1]);
}
// 确定边框高度.
$bounds = ImageTTFBBox($size, $rot, $font, $text);
if ($rot < 0) {
$width = abs($bounds[4]-$bounds[0]);
$height = abs($bounds[3]-$bounds[7]);
$offset_y = $font_height;
$offset_x = 0;
} else if ($rot > 0) {
$width = abs($bounds[2]-$bounds[6]);
$height = abs($bounds[1]-$bounds[5]);
$offset_y = abs($bounds[7]-$bounds[5])+$font_height;
$offset_x = abs($bounds[0]-$bounds[6]);
} else {
$width = abs($bounds[4]-$bounds[6]);
$height = abs($bounds[7]-$bounds[1]);
$offset_y = $font_height;
$offset_x = 0;
}
$bg = imagecreatetruecolor($width + 20,$height + 20); // 创建画布
$color=imagecolorallocatealpha($bg , 0 , 0 , 0 ,127);//拾取一个完全透明的颜色
imagealphablending($bg ,false);//关闭混合模式,以便透明颜色能覆盖原画布
imagefill($bg , 0 , 0, $color);//填充
imagesavealpha($bg ,true);//设置保存PNG时保留透明通道信息
$textImg = imagecolorallocate($bg, $red, $grn, $blu); // 创建白色
ImageTTFText($bg, $size, 0, 10, $size + 10, $textImg, $font, $text);
imagepng($bg,$filename);
}
/**
* 文字自动换行
* @param  [type] $fontsize    [字体大小]
* @param  [type] $angle       [角度]
* @param  [type] $fontface    [字体名称]
* @param  [type] $string      [字符串]
* @param  [type] $width       [预设宽度]
*/
function autowrap($fontsize, $angle, $fontface, $string, $width) {
$content = "";
// 将字符串拆分成一个个单字 保存到数组 letter 中
preg_match_all("/./u", $string, $arr);
$letter = $arr[0];
foreach ($letter as $l) {
$teststr = $content." ".$l;
$testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
// 判断拼接后的字符串是否超过预设的宽度
if (($testbox[2] > $width) && ($content !== "")) {
$content .= PHP_EOL;
}
$content .= $l;
}
return $content;
}

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中