lizhen_gitee 3 năm trước cách đây
mục cha
commit
ddbe0480e0

+ 214 - 0
addons/poster/library/Image2.php

@@ -0,0 +1,214 @@
+<?php
+
+namespace addons\poster\library;
+
+use think\Db;
+
+class Image2
+{
+    protected $target;      //图像资源
+    protected $poster_url;  //海报url
+    protected $model;       //海报模型
+
+    public function __construct()
+    {
+        $this->model = new \app\admin\model\Poster;
+    }
+
+
+    /**
+     * 创建海报
+     * @author Created by lizhen
+     */
+    public function createPosterImage($background,$qrcode_url,$text,$new_file_name)
+    {
+        $this->poster_url = $new_file_name;
+
+        set_time_limit(0);
+        @ini_set('memory_limit', '256M');
+
+        $this->target = imagecreatetruecolor(723, 881); //背景图尺寸
+        $white = imagecolorallocate($this->target, 255, 255, 255);
+        imagefill($this->target, 0, 0, $white);
+
+        $bg = $this->createImage($background);
+        if (!empty($bg)) {
+            $bgWidth = imagesx($bg);
+            $bgHeight = imagesy($bg);
+            $ratio = 640 / $bgWidth;
+            $ratio = 1;
+            $newWidth = imagesx($bg) * $ratio;
+            $newHeight = imagesy($bg) * $ratio;
+
+            $bgClone = imagecreatetruecolor($newWidth, $newHeight);
+            imagecopyresampled($bgClone, $bg, 0, 0, 0, 0, $newWidth, $newHeight, $bgWidth, $bgHeight);
+
+            imagecopy($this->target, $bgClone, 0, 0, 0, 0, $newWidth, $newHeight);
+
+            imagedestroy($bg);
+            imagedestroy($bgClone);
+        }
+
+
+
+        $poster = [
+
+            [
+                "left"=> "125px",
+                "top"=> "300px",
+                "type"=> "img",
+                "width"=> "80px",
+                "height"=> "80px",
+                "src"=> $qrcode_url,
+            ]
+        ];
+
+        //写字
+        //$text = '一二三四五六七八九十一二三四五';
+        $text_len = mb_strlen($text);
+        if($text_len > 10){
+            $poster[] = [
+                "left"=> "100px",
+                "top"=> "145px",
+                "type"=> "nickname",
+                "width"=> "80px",
+                "height"=> "40px",
+                "size"=> "14px",
+                "color"=> "#FFFFFF",
+                "text"=> mb_substr($text,0,10),
+            ];
+            $poster[] = [
+                "left"=> "100px",
+                "top"=> "175px",
+                "type"=> "nickname",
+                "width"=> "80px",
+                "height"=> "40px",
+                "size"=> "14px",
+                "color"=> "#FFFFFF",
+                "text"=> mb_substr($text,10,10),
+            ];
+        }else{
+            $poster[] = [
+                "left"=> "140px",
+                "top"=> "220px",
+                "type"=> "nickname",
+                "width"=> "80px",
+                "height"=> "40px",
+                "size"=> "14px",
+                "color"=> "#FF0000",
+                "text"=> $text,
+            ];
+        }
+        //写字
+
+
+        foreach ($poster as $d) {
+            $d = $this->getRealData($d);
+            if ($d['type'] == 'img' && isset($d['src'])) {
+                $this->mergeImage($d, $d['src']);
+            } elseif ($d['type'] == 'nickname') {
+                $this->mergeText($d, $d['text']);
+            }
+        }
+
+        imagepng($this->target, $this->poster_url);
+        imagedestroy($this->target);
+
+        return $this->poster_url;
+    }
+
+    public function mergeImage($data, $imgurl)
+    {
+        $img = $this->createImage($imgurl);
+        if (!$img) {
+            return false;
+        }
+        $w = imagesx($img);
+        $h = imagesy($img);
+        imagecopyresized($this->target, $img, $data['left'], $data['top'], 0, 0, $data['width'], $data['height'], $w, $h);
+        imagedestroy($img);
+    }
+
+    public function mergeText($data, $text)
+    {
+        $font = ROOT_PATH . '/public/assets/fonts/SourceHanSansK-Regular.ttf';
+        if (!isset($data['color'])) {
+            $data['color'] = '#000';
+        }
+        $colors = $this->hex2rgb($data['color']);
+        $color = imagecolorallocate($this->target, $colors['red'], $colors['green'], $colors['blue']);
+        imagettftext($this->target, $data['size'], 0, $data['left'], $data['top'] + $data['size'], $color, $font, $text);
+    }
+
+    public function createImage($imgurl)
+    {
+        if (strpos($imgurl, 'data:image') !== false) {
+            $imgurl = '/assets/addons/poster/images/head.jpg';
+        }
+        if (strpos($imgurl, '://') === false) {
+            $imgurl = ROOT_PATH . '/public' . $imgurl;
+            if (!is_file($imgurl)) {
+                return '';
+            }
+        } else {
+            $domain = 'thirdwx.qlogo.cn';
+            if (strpos($imgurl, $domain) !== false) {
+                $ip = gethostbyname($domain);
+                if ($ip) {
+                    $imgurl = str_replace($domain, $ip, $imgurl);
+                }
+            }
+        }
+        @$content = file_get_contents($imgurl);
+        if ($content) {
+            return imagecreatefromstring($content);
+        }
+        return '';
+    }
+
+    private function getRealData($data)
+    {
+        $data['left'] = isset($data['left']) ? intval(str_replace('px', '', $data['left'])) * 2 : 0;
+        $data['top'] = isset($data['top']) ? intval(str_replace('px', '', $data['top'])) * 2 : 0;
+        $data['width'] = isset($data['width']) ? intval(str_replace('px', '', $data['width'])) * 2 : 0;
+        $data['height'] = isset($data['height']) ? intval(str_replace('px', '', $data['height'])) * 2 : 0;
+        $data['size'] = isset($data['size']) ? intval(str_replace('px', '', $data['size'])) * 2 : 0;
+        return $data;
+    }
+
+    /**
+     * 递归创建文件夹
+     * @author Created by Xing <464401240@qq.com>
+     */
+    private function mkdirs($path)
+    {
+        if (!is_dir($path)) {
+            $this->mkdirs(dirname($path));
+            mkdir($path);
+        }
+        return is_dir($path);
+    }
+
+    /**
+     * hex转rgb
+     * @param $colour
+     * @return array|bool
+     */
+    private 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]);
+        } elseif (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);
+    }
+}

