The method of automatically wrapping the PHP text to picture _php skill

Source: Internet
Author: User

Today, when I accidentally browsed the web, I found a long microblog, it can convert text into a PNG picture, and then study the PHP text to the way the picture, in fact, only if the use of PHP extension library, GD library to generate pictures, and then through the image function generation, code written out, found that if the text too much, The picture will go over the width of the screen, led to the browser right pull, so, think there is no way to make the picture can automatically wrap, through GG, found an article, through the judgment string and the interception of strings and then splicing to achieve text pictures automatically line, the following posted this code to learn:

Copy Code code as follows:

<?php
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, preset width
$content = "";

Split a string into a single word and save it in the array 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, $fontface, $TESTSTR);
Determines 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 = "The previous period of time to practice using the GD Library PHP, for the text of the automatic line wrap entangled for a very long." Although it is possible to wrap by inserting \ n, it is very bad to force the number of words to be wrapped, taking into account both Chinese and English in the text. Then finally found an English word for automatic line-wrapping method, the principle is that the space as a separator, the string is divided into a word, and then one after another to join together to determine whether its length is over the canvas, if more than the line and then splicing, or continue stitching. Taking into account the need for Chinese to take apart each text, so I made a little change, the complete code as follows. ";
$text = AutoWrap (0, "SIMSUN.TTC", $text, 280); Wrap line Processing

If the file is encoded as GB2312 please remove the following comments
$text = Iconv ("GB2312", "UTF-8", $text);

Imagettftext ($BG, 0, $white, "SIMSUN.TTC", $text);
Imagepng ($BG);
Imagedestroy ($BG);
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.