PHP uses the GD library to implement the method of adding text to pictures (code)

不言
Release: 2023-04-03 20:22:01
Original
4125 people have browsed it

The content of this article is about the method (code) of adding text on pictures using PHP GD library. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

We can use PHP's gd library extension to process our images, such as generating thumbnails, cropping images, and adding text to images as discussed in this chapter.

1. First we need to receive the text we need: $key = $_GET['key']; The specific operations such as anti-SQL processing of the received value will not be introduced here.

2. If you directly use the gd library to paste the text, it will not meet our requirements, because generally there is no word spacing when pasting it directly. We will see that the text is very close, which affects us. The aesthetics and customers do not need it, so we need to split the received content. Next, we will use regular encapsulated functions to split the Chinese string into an array.

/**
 * 将字符串分割为数组    
 * @param  string $str 字符串
 * @return array       分割得到的数组
 */
function mb_str_split($str){
    return preg_split('/(?
Copy after login

3. The above function will split the text into an array. Next, we will use the gd library to add the text array to the picture one by one.

mb_str_split($str);//将值拆分为数组
     $size = 40;//字体大小
     $font = PATH_ROOT."/1.ttf";//加载字体ttf
     $img = imagecreatefromjpeg($img);// 加载已有图像
     $black = imagecolorallocate($img, 24, 165, 234);//设置颜色为蓝色
     $x =355;//首个字的横坐标
     $red = imagecolorallocate($img,255,255,255);//创建白色底色
     imagefilledrectangle($img,340,40,420,120,$red);//构建一个矩形
     foreach ($text as $k=>$v){
         imagettftext($img, $size, 0, $x, 100, $black, $font, $v);//循环添加文字
         $x = $x + 70;//增加横坐标来做到间距的效果
     }
     $time = rand(1,10000).time().".png";//定义图片名
     ImagePNG($img,PATH_ROOT."/".$time);//保存图片
}
function mb_str_split($str){
     return preg_split('/(?
Copy after login

So that we can receive the Add text to the image and save it at the same time

If you add header('Content-Type: image/png'); the image will be displayed in the browser

Related recommendations:

How to draw a circle in php_php gd library draws a circle picture

php method to use the GD library to turn a square picture into a circular picture (code)

The above is the detailed content of PHP uses the GD library to implement the method of adding text to pictures (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!