Long micro-blog image generation (can contain pictures, preliminary settings only allow the first two pictures, and this method of image typesetting more trouble, so set two pictures) Simple text generation picture is simple, but if you need rich text is relatively troublesome, of course, there are some molding source, but some need to install components (certain Web environment), the other open source Painty seems to be good, can refer to, here is just their own implementation of P, img Two label image generation, Also took a little time (mainly in the picture typesetting aspect) This function is to do WordPress long microblogging push, encapsulated into a function for Exchange learning
- /**
- *
- * Long Micro Blog Image generation
- * @param unknown_type Article ID
- * @param unknown_type article content (can be obtained by ID, direct value here)
- * @param unknown_type $img _path picture Save hard Path
- * @param unknown_type $img _path_url picture path URL
- */
- function Weibo_img_create ($article _id, $text, $title = ", $img _path=", $img _path_url= ") {
- $font = DirName (__file__). " /droid.ttf ";
- $pid = $article _id;
- Segment P Label processing
- $p _count = Substr_count ($text, '
'); Number of segmented labels
- $content = Preg_replace ("/<\/p>/isu", "\ n", $text); Segmented label
- Image img Tag Handling
- $all _img_height = 0;
- if (Preg_match_all ("/]*src=\" ([^\ "]*) \" [^>]*>/", $content, $m)) {
- Take only the first two pictures
- $m [0] = Array_slice ($m [0],0,2);
- $m [1] = Array_slice ($m [1],0,2);
- Save a picture resource
- foreach ($m [1] as $i = + $src) {
- $imgs [] = $SRC;
- }
- Get all pictures
- foreach ($imgs as $i = = $image) {
- $ext = End (Explode (".", $image));
- $im = null;
- Switch ($ext) {
- Case "GIF":
- $im = Imagecreatefromgif ($image);
- Break
- Case "PNG":
- $im = Imagecreatefrompng ($image);
- Break
- Case "JPEG":
- $im = Imagecreatefromjpeg ($image);
- Break
- Case "JPG":
- $im = Imagecreatefromjpeg ($image);
- Break
- }
- $imgs [$i] = Array (
- ' 0 ' = $im,
- ' Height ' =>floor (410/imagesx ($im) *imagesy ($im)),//proportionally scaled
- );
- }
- $content = Strip_tags ($content, ');
- foreach ($m [0] as $i = + $full) {
- $replace _con = str_repeat ("\ n", Ceil ($imgs [$i] [' height ']/25)];
- $content = Str_replace_once ($full, ' Img-pos-pos '. $i, $content); Replace only once to prevent the occurrence of the same
- $img _pos[$i] = Mb_strpos ($content, ' Img-pos-pos '. $i); Use the text after removing the IMG tag
- $imgs [$i] [' img_pos '] = $img _pos[$i];
- $imgs [$i] [' full '] = $full;
- $content = Str_replace (' Img-pos-pos '. $i, $replace _con, $content);
- $all _img_height + = $imgs [$i] [' height '];
- }
- $all _img_height + = $imgs [0][' height '];
- $content = Strip_tags ($content. ' Endendend ');//prevents added line breaks/spaces from being deleted
- }
- $content = Strip_tags ($content);
- $content = Sphtml2text ($content);//Convert to Text
- $content = AutoWrap (0, $font, $content, 395); Automatic line Wrapping Processing
- if (!empty ($imgs)) {
- foreach ($imgs as $i = = $v) {
- $replace _con = str_repeat ("\ n", Ceil ($v [' height ']/25));
- $img _pos[$i] = Mb_strpos ($content, ' Img-pos-pos '. $i); Use the text after removing the IMG tag
- $imgs [$i] [' img_pos '] = $img _pos[$i];
- $content = Str_replace (' Img-pos-pos '. $i, $replace _con, $content);
- }
- }
- $add _footer_input = "\ n Customize bottom add \n\n\n";//Customize Bottom
- $input = Str_replace ("\ R", "", Stripcslashes ($content));
- $input = Str_replace ("", "" ", Stripcslashes ($input));
- $title = explode ("\ n", $input);
- $ary = Imagettfbbox (0, $font, $input);
- $width = ABS ($ary [2]-$ary [0]) + 40;
- $height = ABS ($ary [1]-$ary [7]) + + 215 + $p _count*25;
- High-definition picture instead of imagecreate (), if the content has no picture recommended to use Imagecreate
- $img = @imagecreatetruecolor ($width, $height);
- $BG _color=imagecolorallocate ($img, 229,231,230);
- Imagecolortransparent ($img, $BG _color); Set to Transparent color, and output the background set above if you comment out the line
- Imagefill ($img, 0,0, $BG _color);
- $bgcolor = Imagecolorallocate ($img, ' 250 ', ' 250 ', ' 250 ');
- $bdcolor = Imagecolorallocate ($img, ' 250 ', ' 250 ', ' 250 ');
- $color = Imagecolorallocate ($img, ' 0 ', ' 0 ', ' 0 ');
- $color _title = imagecolorallocate ($img, ' 250 ', ' 140 ', ' 0 ');
- $input = Str_replace (' endendend ', ' ', $input); Remove the added jammer
- Imagettftext ($img, 0, $color, $font, $input);
- Imagettftext ($img, 0, $color _title, $font, $title);
- Imagerectangle ($img, 0, 0, Imagesx ($img)-1, Imagesy ($img)-1, $bdcolor);
- Here, configure the icon to save the path
- $img _path = dirname (__file__). ' /.. /.. /weibo_img ';
- $img _path_url = '/wp-content/weibo_img ';
- $img _path = Empty ($img _path)? ": $img _path;
- $img _path_url = Empty ($img _path_url)? ": $img _path_url;
- Synthetic common head/Bottom picture
- if (file_exists ($img _path. ' /weibo_header.jpg ') && file_exists ($img _path. ' /weibo_footer.jpg ')) {
- $child 1 = imagecreatefromjpeg ($img _path. ' /weibo_header.jpg ');
- $child 2 = imagecreatefromjpeg ($img _path. ' /weibo_footer.jpg ');
- Imagecopymerge ($img, $child 1, 0, 0, 0, imagesx ($child 1), Imagesy ($child 1), 100);
- Imagecopymerge ($img, $child 2, 0, $height -215, 0, 0, Imagesx ($child 2), Imagesy ($child 2), 100);
- }
- Image join (img tag)
- if (!empty ($imgs)) {
- $before _img_height = 0;
- foreach ($imgs as $i = = $v) {
- $child = $v [0];
- $part _content = mb_substr ($content, 0, $v [' Img_pos '], ' utf-8 ');
- $rows _count = get_wrap_height (0, $font, $part _content, 300);
- $DST _y = ($rows _count+7) *27-9 + $before _img_height;
- $before _img_height + = $v [' height '] + 25;//cumulative occupancy
- Imagecopyresampled ($img, $child, $dst _y,0,0, ' 410 ', $v [' Height '],imagesx ($child), Imagesy ($child));
- }
- }
- Generate Picture back picture link
- $file = Empty ($img _path)? ' img/p-'. $pid. '. png ': $img _path. ' /p-'. $pid. PNG ';
- Imagepng ($img, $file);
- Imagedestroy ($IMG);
- if (Empty ($img _path_url)) {
- Return ' http://'. $_server[' Http_host '. DirName ($_server[' Request_uri '). '/' . $file;
- }else{
- Return ' http://'. $_server[' Http_host '. $img _path_url. '/p-'. $pid. PNG ';
- }
- }
- /**
- * HTML converted to text
- * @param inputstring
- * @return
- */
- function Sphtml2text ($STR) {
- $str = Strip_tags ($STR);
- $str = Preg_replace ("/ | | /isu "," \ n ", $str);
- $alltext = "";
- $start = 1;
- for ($i =0; $i<>
- if ($start ==0 && $str [$i]== ">") {
- $start = 1;
- }elseif ($start ==1) {
- if ($str [$i]== "<") {
- $start = 0;
- $alltext. = "";
- }elseif (Ord ($str [$i]) >31) {
- $alltext. = $str [$i];
- }
- }
- }
- $alltext = Str_replace ("", "" ", $alltext);
- $alltext = Preg_replace ("/& ([^;&]*) (;|&)/", "", $alltext);
- $alltext = Preg_replace ("/[]+/s", "", $alltext);
- return $alltext;
- }
- /**
- *
- * Automatic line wrapping Process
- * @param unknown_type $fontsize Font size
- * @param unknown_type $angle angle
- * @param unknown_type $fontface font name (preferably using absolute path)
- * @param unknown_type $string string
- * @param unknown_type $width Preset width
- */
- function AutoWrap ($fontsize, $angle, $fontface, $string, $width) {
- $content = "";
- $letter = Array ();
- Splits a string into a single word and saves it 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);
- Determines whether the stitched string exceeds the preset width
- if ($testbox [2] > $width) && ($content!== "")) {
- $content. = "\ n";
- }
- $content. = $l;
- }
- return $content;
- }
- /**
- *
- * Gets the number of lines (height) after a certain text has been wrapped
- * @param unknown_type $fontsize Font size
- * @param unknown_type $angle angle
- * @param unknown_type $fontface font name (preferably using absolute path)
- * @param unknown_type $string string
- * @param unknown_type $width Preset width
- */
- function Get_wrap_height ($fontsize, $angle, $fontface, $string, $width) {
- $content = "";
- $rows _count = 0;
- $letter = Array ();
- Splits a string into a single word and saves it 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);
- Determines whether the stitched string exceeds the preset width
- if ($testbox [2] > $width) && ($content!== "")) {
- $content. = "\ n";
- $rows _count + = 1;
- }else{
- $rows _count + = 1/$width;
- }
- $content. = $l;
- }
- return $rows _count;
- }
- /**
- *
- * Replace function (replace once)
- * @param unknown_type $needle
- * @param unknown_type $replace
- * @param unknown_type $haystack
- */
- function Str_replace_once ($needle, $replace, $haystack) {
- $pos = Strpos ($haystack, $needle);
- if ($pos = = = False) {
- return $haystack;
- }
- Return Substr_replace ($haystack, $replace, $pos, strlen ($needle));
- }
- ?>
Copy Code |