Long Weibo image generation (can include pictures)

Source: Internet
Author: User
Tags transparent color
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
  1. /**
  2. *
  3. * Long Micro Blog Image generation
  4. * @param unknown_type Article ID
  5. * @param unknown_type article content (can be obtained by ID, direct value here)
  6. * @param unknown_type $img _path picture Save hard Path
  7. * @param unknown_type $img _path_url picture path URL
  8. */
  9. function Weibo_img_create ($article _id, $text, $title = ", $img _path=", $img _path_url= ") {
  10. $font = DirName (__file__). " /droid.ttf ";
  11. $pid = $article _id;
  12. Segment P Label processing
  13. $p _count = Substr_count ($text, '

    '); Number of segmented labels

  14. $content = Preg_replace ("/<\/p>/isu", "\ n", $text); Segmented label
  15. Image img Tag Handling
  16. $all _img_height = 0;
  17. if (Preg_match_all ("/]*src=\" ([^\ "]*) \" [^>]*>/", $content, $m)) {
  18. Take only the first two pictures
  19. $m [0] = Array_slice ($m [0],0,2);
  20. $m [1] = Array_slice ($m [1],0,2);
  21. Save a picture resource
  22. foreach ($m [1] as $i = + $src) {
  23. $imgs [] = $SRC;
  24. }
  25. Get all pictures
  26. foreach ($imgs as $i = = $image) {
  27. $ext = End (Explode (".", $image));
  28. $im = null;
  29. Switch ($ext) {
  30. Case "GIF":
  31. $im = Imagecreatefromgif ($image);
  32. Break
  33. Case "PNG":
  34. $im = Imagecreatefrompng ($image);
  35. Break
  36. Case "JPEG":
  37. $im = Imagecreatefromjpeg ($image);
  38. Break
  39. Case "JPG":
  40. $im = Imagecreatefromjpeg ($image);
  41. Break
  42. }
  43. $imgs [$i] = Array (
  44. ' 0 ' = $im,
  45. ' Height ' =>floor (410/imagesx ($im) *imagesy ($im)),//proportionally scaled
  46. );
  47. }
  48. $content = Strip_tags ($content, ');
  49. foreach ($m [0] as $i = + $full) {
  50. $replace _con = str_repeat ("\ n", Ceil ($imgs [$i] [' height ']/25)];
  51. $content = Str_replace_once ($full, ' Img-pos-pos '. $i, $content); Replace only once to prevent the occurrence of the same
  52. $img _pos[$i] = Mb_strpos ($content, ' Img-pos-pos '. $i); Use the text after removing the IMG tag
  53. $imgs [$i] [' img_pos '] = $img _pos[$i];
  54. $imgs [$i] [' full '] = $full;
  55. $content = Str_replace (' Img-pos-pos '. $i, $replace _con, $content);
  56. $all _img_height + = $imgs [$i] [' height '];
  57. }
  58. $all _img_height + = $imgs [0][' height '];
  59. $content = Strip_tags ($content. ' Endendend ');//prevents added line breaks/spaces from being deleted
  60. }
  61. $content = Strip_tags ($content);
  62. $content = Sphtml2text ($content);//Convert to Text
  63. $content = AutoWrap (0, $font, $content, 395); Automatic line Wrapping Processing
  64. if (!empty ($imgs)) {
  65. foreach ($imgs as $i = = $v) {
  66. $replace _con = str_repeat ("\ n", Ceil ($v [' height ']/25));
  67. $img _pos[$i] = Mb_strpos ($content, ' Img-pos-pos '. $i); Use the text after removing the IMG tag
  68. $imgs [$i] [' img_pos '] = $img _pos[$i];
  69. $content = Str_replace (' Img-pos-pos '. $i, $replace _con, $content);
  70. }
  71. }
  72. $add _footer_input = "\ n Customize bottom add \n\n\n";//Customize Bottom
  73. $input = Str_replace ("\ R", "", Stripcslashes ($content));
  74. $input = Str_replace ("", "" ", Stripcslashes ($input));
  75. $title = explode ("\ n", $input);
  76. $ary = Imagettfbbox (0, $font, $input);
  77. $width = ABS ($ary [2]-$ary [0]) + 40;
  78. $height = ABS ($ary [1]-$ary [7]) + + 215 + $p _count*25;
  79. High-definition picture instead of imagecreate (), if the content has no picture recommended to use Imagecreate
  80. $img = @imagecreatetruecolor ($width, $height);
  81. $BG _color=imagecolorallocate ($img, 229,231,230);
  82. Imagecolortransparent ($img, $BG _color); Set to Transparent color, and output the background set above if you comment out the line
  83. Imagefill ($img, 0,0, $BG _color);
  84. $bgcolor = Imagecolorallocate ($img, ' 250 ', ' 250 ', ' 250 ');
  85. $bdcolor = Imagecolorallocate ($img, ' 250 ', ' 250 ', ' 250 ');
  86. $color = Imagecolorallocate ($img, ' 0 ', ' 0 ', ' 0 ');
  87. $color _title = imagecolorallocate ($img, ' 250 ', ' 140 ', ' 0 ');
  88. $input = Str_replace (' endendend ', ' ', $input); Remove the added jammer
  89. Imagettftext ($img, 0, $color, $font, $input);
  90. Imagettftext ($img, 0, $color _title, $font, $title);
  91. Imagerectangle ($img, 0, 0, Imagesx ($img)-1, Imagesy ($img)-1, $bdcolor);
  92. Here, configure the icon to save the path
  93. $img _path = dirname (__file__). ' /.. /.. /weibo_img ';
  94. $img _path_url = '/wp-content/weibo_img ';
  95. $img _path = Empty ($img _path)? ": $img _path;
  96. $img _path_url = Empty ($img _path_url)? ": $img _path_url;
  97. Synthetic common head/Bottom picture
  98. if (file_exists ($img _path. ' /weibo_header.jpg ') && file_exists ($img _path. ' /weibo_footer.jpg ')) {
  99. $child 1 = imagecreatefromjpeg ($img _path. ' /weibo_header.jpg ');
  100. $child 2 = imagecreatefromjpeg ($img _path. ' /weibo_footer.jpg ');
  101. Imagecopymerge ($img, $child 1, 0, 0, 0, imagesx ($child 1), Imagesy ($child 1), 100);
  102. Imagecopymerge ($img, $child 2, 0, $height -215, 0, 0, Imagesx ($child 2), Imagesy ($child 2), 100);
  103. }
  104. Image join (img tag)
  105. if (!empty ($imgs)) {
  106. $before _img_height = 0;
  107. foreach ($imgs as $i = = $v) {
  108. $child = $v [0];
  109. $part _content = mb_substr ($content, 0, $v [' Img_pos '], ' utf-8 ');
  110. $rows _count = get_wrap_height (0, $font, $part _content, 300);
  111. $DST _y = ($rows _count+7) *27-9 + $before _img_height;
  112. $before _img_height + = $v [' height '] + 25;//cumulative occupancy
  113. Imagecopyresampled ($img, $child, $dst _y,0,0, ' 410 ', $v [' Height '],imagesx ($child), Imagesy ($child));
  114. }
  115. }
  116. Generate Picture back picture link
  117. $file = Empty ($img _path)? ' img/p-'. $pid. '. png ': $img _path. ' /p-'. $pid. PNG ';
  118. Imagepng ($img, $file);
  119. Imagedestroy ($IMG);
  120. if (Empty ($img _path_url)) {
  121. Return ' http://'. $_server[' Http_host '. DirName ($_server[' Request_uri '). '/' . $file;
  122. }else{
  123. Return ' http://'. $_server[' Http_host '. $img _path_url. '/p-'. $pid. PNG ';
  124. }
  125. }
  126. /**
  127. * HTML converted to text
  128. * @param inputstring
  129. * @return
  130. */
  131. function Sphtml2text ($STR) {
  132. $str = Strip_tags ($STR);
  133. $str = Preg_replace ("/ | | /isu "," \ n ", $str);
  134. $alltext = "";
  135. $start = 1;
  136. for ($i =0; $i<>
  137. if ($start ==0 && $str [$i]== ">") {
  138. $start = 1;
  139. }elseif ($start ==1) {
  140. if ($str [$i]== "<") {
  141. $start = 0;
  142. $alltext. = "";
  143. }elseif (Ord ($str [$i]) >31) {
  144. $alltext. = $str [$i];
  145. }
  146. }
  147. }
  148. $alltext = Str_replace ("", "" ", $alltext);
  149. $alltext = Preg_replace ("/& ([^;&]*) (;|&)/", "", $alltext);
  150. $alltext = Preg_replace ("/[]+/s", "", $alltext);
  151. return $alltext;
  152. }
  153. /**
  154. *
  155. * Automatic line wrapping Process
  156. * @param unknown_type $fontsize Font size
  157. * @param unknown_type $angle angle
  158. * @param unknown_type $fontface font name (preferably using absolute path)
  159. * @param unknown_type $string string
  160. * @param unknown_type $width Preset width
  161. */
  162. function AutoWrap ($fontsize, $angle, $fontface, $string, $width) {
  163. $content = "";
  164. $letter = Array ();
  165. Splits a string into a single word and saves it in the array letter
  166. for ($i =0; $i
  167. $letter [] = Mb_substr ($string, $i, 1);
  168. }
  169. foreach ($letter as $l) {
  170. $teststr = $content. " ". $l;
  171. $testbox = Imagettfbbox ($fontsize, $angle, $fontface, $TESTSTR);
  172. Determines whether the stitched string exceeds the preset width
  173. if ($testbox [2] > $width) && ($content!== "")) {
  174. $content. = "\ n";
  175. }
  176. $content. = $l;
  177. }
  178. return $content;
  179. }
  180. /**
  181. *
  182. * Gets the number of lines (height) after a certain text has been wrapped
  183. * @param unknown_type $fontsize Font size
  184. * @param unknown_type $angle angle
  185. * @param unknown_type $fontface font name (preferably using absolute path)
  186. * @param unknown_type $string string
  187. * @param unknown_type $width Preset width
  188. */
  189. function Get_wrap_height ($fontsize, $angle, $fontface, $string, $width) {
  190. $content = "";
  191. $rows _count = 0;
  192. $letter = Array ();
  193. Splits a string into a single word and saves it in the array letter
  194. for ($i =0; $i
  195. $letter [] = Mb_substr ($string, $i, 1);
  196. }
  197. foreach ($letter as $l) {
  198. $teststr = $content. " ". $l;
  199. $testbox = Imagettfbbox ($fontsize, $angle, $fontface, $TESTSTR);
  200. Determines whether the stitched string exceeds the preset width
  201. if ($testbox [2] > $width) && ($content!== "")) {
  202. $content. = "\ n";
  203. $rows _count + = 1;
  204. }else{
  205. $rows _count + = 1/$width;
  206. }
  207. $content. = $l;
  208. }
  209. return $rows _count;
  210. }
  211. /**
  212. *
  213. * Replace function (replace once)
  214. * @param unknown_type $needle
  215. * @param unknown_type $replace
  216. * @param unknown_type $haystack
  217. */
  218. function Str_replace_once ($needle, $replace, $haystack) {
  219. $pos = Strpos ($haystack, $needle);
  220. if ($pos = = = False) {
  221. return $haystack;
  222. }
  223. Return Substr_replace ($haystack, $replace, $pos, strlen ($needle));
  224. }
  225. ?>
Copy Code
  • Related Article

    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.