Home > Backend Development > PHP Tutorial > How to convert php text to image with automatic line wrapping_PHP tutorial

How to convert php text to image with automatic line wrapping_PHP tutorial

WBOY
Release: 2016-07-21 15:12:12
Original
869 people have browsed it

When I accidentally browsed the Internet today, I found a long Weibo, which can convert text into png images, so I studied the method of converting text into images in PHP. In fact, as long as I use the PHP extension library, GD library to generate the image, and then generate it through the image function. After writing the code, I found that if there are too many words, the image will exceed the width of the screen, causing the browser's right pull bar to appear. So, I thought about whether there is any way to make the image It can automatically wrap lines. Through GG, I found an article that realizes automatic line wrapping of text and images by judging strings, intercepting strings, and then splicing them together. This code is posted below for learning:

Copy code The code is as follows:

header ("Content-type: image/png" );
mb_internal_encoding("UTF-8"); // Set encoding

function autowrap($fontsize, $angle, $fontface, $string, $width) {
// These variables are font size, angle, font name, string, default width
$content = "";

// Split the string into individual words and save them in the array letter
for ($i=0;$i $letter[] = mb_substr($string, $i, 1);
}

foreach ($letter as $l) {
$teststr = $content." ".$l;
$testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
// Determine whether the spliced ​​string exceeds the preset width
if (($testbox[2] > $width) && ($content !== "")) {
$content .= "n";
}
$content .= $l;
}
return $content;
}

$bg = imagecreatetruecolor(300, 290); // Create canvas
$white = imagecolorallocate($bg, 255, 255, 255); // Create white
$text = "Practice using it some time ago When using PHP's GD library, I struggled for a long time with the automatic line wrapping of text. Although line wrapping can be achieved by inserting \n, considering that the text contains both Chinese and English, the effect of forcing line wrapping every few words is very poor. Finally, I found a method to automatically wrap lines in English. The general principle is to use spaces as separators to split the string into words, and then splice them together one by one to determine whether their length exceeds the canvas. If so, Then change the line and then splice, otherwise continue to splice. Considering that Chinese needs to separate each text, I made a little modification. The complete code is as follows: ";
$text = autowrap(12, 0, "simsun.ttc. ", $text, 280); // Automatic line wrapping

// If the file encoding is GB2312, please remove the comments in the following line
// $text = iconv("GB2312", "UTF-8", $text);

imagettftext($bg, 12, 0, 10, 30, $white, "simsun.ttc", $text);
imagepng($bg);
imagedestroy($bg);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326737.htmlTechArticleWhen I accidentally browsed the Internet today, I found a long Weibo, which can convert text into png images, so Just study how to convert PHP text to image. In fact, as long as you use PHP extensions...
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