Bladeren bron

Add font to qr

Zhu Jinhui 7 jaren geleden
bovenliggende
commit
80d1389c82
3 gewijzigde bestanden met toevoegingen van 125 en 5 verwijderingen
  1. 12 1
      src/Library/Qr.php
  2. 113 4
      src/Library/QrCode/Traits/Fill.php
  3. BIN
      src/Library/ttfs/msyh.ttc

+ 12 - 1
src/Library/Qr.php

@@ -57,7 +57,7 @@ class Qr
      */
     public function creator($txt, $pointSize = 8, $margin = 1, $errorLevel = 4)
     {
-        if (!$txt) return;
+        if (!$txt) throw new \Exception('请输入要生成的内容');
         return \QrCode\QRcode::png($txt, false, $errorLevel, $pointSize, $margin);
     }
 
@@ -73,11 +73,15 @@ class Qr
      */
     public function creatorColor($txt, $pointSize = 10, $margin = 1, $errorLevel = 4, $options = array())
     {
+        if (!$txt) throw new \Exception('请输入要生成的内容');
         $defaults = array(
             'width' => 240, //图片大小
             'margin' => 2,
             'logo' => '', //logo
             'bg' => '',
+            'fontSize' => 14,
+            'fontPath' => __DIR__ . DS . 'ttfs'. DS . 'msyh.ttc',
+            'fontColor' => '#000000',
             'pointColor' => '', //定点颜色
             'inPointColor' => '',//内定点
             'frontColor' => '#000000',//前景色
@@ -109,6 +113,8 @@ class Qr
         //保存图片
         $im = $this->resizeImage($qrImage, $options['width'], $options['width']);
 
+
+
         //增加logo
         if (!empty($options['logo'])) {
             $im = $this->imageAddLogo($im, $options['logo']);
@@ -118,6 +124,11 @@ class Qr
         if (!empty($options['bg'])) {
             $im = $this->imageAddBG($im, $options['bg']);
         }
+
+        if(!empty($options['text']))
+        {
+            $im = $this->imageAddText($im, $options['text'], $options['fontSize'], $options['fontPath'], $options);
+        }
         //保存图片
         header('Content-type:image/png');
         imagepng($im);

+ 113 - 4
src/Library/QrCode/Traits/Fill.php

@@ -1,4 +1,5 @@
 <?php
+
 namespace QrCode\traits;
 
 trait Fill
@@ -114,11 +115,11 @@ trait Fill
         $picWidth = imagesx($im);
         $picHeight = imagesy($im);
 
-        $newim = imagecreatetruecolor($maxwidth, $maxheight);
-        ImageCopyResampled($newim, $im, 0, 0, 0, 0, $maxwidth, $maxheight, $picWidth, $picHeight);
+        $newIM = imagecreatetruecolor($maxwidth, $maxheight);
+        ImageCopyResampled($newIM, $im, 0, 0, 0, 0, $maxwidth, $maxheight, $picWidth, $picHeight);
         imagedestroy($im);
 
-        return $newim;
+        return $newIM;
 
     }
 
@@ -158,7 +159,6 @@ trait Fill
     //图片增加logo
     public function imageAddLogo($im, $logo)
     {
-
         //计算宽和高
         $w = imagesx($im);
         $h = imagesy($im);
@@ -229,6 +229,115 @@ trait Fill
         return $im;
     }
 
+    /**
+     * 在二维码下边生成图片
+     *
+     * @param resource $im 图片资源
+     * @param string $text 文字
+     * @param int $fontSize 字体
+     * @param string $fontPath 字体路径
+     * @param array $options 选型
+     * @return resource
+     */
+    public function imageAddText($im, $text, $fontSize = 14, $fontPath = '', $options = array())
+    {
+        $fontSize = $fontSize ?? 14;
+        if (empty($options['bgColor'])) {
+            $options['bgColor'] = '#FFFFFF';
+        }
+        if (empty($options['fontColor'])) {
+            $options['fontColor'] = $options['frontColor'] ?? '#000000';
+        }
+
+        //计算宽和高
+        $w = imagesx($im);
+        $h = imagesy($im);
+
+        $bgColor = $this->hex2rgb($options['bgColor']);
+        $fontColor = $this->hex2rgb($options['fontColor']);
+        //自动换行
+        $text = $this->autoWrap($text, 0, $fontSize, $fontPath, $w);
+        $box = $this->imageTTFBoxExtended($fontSize, 0, $fontPath, $text);
+        $maxHeight = $h + $box['height'] + 20;
+
+        $newIM = imagecreatetruecolor($w, $maxHeight);
+
+        //背景色
+        $background = imagecolorallocatealpha($newIM, $bgColor['r'], $bgColor['g'], $bgColor['b'], 100);
+        imagefill($newIM, 0, 0, $background);
+        imagecopymerge($newIM, $im, 0, 0, 0, 0, $w, $h, 100);
+
+        $fontColor = imagecolorallocatealpha($newIM, $fontColor['r'], $fontColor['g'], $fontColor['b'], 50);
+        imagettftext($newIM, $fontSize, 0, max(0, ($w - $box['width']) / 2), $h + 25, $fontColor, $fontPath, $text);
+        imagedestroy($im);
+        return $newIM;
+    }
+
+    /**
+     * 获取文字的宽高
+     *
+     * @param int $size 字体大小
+     * @param int $angle
+     * @param string $fontPath 字体路径
+     * @param string $text 文字
+     * @return array
+     */
+    public function imageTTFBoxExtended($size, $angle, $fontPath, $text)
+    {
+        $box = imagettfbbox($size, $angle, $fontPath, $text);
+
+        //calculate x baseline
+        if ($box[0] >= -1) {
+            $box['x'] = abs($box[0] + 1) * -1;
+        } else {
+            //$box['x'] = 0;
+            $box['x'] = abs($box[0] + 2);
+        }
+
+        //calculate actual text width
+        $box['width'] = abs($box[2] - $box[0]);
+        if ($box[0] < -1) {
+            $box['width'] = abs($box[2]) + abs($box[0]) - 1;
+        }
+
+        //calculate y baseline
+        $box['y'] = abs($box[5] + 1);
+
+        //calculate actual text height
+        $box['height'] = abs($box[7]) - abs($box[1]);
+        if ($box[3] > 0) {
+            $box['height'] = abs($box[7] - $box[1]) - 1;
+        }
+        return $box;
+    }
+
+    /**
+     * 文字自动换行
+     *
+     * @param int $fontSize [字体大小]
+     * @param int $angle [角度]
+     * @param string $fontFace [字体名称]
+     * @param string $string [字符串]
+     * @param int $width [预设宽度]
+     */
+    function autoWrap($text, $angle, $fontSize, $fontFace, $width)
+    {
+        $content = "";
+        // 将字符串拆分成一个个单字 保存到数组 letter 中
+        preg_match_all("/./u", $text, $arr);
+        $letter = $arr[0];
+        foreach ($letter as $l) {
+            $testStr = $content . " " . $l;
+            $testBox = imagettfbbox($fontSize, $angle, $fontFace, $testStr);
+            // 判断拼接后的字符串是否超过预设的宽度
+            if (($testBox[2] > $width) && ($content !== "")) {
+                $content .= PHP_EOL;
+            }
+            $content .= $l;
+        }
+        return $content;
+    }
+
     /**
      * 16进制颜色转换为RGB色值
      * @method hex2rgb

BIN
src/Library/ttfs/msyh.ttc