<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/11/12 * Time: 13:44 */ /** * @param $imgurl * @return resource * GD库载入一幅图像 (背景) */ function createImage($imgurl) { load()->func("communication"); $resp = ihttp_request($imgurl); return imagecreatefromstring($resp["content"]); } /** * @param $param 海报配置文件 * @param $qr_file 用户二维码 * @return string * 创建海报图片 */ function createImageUrl($param, $qr_file) { load()->func('logging'); error_reporting(E_ALL ^ E_NOTICE); load()->func("file"); $pp1="images/" . $param["uniacid"] . "/" . date("Y/m/d"); logging_run($pp1); $path = ATTACHMENT_ROOT.$pp1; /*$path = "../attachment/images/" . $param["uniacid"] . "/" . date("Y/m/d"); if (!is_dir($path)) { load()->func("file"); mkdirs($path); }*/ if (!is_dir($path)) { load()->func("file"); mkdirs($path); } $filename="/qr-image-" . $param["from_user"] . rand() . ".jpg"; $target_file = $path.$filename; if (empty($param["bg"])) { $bg_file = IA_ROOT . "/addons/le_hykx/style/images/qrcode.jpg"; } else { $bg_file = $param["bg"]; } set_time_limit(0); @ini_set("memory_limit", "256M"); //创建一个突破 宽:640 高:1008 $target = imagecreatetruecolor(640, 1008); //载入后台配置的背景图片 $bg = createImage(tomedia($bg_file)); //拷贝背景图到target上 宽:640 高:1008 imagecopy($target, $bg, 0, 0, 0, 0, 640, 1008); //释放原背景关联内存 imagedestroy($bg); $data = json_decode(str_replace(""", "'", $param["data"]), true); foreach ($data as $d) { $d = getRealData($d); if ($d["type"] == "head") { $avatar = preg_replace("/\\/0\$/i", "/96", $param["avatar"]); $target = newMergeImage($target, $d, $avatar); } else { if ($d["type"] == "img") { $target = newMergeImage($target, $d, $d["src"]); } else { if ($d["type"] == "qr") { $target = newMergeImage($target, $d, tomedia($qr_file)); } else { if ($d["type"] == "nickname") { $target = mergeText($target, $d, $param["nickname"]); }else { if ($d["type"] == "qq") { $target = mergeText($target, $d, $param["qq"]); }else { if ($d["type"] == "jg") { $target = mergeText($target, $d, $param["jg"]); } } } } } } } $res=imagejpeg($target, $target_file); logging_run('图片创建结果'); logging_run($target_file); imagedestroy($target); tomedia($target_file); return array('target_file'=>$target_file,'pp1'=>$pp1.$filename); } function getRealData($data) { $data["left"] = intval(str_replace("px", '', $data["left"])) * 2; $data["top"] = intval(str_replace("px", '', $data["top"])) * 2; $data["width"] = intval(str_replace("px", '', $data["width"])) * 2; $data["height"] = intval(str_replace("px", '', $data["height"])) * 2; $data["size"] = intval(str_replace("px", '', $data["size"])) * 2; $data["src"] = tomedia($data["src"]); return $data; } function newMergeImage($target, $data, $imgurl) { $img = createImage($imgurl); $w = imagesx($img); $h = imagesy($img); imagecopyresized($target, $img, $data["left"], $data["top"], 0, 0, $data["width"], $data["height"], $w, $h); imagedestroy($img); return $target; } //用户昵称处理 function mergeText($target, $data, $text) { load()->func('logging'); $font = IA_ROOT . "/web/resource/fonts/le_hykx.ttf"; $colors = hex2rgb($data["color"]); $color = imagecolorallocate($target, $colors["red"], $colors["green"], $colors["blue"]); $temp = array("color" => array($colors['red'], $colors['green'], $colors['blue']), "fontsize" =>$data['size'],"height"=>$data['height'], "width" => $data['width'], "left" => $data['left'], "top" => $data['top'], "hang_size" => 40); //这里我只用它做测量高度,把参数false改为true就是绘制了。 draw_txt_to($target, $temp, $text, true); //$res=textalign($target,$data,$text,true,'','',''); //logging_run($str_h); //imagettftext($target, $data["size"], 0, $data["left"], $data["top"] + $data["size"], $color, $font, $text); return $target; } //----------分割线------------/** /* 文字自动换行算法 * @param $card 画板 * @param $pos 数组,top距离画板顶端的距离,fontsize文字的大小,width宽度,left距离左边的距离,hang_size行高 * @param $str 要写的字符串 * @param $iswrite 是否输出,ture, 花出文字,false只计算占用的高度 * @return int 返回整个字符所占用的高度*/ function draw_txt_to($card, $pos, $str, $iswrite){ load()->func('logging'); $_str_h = $pos['top']; $fontsize = $pos["fontsize"]; $width = $pos["width"]; $margin_lift = $pos["left"]; $hang_size = $pos["hang_size"]; $temp_string = ""; $font_file = IA_ROOT . "/web/resource/fonts/le_hykx.ttf"; $tp = 0; logging_run($pos); $font_color = imagecolorallocate($card, $pos["color"][0], $pos["color"][1], $pos["color"][2]); for ($i = 0; $i < mb_strlen($str); $i++) { if($_str_h>$pos['height']+$pos['top']){ continue; } $box = imagettfbbox($fontsize, 0, $font_file, $temp_string); $_string_length = $box[2] - $box[0]; $temptext = mb_substr($str, $i, 1); $temp = imagettfbbox($fontsize, 0, $font_file, $temptext); //logging_run('宽度测试:'.$_str_h); if ($_string_length + $temp[2] - $temp[0] < $width) {//长度不够,字数不够,需要 //继续拼接字符串。 $temp_string .= mb_substr($str, $i, 1); if ($i == mb_strlen($str) - 1) {//是不是最后半行。不满一行的情况 $_str_h += $hang_size;//计算整个文字换行后的高度。 $tp++;//行数 if ($iswrite) {//是否需要写入,核心绘制函数 imagettftext($card, $fontsize, 0, $margin_lift, $_str_h, $font_color, $font_file, $temp_string); } } } else {//一行的字数够了,长度够了。// 打印输出,对字符串零时字符串置null logging_run('宽度测试:2'); $texts = mb_substr($str, $i, 1);//零时行的开头第一个字。// 判断默认第一个字符是不是符号; $isfuhao = preg_match("/[\\\\pP]/u", $texts) ? true : false;//一行的开头这个字符,是不是标点符号 if ($isfuhao) {//如果是标点符号,则添加在第一行的结尾 $temp_string .= $texts;// 判断如果是连续两个字符出现,并且两个丢失必须放在句末尾的,单独处理 $f = mb_substr($str, $i + 1, 1); $fh = preg_match("/[\\\\pP]/u", $f) ? true : false; if ($fh) { $temp_string .= $f; $i++; } } else { $i--; } $tmp_str_len = mb_strlen($temp_string); $s = mb_substr($temp_string, $tmp_str_len-1, 1);//取零时字符串最后一位字符 if (is_firstfuhao($s)) {//判断零时字符串的最后一个字符是不是可以放在见面 //讲最后一个字符用“_”代替。指针前移动一位。重新取被替换的字符。 $temp_string=rtrim($temp_string,$s); $i--; }// }// 计算行高,和行数。 $_str_h += $hang_size; $tp++; if ($iswrite) { imagettftext($card, $fontsize, 0, $margin_lift, $_str_h, $font_color, $font_file, $temp_string); }// 写完了改行,置null该行的临时字符串。 $temp_string = ""; } } return $tp * $hang_size; } function is_firstfuhao($str){ $fuhaos = array("\\", "“", "'", "<", "《","[","]","+"); return in_array($str, $fuhaos); } function autowrap($fontsize, $angle, $fontface, $string, $width) { mb_internal_encoding("UTF-8"); // 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度 $content = ""; // 将字符串拆分成一个个单字 保存到数组 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); // 判断拼接后的字符串是否超过预设的宽度 if (($testbox[2] > $width) && ($content !== "")) { $content .= "\n"; } $content .= $l; } return $content; } function hex2rgb($colour) { if ($colour[0] == "#") { $colour = substr($colour, 1); } if (strlen($colour) == 6) { list($r, $g, $b) = array($colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5]); } else { if (strlen($colour) == 3) { list($r, $g, $b) = array($colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2]); } else { return false; } } $r = hexdec($r); $g = hexdec($g); $b = hexdec($b); return array("red" => $r, "green" => $g, "blue" => $b); } function get_config(){ global $_W; $sysconfig=pdo_get('le_hykx_config',array('uniacid'=>$_W['uniacid'])); return unserialize($sysconfig['sysconfig']); } //-------------新版图片生成 多图拼接方式------------ function createImageUrl_new($param, $qr_file) { load()->func('logging'); error_reporting(E_ALL ^ E_NOTICE); load()->func("file"); $pp1="images/" . $param["uniacid"] . "/" . date("Y/m/d"); logging_run($pp1); $path = ATTACHMENT_ROOT.$pp1; if (!is_dir($path)) { load()->func("file"); mkdirs($path); } $content_file_name="/qr-image-content-" . $param["from_user"] . rand() . ".jpg"; $filename="/qr-image-" . $param["from_user"] . rand() . ".jpg"; $target_file = $path.$filename; $content_file=$path.$content_file_name; if (empty($param["bg"])) { $bg_file = IA_ROOT . "/addons/le_hykx/style/images/qrcode.jpg"; } else { $bg_file = $param["bg"]; } set_time_limit(0); @ini_set("memory_limit", "256M"); //创建一个图片 宽:640 高:1008 //计算文章内容的高度/宽度 $poster_header_img=tomedia($param['poster_header_img']); $poster_footer_img=tomedia($param['poster_footer_img']); //头部图片信息 $header_img_info = getimagesize($poster_header_img); $header_img_height=$header_img_info[1]; //尾部图片信息 $footer_img_info = getimagesize($poster_footer_img); $footer_img_height=$footer_img_info[1]; //获取文字标题字数 $title_count=mb_strlen($param['nickname'],"utf-8"); //获取行数 $title_h_count=ceil($title_count/14); //计算高度 $title_height=$title_h_count*50; //获取文章内容数量 $content_count=mb_strlen($param['jg'],"utf-8"); //获取文章行数 $content_h_count=ceil($content_count/22); //计算文章高度 $content_height=$content_h_count*40; //计算总高度 $z_height=$header_img_height+$footer_img_height+$title_height+$content_height; //单独创建中间内容图片 $content_garget_px=180;//固定增加px $content_garget = @imagecreate(640, $title_height+$content_height+$content_garget_px); $content_bg = imagecolorallocate($content_garget, 255, 255, 255); //标题 $n_d=array( 'top'=>"50px", 'left'=>'30px', 'width'=>'580px', 'height'=>$title_height."px", 'size'=>"28px", 'color'=>'#212121' ); $targets = mergeText($content_garget, $n_d, $param["nickname"]); //内容 $n_d=array( 'top'=>($title_height+60)."px", 'left'=>'30px', 'width'=>'580px', 'height'=>$content_height."px", 'size'=>"20px", 'color'=>'#666666' ); $targets = mergeText($content_garget, $n_d, $param["jg"]); $ress=imagejpeg($content_garget, $content_file); //内容图片信息 $content_img_info = getimagesize(tomedia($pp1.$content_file_name)); //创建总画布 //高度 $z_height=$title_height+$content_height+$content_garget_px+$header_img_height+$footer_img_height; $target = imagecreatetruecolor(640, $z_height); //拷贝头部图到target上 $c_poster_header_img=createImage($poster_header_img); imagecopy($target, $c_poster_header_img, 0, 0, 0, 0, $header_img_info[0], $header_img_info[1]); //释放头部图片关联内存 imagedestroy($c_poster_header_img); //拷贝内容图到target上 $c_poster_content_img=createImage(tomedia($pp1.$content_file_name)); imagecopy($target, $c_poster_content_img, 0, $header_img_height, 0, 0, $content_img_info[0], $content_img_info[1]); //释放头部图片关联内存 imagedestroy($c_poster_content_img); //拷贝底部图片到target上 放在底部,计算坐标 $c_poster_footer_img=createImage($poster_footer_img); $f_y=$z_height-$footer_img_height; imagecopy($target, $c_poster_footer_img, 0, $f_y, 0, 0, $footer_img_info[0], $footer_img_info[1]); //释放底部图片关联内存 imagedestroy($c_poster_footer_img); //二维码 //二维码 计算坐标 $n_d=array( 'top'=>($z_height-$footer_img_height+40), 'left'=>'508', 'width'=>'100', 'height'=>"100", ); $targets = newMergeImage($target, $n_d, tomedia($qr_file)); $res=imagejpeg($target, $target_file); logging_run('图片创建结果'); logging_run($target_file); imagedestroy($target); tomedia($target_file); return array('target_file'=>$target_file,'pp1'=>$pp1.$filename); } //调用 //查询海报配置信息 $poster = pdo_fetch("select * from " . tablename("le_hykx_poster") . " where `uniacid`=:uniacid limit 1", array(":uniacid" => $uniacid)); if(empty($poster)){ return false; } $article=pdo_get('le_hykx_article',array('id'=>$articleid)); $sysconfig=get_config(); if($sysconfig['code_type']=='1'){ //固定二维码 $code_url=$sysconfig['code_img']; }else{ //链接二维码 if(empty($article['link_code'])){ //生成二维码 $link=$_W['siteroot'].$this->createMobileUrl('detail',array('id'=>$articleid)); load()->library('qrcode'); $errorCorrectionLevel = 'L'; //容错级别 $matrixPointSize = 4; //生成图片大小 //生成二维码图片 $time=date("Y/m/d"); $path = "../attachment/images/" . $_W['uniacid'] . "/" . $time; if (!is_dir($path)) { load()->func("file"); mkdirs($path); } $filename="/qr-image-article".$articleid. rand() . ".png"; QRcode::png($link,$path.$filename , $errorCorrectionLevel, $matrixPointSize); pdo_update('le_hykx_article',array('link_code'=>'images/'.$_W['uniacid'].'/'.$time.$filename),array('id'=>$articleid)); $code_url=$path.$filename; }else{ $code_url=$article['link_code']; } } //数据组合 $poster['nickname']=$article['title']; $poster['from_user']=time(); //$poster['avatar']=$fans['avatar']; $poster['jg']=$article['content']; //二维码处理 //判断海报生成类型 if($sysconfig['poster_type']=="1"){ //老版本生成 $updates=createImageUrl($poster,$code_url); if($updates['pp1']){ pdo_update('le_hykx_article',array('poster_img'=>$updates['pp1']),array('id'=>$articleid)); return true; }else{ return false; } }else{ //新版 $poster['poster_header_img']=$sysconfig['poster_header_img']; $poster['poster_footer_img']=$sysconfig['poster_footer_img']; $updates=createImageUrl_new($poster,$code_url); if($updates['pp1']){ pdo_update('le_hykx_article',array('poster_img'=>$updates['pp1']),array('id'=>$articleid)); return true; }else{ return false; } }
下一篇: 微擎相关记录...