Procházet zdrojové kódy

Update Qr resize logo

Zhu Jinhui před 6 roky
rodič
revize
0583303c4d
2 změnil soubory, kde provedl 30 přidání a 8 odebrání
  1. 1 1
      src/Library/Qr.php
  2. 29 7
      src/Library/QrCode/Traits/Fill.php

+ 1 - 1
src/Library/Qr.php

@@ -127,7 +127,7 @@ class Qr
             'stroke' => 0,//是否描边
         );
         $options = array_merge($defaults, $options);
-
+        
         \QrCode\QRencode::factory($errorLevel, $pointSize, $margin);
 
         $qrCls = new \QrCode\QRencode();

+ 29 - 7
src/Library/QrCode/Traits/Fill.php

@@ -104,21 +104,42 @@ trait Fill
             imagefilledpolygon($img, $values2, 3, $color);
         }
     }
+    /**
+     * 缩放图片Logo
+     * @param type $im 资源
+     * @param type $maxWidth 最大宽度
+     * @param type $maxHeight 最大高度
+     * @return type
+     */
+    public function resizeLogo($im, $maxWidth, $maxHeight) {
+        $picWidth = imagesx($im);
+        $picHeight = imagesy($im);
+        $newIM = imagecreatetruecolor($maxWidth, $maxHeight);
+
 
+        $almostBlack = imagecolorallocate($newIM,254,254,254);
+        imagefill($newIM,0,0, $almostBlack);
+        imagecolortransparent($newIM, $almostBlack);
+
+        ImageCopyResampled($newIM, $im, 0, 0, 0, 0, $maxWidth, $maxHeight, $picWidth, $picHeight);
+        imagedestroy($im);
+
+        return $newIM;
+    }
     /**
      * 缩放图片
-     * @param type $im
-     * @param type $maxwidth
-     * @param type $maxheight
+     * @param type $im 资源
+     * @param type $maxWidth 最大宽度
+     * @param type $maxHeight 最大高度
      * @return type
      */
-    public function resizeImage($im, $maxwidth, $maxheight)
+    public function resizeImage($im, $maxWidth, $maxHeight)
     {
         $picWidth = imagesx($im);
         $picHeight = imagesy($im);
+        $newIM = imagecreatetruecolor($maxWidth, $maxHeight);
 
-        $newIM = imagecreatetruecolor($maxwidth, $maxheight);
-        ImageCopyResampled($newIM, $im, 0, 0, 0, 0, $maxwidth, $maxheight, $picWidth, $picHeight);
+        ImageCopyResampled($newIM, $im, 0, 0, 0, 0, $maxWidth, $maxHeight, $picWidth, $picHeight);
         imagedestroy($im);
 
         return $newIM;
@@ -200,13 +221,14 @@ trait Fill
 	 * 通过图片资源生成
 	 * @param resource $im 图片资源
 	 * @param resource $logoRes Logo资源
+     * @param int $stroke 是否对logo进行描边
 	 * @return resource
 	 */
 	public function imageAddLogoRes($im, $logoRes, $stroke = 0) {
         //计算宽和高
         $w = imagesx($im);
         $h = imagesy($im);
-        $logoRes = $this->resizeImage($logoRes, min(36, $w / 5), min(36, $h / 5));
+        $logoRes = $this->resizeLogo($logoRes, max(36, $w / 6), max(36, $h / 6));
         $srcWidth = imagesx($logoRes);
         $srcHeight = imagesy($logoRes);
         if($stroke) {