/* **生成证书图片 **$BlockHash 区块hash ** */ public function createNewPic($mer_id,$mer_name,$BlockHash,$TxID){ //背景图片 $path_1 = 'img/background_code.png'; //照片 $path_2= 'img/1.png'; //建立图片对象 $image_1 = imagecreatefrompng($path_1); $image_2 = imagecreatefrompng($path_2); //合成图片 imagecopymerge($image_1, $image_2, 0, 0, 10, 10, imagesx($image_2), imagesy($image_2), 100); //这是要插入到图片的文字 $data=date('ymd',time()); $pad=str_pad(1,4,"0",STR_PAD_LEFT); $str=mt_rand(10000000,99999999); $number=$data.$pad.$subtle.$str; $prove="兹证明:\n\n美嘉兔科技有限公司,产品符合溯源规范,认证内容如下:\n特授权期产品在元一众享溯源商城出售"; $prove="兹证明:"; $mer_name = "中国溯源旗舰店"; $merName = $mer_name.",产品符合溯源规范,\n认证内容如下:\n特授权其产品在元一众享溯源商城出售!"; $hash = "930faf66cc24e7a5af56fcdee7c9b726c5869cd763ba5166a22de5f34c3570d5"; $hash = wordwrap($hash,45,"\n",TRUE); $authorization_time = date('Y/m/d',time()); $valid_time = date("Y/m/d",strtotime("+1years",strtotime(date('Y/m/d',time())))); //设置文字颜色 $black = imagecolorallocate($image_1, 0, 0, 0 ); $blue = imagecolorallocate($image_1, 27,142,187); //写的文字用到的字体 $font = 'img/msyh.ttf'; $font2= 'img/msyhbd.ttf'; //在图片里插入文字$black imagettftext($image_1, 20, 0, 290, 340, $black,$font, "证书编号:".$number); imagettftext($image_1, 20, 0, 150, 450, $black,$font, $prove); imagettftext($image_1, 20, 0, 200, 500, $black,$font, $merName); imagettftext($image_1, 20, 0, 140, 730, $blue,$font, "交易哈希:\n".$hash); imagettftext($image_1, 20, 0, 570, 1050, $black,$font, "授权时间:".$authorization_time); imagettftext($image_1, 20, 0, 570, 1100, $black,$font, "有效期至:".$valid_time); //这是合成后的图片保存的路径 $upload_dir = "pic/"; //文件名 $filename="YY"; $data=date('ymd',time()); $pad=str_pad(1,2,"0",STR_PAD_LEFT); $str=mt_rand(100000,999999); $subtle = microtime()*1000000; $jobnumber=$data.$pad.$subtle.$str; $picture=$upload_dir.$filename.$jobnumber.'.png'; imagepng($image_1,$picture); $url = 'http://'.$_SERVER['HTTP_HOST'].'/'.$picture; return $url; }
如果添加的文字过长可使用wordwrap()函数进行设置换行。
例如:
$BlockHash = wordwrap($result['data']['blockhash'],42,"\n",TRUE);