+ 1 - 0
application/api/controller/Baseconfig.php

@@ -17,6 +17,7 @@ class Baseconfig extends Api
 
         $config = [
             'domain_name' => config('site.domain_name'),
+            'domain_cdnurl' => config('site.domain_cdnurl'),
             'android_update_num' => config('site.android_update_num'),
             'android_update_version' => config('site.android_update_version'),
             'ios_update_num' => config('site.ios_update_num'),

+ 49 - 0
application/api/controller/User.php

@@ -169,6 +169,55 @@ class User extends Api
         $this->success('success',$rs);
     }
 
+    public function testhaibao(){
+        $haibao = $this->haibao($this->auth->id,['introcode'=>$this->auth->introcode]);
+        dump($haibao);
+    }
+
+    //海报
+    public function haibao($player_id,$data){
+
+        //下载页二维码,没必要保留
+        $params = [
+            'text' => config('site.domain_name').'/index/index/appdownload',
+            'size' => 100,
+            'logo' => false,
+            'label' => false,
+            'padding' => 0,
+        ];
+        $qrCode = \addons\qrcode\library\Service::qrcode($params);
+        $qrcode_path = 'uploads/hbplayer/'.date('Ymd');
+        mk_dir($qrcode_path);
+        $download_qrcode = $qrcode_path.'/download.png';
+        $qrCode->writeFile($download_qrcode);
+
+        //海报
+        $haibao = $this->createhaibao($download_qrcode,$player_id,$data);
+
+        return $haibao;
+    }
+
+    public function createhaibao($download_qrcode,$player_id,$sub_data){
+
+        //背景图
+        $background = config('site.domain_name').'/assets/img/haibao.png';
+        //海报图片路径
+        $new_path = 'uploads/hbplayer/'.date("Ymd").'/';
+        mk_dir($new_path);
+        $wap_file_name  = $new_path .'wap_player_'. $player_id . '.png';
+
+        //二维码
+        $download_qrcode= config('site.domain_name').'/'.$download_qrcode;
+
+
+        //合成wap图片
+        $image = new \addons\poster\library\Image2();
+        $imgurl = $image->createPosterImage($background,$download_qrcode,$sub_data['introcode'],$wap_file_name);
+
+        return '/'.$wap_file_name;
+    }
+
+
     //申请真人认证
     public function apply_real_confirm(){
         $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');

+ 4 - 8
application/common.php

@@ -493,7 +493,7 @@ function list_birthday_age($list){
     return $list;
 }
 
-//结果集信息里,多个字段需要增加domain_name
+//结果集信息里,多个字段需要增加domain_cdnurl
 function list_domain_image($list,$field){
     if(!$list || empty($list)){
         return $list;
@@ -503,7 +503,7 @@ function list_domain_image($list,$field){
     }
     return $list;
 }
-//单条信息里,多个字段需要增加domain_name
+//单条信息里,多个字段需要增加domain_cdnurl
 //支持image,images
 function info_domain_image($data,$field){
     if(!$data || empty($data)){
@@ -516,23 +516,20 @@ function info_domain_image($data,$field){
     }
     return $data;
 }
-//支持单个字段,需要增加domain_name
+//支持单个字段,需要增加domain_cdnurl
 //支持image,images
 function one_domain_image($one){
     if(!$one){
         return $one;
     }
-    //$domain_name = config('site.domain_name');
     if(strpos($one,',')){
         //逗号隔开的多个图片
         $one = explode(',',$one);
         foreach($one as $k => $v){
-            //$one[$k] = $v ? $domain_name.$v : $v;
             $one[$k] = localpath_to_netpath($v);
         }
         $one = implode(',',$one);
     }else{
-        //$one = $one ? $domain_name.$one : $one;
         $one = localpath_to_netpath($one);
     }
     return $one;
@@ -545,8 +542,7 @@ function localpath_to_netpath($path)
     } elseif (strrpos($path, 'http') !== false) {
         return $path;
     } else {
-        return config('site.domain_name') . str_replace("\\", "/", $path);
-        //return config('site.domain_name') . $path;
+        return config('site.domain_cdnurl') . str_replace("\\", "/", $path);
     }
 }
 

BIN
public/assets/img/haibao.png