123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- require_once(__DIR__.'/qrlib.php');
- function CreateQRCode($data,$width,$title="",$logofile="",$issave=true,$savefile="")
- {
-
- $filename = "qrcode_" . time() . ".png";
-
- $level = "L";
-
- $size = get_qrcode_size($width);
-
- $padding = 2;
-
- $saveandprint = false;
- if($issave==true){
- if($savefile==""){
- $DIR1="/uploads/qrcode/".MyDate('Ymd',time())."/";
- $FILENAME="QR_".time().GetRandStr(20).".jpg";
- $path=ROOT_PATH.$DIR1;
- if(!file_exists($path)){
- mkdir($path,0777,true);
- }
- $savefile=$path.$FILENAME;
- $return_path=$DIR1.$FILENAME;
- }else{
- $return_path="";
- }
-
- \QRcode::png($data,$savefile,$level,$size,$padding,$saveandprint);
- if($logofile!=""){
- QRCodeLogo($savefile,$logofile);
- }
- if($title!=""){
- QRCodeTitle($savefile,$title);
- }
- return $return_path;
- }else{
-
- \QRcode::png($data,false,$level,$size,$padding,$saveandprint);
- }
- }
- function get_qrcode_size($w)
- {
- $size=floor($w/29*100)/100 + 0.01;
- return $size;
- }
- function QRCodeLogo($Qrfile,$logofile)
- {
- $QR = imagecreatefromstring(file_get_contents($Qrfile));
- $logo = imagecreatefromstring(file_get_contents($logofile));
- $QR_width = imagesx($QR);
- $QR_height = imagesy($QR);
- $logo_width = imagesx($logo);
- $logo_height = imagesy($logo);
- $logo_qr_width = $QR_width / 4;
- $scale = $logo_width/$logo_qr_width;
- $logo_qr_height = $logo_height/$scale;
- $from_width = ($QR_width - $logo_qr_width) / 2;
-
-
- imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
-
- imagejpeg($QR,$Qrfile);
- imagedestroy($QR);
- imagedestroy($logo);
- }
- function QRCodeTitle($Qrfile,$title=""){
- if(!file_exists($Qrfile)){
- return false;
- }
- $place = 40;
- $line_height = 30;
- $fileContent = file_get_contents($Qrfile);
- if(empty($fileContent)){
- return false;
- }
- $QR = imagecreatefromstring($fileContent);
- $QR_width = imagesx($QR);
- $QR_height = imagesy($QR);
- $Img_width = $QR_width;
- $Img_height = $QR_height + $place*2 + $line_height + 20;
- $Img = imagecreatetruecolor($QR_width,$QR_height + $place*2 + $line_height);
-
- $black=imagecolorallocate($Img,3,3,3);
- $white=imagecolorallocate($Img,255,255,255);
-
- imagefill ($Img,0,0,$white);
-
- imagecopyresampled($Img, $QR, 0, 0, 0, 0, $QR_width, $QR_height, $QR_width, $QR_height);
- $pos = [
- 'top' => $QR_height+ $place + 20,
- 'left' => 0,
- 'line_height'=> $line_height,
- 'width' => $QR_width,
- 'pos' => 'C'
- ];
-
-
- $font = PathDS(public_path().'static'.DS.'font'.DS.'MSYH.TTC');
- $font = file_exists($font)?$font:__DIR__.DS.'MSYH.TTC';
-
- $txt = [];
- $strNum = 0;
- for($i=0;$i<mb_strlen($title,"UTF-8");$i++){
- $f = mb_substr($title,$i,1,"UTF-8");
- if(preg_match('/[\x{4e00}-\x{9fa5}]/u',$f)>0){
- $strNum+=1.5;
- }else{
- $strNum+=1;
- }
- }
- $fontSize = intval(($QR_width)/$strNum);
- $fontSize = $fontSize<=60?$fontSize:60;
- $txt[] = [
- 'content' => $title,
- 'font' => $font,
- 'color' => $black,
- 'fontsize' => $fontSize,
- 'blod' => false,
- 'underline' => false
- ];
- draw_txt_to($Img,$pos,$txt);
- ImageJpeg($Img,$Qrfile,80);
- ImageDestroy($Img);
- }
- function draw_txt_to($card, $pos,$txtArr)
- {
-
- $_str_h = $pos["top"];
- $width = $pos["width"];
- $margin_left = $pos["left"];
- $hang_size = $pos["line_height"];
- $pos = $pos['pos'];
- $tp = 0;
- $allline = [];
- $all_temp_string = "";
- foreach($txtArr as $key => $sentence){
- $fontsize = $sentence["fontsize"];
- $fontcolor = $sentence['color'];
- $str = $sentence['content'];
- $font_file = $sentence['font']&&file_exists($sentence['font'])?$sentence['font']:'./MSYH.TTC';
- $blod = $sentence['blod'];
- $underline = $sentence['underline'];
- $temp_string = "";
-
- for ($i = 0; $i < mb_strlen($str); $i++) {
-
- $box = imagettfbbox($fontsize, 0, $font_file, $all_temp_string);
-
- $_string_length = $box[2] - $box[0];
-
- $temptext = mb_substr($str, $i, 1);
-
- $temp = imagettfbbox($fontsize, 0, $font_file, $temptext);
-
- if ($_string_length + $temp[2] - $temp[0] < $width) {
- $temp_string .= mb_substr($str, $i, 1);
- $all_temp_string .= mb_substr($str, $i, 1);
-
- if ($i == mb_strlen($str) - 1) {
- $allline[$tp][] = ['str'=>$temp_string,'top'=>$_str_h,'color'=>$fontcolor,'fontsize'=>$fontsize,'font_file'=>$font_file,'blod'=>$blod,'underline'=>$underline];
- }
- }
-
- else{
-
- $texts = mb_substr($str, $i, 1);
-
- $isfuhao = preg_match("/[\\\\pP]/u", $texts) ? true : false;
-
- if ($isfuhao) {
- $temp_string .= $texts;
- $all_temp_string .= $texts;
-
- $f = mb_substr($str, $i + 1, 1);
- $fh = preg_match("/[\\\\pP]/u", $f) ? true : false;
- if ($fh) {
- $temp_string .= $f;
- $all_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);
- $all_temp_string=rtrim($all_temp_string,$s);
- $i--;
- }
- $allline[$tp][] = ['str'=>$temp_string,'top'=>$_str_h,'color'=>$fontcolor,'fontsize'=>$fontsize,'font_file'=>$font_file,'blod'=>$blod,'underline'=>$underline];
-
- $_str_h += ($hang_size - ($hang_size - $fontsize)/2);
- $tp++;
-
- $temp_string = "";
- $all_temp_string = "";
- }
- }
- }
- foreach($allline as $hang=>$row){
-
- $rowstr = implode(",",array_column($row,'str'));
- $tmp = imagettfbbox($fontsize, 0, $font_file, $rowstr);
- $marginleft = $margin_left;
-
- if($pos=='L'){
- $marginleft = $margin_left;
- }elseif($pos=='R'){
- $marginleft = $margin_left + $width - ($tmp[2] - $tmp[0]);
- }elseif($pos=='C'){
- $marginleft = $margin_left + ($width - ($tmp[2] - $tmp[0])) / 2;
- }
- foreach($row as $sub){
- $tmp = imagettftext($card, $sub['fontsize'], 0, $marginleft, $sub['top'], $sub['color'], $sub['font_file'], $sub['str']);
- if($sub['blod']){
- imagettftext($card, $sub['fontsize'], 0, $marginleft, $sub['top']+1, $sub['color'], $sub['font_file'], $sub['str']);
- imagettftext($card, $sub['fontsize'], 0, $marginleft, $sub['top']-1, $sub['color'], $sub['font_file'], $sub['str']);
-
-
- }
- if($sub['underline']){
- imagefilledrectangle($card,$tmp[0],$tmp[1]+1,$tmp[2],$tmp[3]+3,$sub['color']);
- }
- $marginleft += ($tmp[2] - $tmp[0]);
- }
- }
- return $sub['top'] + $hang_size;
- }
- function is_firstfuhao($str)
- {
- $fuhaos = array("\\", "“", "'", "<", "《",);
- return in_array($str, $fuhaos);
- }
- ?>
|