長い Weibo 写真の生成 (写真を含めることができます)

WBOY
リリース: 2016-07-25 09:12:15
オリジナル
1680 人が閲覧しました
長文Weibo画像生成(画像を含めることができ、初期設定では最初の2枚のみ許可されています。また、この方法は画像のフォーマットが面倒なので、一時的に2枚の画像を設定します)
単純なテキストから画像を生成するのは比較的簡単です、ただし、リッチテキストが必要な場合は、比較的面倒です。もちろん、いくつかのソースコードが形成されていますが、コンポーネントのインストールが必要なものもあります(特定のWeb環境)。また、オープンソースのpaintyが良いようです。ここでは、p タグと img タグの画像生成を自分で実装していますが、これにも少し時間がかかります (主に画像のレイアウトに)。この機能はもともと WordPress で長い Weibo 投稿をプッシュするために使用され、コミュニケーションと学習のための機能にカプセル化されました。
  1. /**
  2. *
  3. * 長い Weibo 画像生成
  4. * @paramknown_type 記事 ID
  5. * @paramknown_type 記事コンテンツ (ID に従って取得でき、ここに値を直接渡します)
  6. * @paramknown_type $img_path 画像を保存するためのハード パス
  7. * @paramknown_type $img_path_url画像パス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. //分割された p タグの処理
  13. $p_count = substr_count($text,'

    ') //分割されたタグ番号
  14. ; $content = preg_replace("/

    /isU","n",$text); //分割ラベル
  15. //画像画像ラベル処理
  16. $all_img_height = 0;
  17. if(preg_match_all( "/< img[^>]*src="([^"]*)"[^>]*>/", $content, $m)) {
  18. //最初の 2 枚の写真のみを撮影します
  19. $m[ 0] = array_slice($m[0],0,2);
  20. $m[1] = array_slice($m[1],0,2);
  21. //画像リソースを保存
  22. foreach($ m[1] as $i=>$src) {
  23. $imgs[] = $src;
  24. }
  25. //すべての画像を取得
  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. ブレーク;
  36. case "jpeg":
  37. $im = imagecreatefromjpeg($image);
  38. ブレーク;
  39. case "jpg":
  40. $im = imagecreatefromjpeg($image);
  41. ブレーク;
  42. }
  43. $imgs[$i] = array(
  44. '0'=>$im,
  45. 'height'=>floor(410/imagesx($im)*imagesy($im)), // 比例して拡大縮小します
  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); //同じ
  52. //$img_pos[$i] = mb_strpos($content, 'img-pos-pos'.$i); //img タグを削除したテキストを使用します
  53. //$imgs[$ i]['img_pos '] = $img_pos[$i];
  54. $imgs[$i]['f​​ull'] = $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 ');//追加された改行/スペースが削除されないようにします
  60. }
  61. $content =strip_tags($content);
  62. //$content = SpHtml2Text($content);//テキストに変換します
  63. $ content = autowrap(12 , 0, $font, $content, 395); // 自動行折り返し
  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); //imgタグを削除した後にテキストを使用
  68. $imgs[$i]['img_pos'] = $img_pos[$i];
  69. $content = str_replace('img-pos-pos'.$i,$replace_con, $content);
  70. //$add_footer_input = "nカスタムボトム追加nnn";//カスタムボトム
  71. $input = str_replace("r", "",stripcslashes($content));
  72. //$input = str_replace(" " , "" 、stripcslashes($input));
  73. //$title =explode("nn", $input);
  74. $ary = imagettfbbox (12, 0, $font, $input);
  75. $width = abs( $ary[ 2] - $ary[0]) + 40;
  76. $height = abs($ary[1] - $ary[7]) + 220 + 215 + $p_count*25;
  77. //代わりに高精細画像 imagecreate ()、コンテンツに画像がない場合は、imagecreate を使用することをお勧めします
  78. $img = @imagecreatetruecolor($width, $height);
  79. $bg_color=imagecolorallocate($img,229,231,230);
  80. imagecolortransparent($img,$) bg_color); // 設定 この行をコメントアウトすると上で設定した背景が出力されます
  81. $bgcolor = imagecolorallocate($img, ' 250', '250', '250') ;
  82. $bdcolor = imagecolorallocate($img, '250', '250', '250');
  83. $color = imagecolorallocate($img, '0', '0' , '0');
  84. $color_title = imagecolorallocate ($img, '250', '140', '0');
  85. $input = str_replace('endendend','',$input); //追加されたものを削除します。干渉文字
  86. imagettftext($img, 12, 0 , 20, 160, $color, $font, $input);
  87. imagettftext($img, 18, 0, 21, 160, $color_title, $font, $title);
  88. imagerectangle($img, 0, 0, imagex ($img) - 1, imagesy($img) - 1, $bdcolor);
  89. //ここでアイコンの保存パスを設定します
  90. $img_path = dirname(__FILE__).' /../../weibo_img';
  91. $ img_path_url = '/wp-content/weibo_img';
  92. //$img_path = empty($img_path)?'':$img_path;
  93. //$img_path_url = empty( $img_path_url)?'':$img_path_url;
  94. //公開ヘッダー/ボトム画像を合成します
  95. if(file_exists($img_path.'/weibo_header.jpg') && file_exists($img_path.'/weibo_footer.jpg')){
  96. $child1 = imagecreatefromjpeg($img_path.'/weibo_header.jpg');
  97. $child2 = imagecreatefromjpeg($img_path.'/weibo_footer.jpg');
  98. imagecopymerge ( $img, $child1, 0, 40, 0, 0 、imagesx ( $child1 )、imagesy ( $child1 )、100 );
  99. imagecopymerge ( $img、$child2、0、$height-215、0、0、imagesx ( $child2 )、imagesy ( $child2 )、100 ) ;
  100. }
  101. //画像追加(imgタグ)
  102. if(!empty($imgs)){
  103. $before_img_height = 0;
  104. foreach($imgs as $i=>$v){
  105. $child = $ v[0];
  106. $part_content = mb_substr( $content,0,$v['img_pos'],'utf-8');
  107. $rows_count = get_wrap_height(12, 0, $font, $part_content , 300);
  108. $dst_y = ($rows_count+7)* 27 - 9 + $before_img_height;
  109. $before_img_height += $v['height'] + 25;//累積占有高さ
  110. imagecopyresampled($img,$child,18,$ dst_y,0,0,'410',$ v['height'],imagesx($child),imagesy($child));
  111. }
  112. }
  113. //画像を生成して画像リンクを返す
  114. $file = empty ($img_path)?'img/p-' . $pid . '.png':$img_path.'/p-'.$pid.'.png';
  115. imagepng($img, $file);
  116. imagedestroy( $img);
  117. if(empty($img_path_url) ){
  118. 'http://' . $_SERVER['REQUEST_URI'] . $file;
  119. }else{
  120. return 'http://' . $_SERVER['HTTP_HOST'] . $img_path_url . '/p-'.$pid.'.png';
  121. }
  122. }
  123. /**
  124. * HTML をテキストに変換します
  125. * @param inputString
  126. * @return
  127. * /
  128. function SpHtml2Text($str){
  129. // $str =strip_tags($str);
  130. $str = preg_replace("/||/isU","n",$str);
  131. $alltext = "";
  132. $start = 1;
  133. for($i=0;$ i if($start==0 && $str[$i]==">"){
  134. $start = 1;
  135. }elseif($start==1 ){
  136. if($str[$i]= ="<"){
  137. $start = 0;
  138. $alltext .= " ";
  139. }elseif(ord($str[$i])>31) {
  140. $alltext .= $str[$i] ;
  141. }
  142. }
  143. }
  144. $alltext = str_replace(" "," ",$alltext);
  145. $alltext = preg_replace("/&([^;&] *)(;|&)/","" ,$alltext);
  146. $alltext = preg_replace("/[ ]+/s"," ",$alltext);
  147. return $alltext;
  148. }
  149. /* *
  150. *
  151. * 自動行折り返し処理
  152. * @paramunknown_type $fontsize フォントサイズ
  153. * @paramunknown_type $angle 角度
  154. * @paramunknown_type $fontface フォント名 (できれば絶対パスを使用)
  155. * @paramunknown_type $string string
  156. * @paramknown_type $widthデフォルト幅
  157. */
  158. function autowrap($fontsize , $angle, $fontface, $string, $width) {
  159. $content = "";
  160. $letter = array();
  161. // 文字列を個々の単語に分割しますそしてそれらを配列letter
  162. for ($ i=0;$i $letter[] = mb_substr($string, $i, 1);
  163. }
  164. foreach ( $letter as $l) {
  165. $teststr = $content." ".$l;
  166. $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
  167. // 結合された文字列が次の値を超えているかどうかを判断しますプリセット幅
  168. if (($testbox[ 2] > $width) && ($content !== "")) {
  169. $content .= "n";
  170. }
  171. $content .= $l;
  172. }
  173. $content を返します;
  174. }
  175. /**
  176. *
  177. * 特定のテキストが折り返された後の行数(高さ)を取得します
  178. * @paramknown_type $fontsize フォントサイズ
  179. * @paramunknown_type $angle angle
  180. * @paramunknown_type $fontface フォント名 (を使用するのが最適です)絶対パス)
  181. * @paramknown_type $string string
  182. * @paramknown_type $widthDefault width
  183. */
  184. function get_wrap_height($fontsize, $angle, $fontface, $string, $width) {
  185. $content = "";
  186. $rows_count = 0;
  187. $letter = array() ;
  188. // 文字列を個々の単語に分割し、配列 Letter
  189. for ($i=0;$i $letter[] = mb_substr($string, $ i, 1);
  190. }
  191. foreach ($letter as $l) {
  192. $teststr = $content." ".$l;
  193. $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr); // 結合された文字列がプリセット幅を超えているかどうかを判定します
  194. if (($testbox[2] > $width) && ($content !== "")) {
  195. $content .= "n"; $ rows_count += 1;
  196. }else{
  197. $rows_count += 1/$width;
  198. }
  199. $content .= $l;
  200. }
  201. return $rows_count;
  202. }
  203. /**
  204. *
  205. * 置換関数(1回置換)
  206. * @paramknown_type $needle
  207. * @paramunknown_type $replace
  208. * @paramunknown_type $haystack
  209. */
  210. 関数 str_replace_once ($needle, $replace, $haystack) {
  211. $pos = strpos($haystack, $needle);
  212. if ($pos === false) {
  213. return $haystack;
  214. }
  215. return substr_replace($haystack , $ replace, $pos, strlen($needle));
  216. }
  217. ?>
コードをコピー
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!