search
Homephp教程php手册PHP native image synthesis and text generation images

There was a post I wrote before that allows you to get pictures from remote addresses and save them locally.
The Moments tag, which was very popular some time ago, can be synthesized using just three custom functions below. The specific parameters have been written by Shitou. If you have any areas that need improvement, please leave a message directly!

/**
* 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;
}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft