Long Weibo image generation (can include images, the initial setting only allows the first two images. In addition, this method is more troublesome to format images, so two images are temporarily set) It is relatively simple to generate images from simple text, but if rich text is required, it is relatively It is more troublesome. Of course, there are some formed source codes, but some require the installation of components (certain web environment). In addition, the open source painty seems to be good, you can refer to it. Here I just implement the image generation of p and img tags by myself, which also takes a lot of time. A little time (mainly for picture layout) This function was originally used to push long Weibo posts in WordPress, and was encapsulated into functions for communication and learning.
- /**
- *
- * Long Weibo image generation
- * @param unknown_type article id
- * @param unknown_type article content (can be obtained according to the id, pass the value directly here)
- * @param unknown_type $img_path hard path for image saving
- * @param unknown_type $img_path_urlimage path url
- */
- function weibo_img_create($article_id,$text,$title='',$img_path='',$img_path_url=''){
- $font = dirname(__FILE__)."/droid.ttf";
- $pid = $article_id;
- //Segmented p tag processing
- $p_count = substr_count($text,''); //Segmented tag Number
- $content = preg_replace("//isU","n",$text); //Segmentation label
- //Image img label processing
- $all_img_height = 0;
- if(preg_match_all( "/]*src="([^"]*)"[^>]*>/", $content, $m)) {
- //Only take the first two pictures
- $m[0] = array_slice($m[0],0,2);
- $m[1] = array_slice($m[1],0,2);
- //Save image resources
- foreach($ m[1] as $i=>$src) {
- $imgs[] = $src;
- }
- //Get all images
- 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)), // Scale proportionally
- );
- }
- $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); //only Replace once to prevent 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');//Prevent added line breaks/spaces from being deleted
- }
- $content = strip_tags($content);
- //$content = SpHtml2Text($content);//Convert to text
- $ content = autowrap(12, 0, $font, $content, 395); // Automatic line wrapping
- 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 Text after removing img tag
- $imgs[$i]['img_pos'] = $img_pos[$i];
- $content = str_replace('img-pos-pos'.$i,$replace_con, $content);
- }
- }
- //$add_footer_input = "ncustom bottom add nnn";//custom bottom
- $input = str_replace("r", "", stripcslashes($content));
- //$input = str_replace(" " , "", stripcslashes($input));
- //$title = explode("nn", $input);
- $ary = imagettfbbox (12, 0, $font, $input);
- $width = abs( $ary[2] - $ary[0]) + 40;
- $height = abs($ary[1] - $ary[7]) + 220 + 215 + $p_count*25;
-
- //High-definition picture instead imagecreate(), if the content does not have images, it is recommended to use imagecreate
- $img = @imagecreatetruecolor($width, $height);
- $bg_color=imagecolorallocate($img,229,231,230);
- imagecolortransparent($img,$bg_color); // Settings It is a transparent color. If this line is commented out, the background set above will be output
- 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 interference characters
- imagettftext($img, 12, 0 , 20, 160, $color, $font, $input);
- imagettftext($img, 18, 0, 21, 160, $color_title, $font, $title);
- imagerectangle($img, 0, 0, imagesx ($img) - 1, imagesy($img) - 1, $bdcolor);
-
- //Configure the icon saving path here
- $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;
-
- //Synthesize public header/bottom images
- if(file_exists($img_path.'/weibo_header.jpg') && file_exists($img_path.'/weibo_footer.jpg')){
- $child1 = imagecreatefromjpeg($img_path.'/ weibo_header.jpg');
- $child2 = imagecreatefromjpeg($img_path.'/weibo_footer.jpg');
- imagecopymerge ( $img, $child1, 0, 40, 0, 0, imagesx ( $child1 ), imagesy ( $child1 ), 100 );
- imagecopymerge ( $img, $child2, 0, $height-215, 0, 0, imagesx ( $child2 ), imagesy ( $child2 ), 100 );
- }
-
- //Image addition (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(12, 0, $font, $part_content , 300);
- $dst_y = ($rows_count+7)* 27 - 9 + $before_img_height;
- $before_img_height += $v['height'] + 25;//Cumulative occupied height
- imagecopyresampled($img,$child,18,$dst_y,0,0,'410',$ v['height'],imagesx($child),imagesy($child));
- }
- }
-
- //Generate image and return image 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';
- }
- }
-
- /**
- * Convert html 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 processing
- * @param unknown_type $fontsize font size
- * @param unknown_type $angle angle
- * @param unknown_type $fontface font name (preferably use absolute path)
- * @param unknown_type $string string
- * @param unknown_type $widthDefault width
- */
- function autowrap($fontsize , $angle, $fontface, $string, $width) {
- $content = "";
- $letter = array();
- // Split the string into individual words and save them into 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;
- }
-
- /**
- *
- * Get the number of lines (height) after a certain text is wrapped
- * @param unknown_type $fontsize font size
- * @param unknown_type $angle angle
- * @param unknown_type $fontface font name (it is best to use absolute paths)
- * @param unknown_type $string string
- * @param unknown_type $widthDefault width
- */
- function get_wrap_height($fontsize, $angle, $fontface, $string, $width) {
- $content = "";
- $rows_count = 0;
- $letter = array() ;
- // 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";
- $rows_count += 1;
- }else{
- $rows_count += 1/$width;
- }
- $content .= $l;
- }
- return $rows_count;
- }
- /**
- *
- * Replacement 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
|