| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models;
- use app\constants\OptionSetting;
- use app\models\Qrcode;
- use app\models\SaasUser;
- use app\models\Topic;
- use app\models\VideoGoodsList;
- use app\modules\admin\models\UserCenterForm;
- use app\modules\alliance\models\ApiModel;
- use app\utils\CurlHelper;
- use claviska\SimpleImage;
- use yii\base\Model;
- use app\utils\ShareQrcode;
- use app\models\Goods;
- use app\models\Store;
- use app\models\Option;
- use yii\helpers\Json;
- class ShareQrcodeForm extends ApiModel
- {
- public $store_id;
- public $user;
- public $saas_id;
- public $user_id;
- public $goods_id;
- public $vl_id;
- public $num;
- public $type; //0--商城海报 1--秒杀海报 2--拼团海报 3--预约海报 4--分销海报 5--砍价海报 6--专题海报 7--抽奖海报 8--布数宝海报
- public function rules()
- {
- return [
- [['type'], 'required'],
- [['type'], 'in', 'range' => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]],
- [['goods_id', 'num', 'vl_id'], 'integer']
- ];
- }
- public function search()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- if ($this->type == 0) {
- return $this->goods_qrcode();
- } elseif ($this->type == 1) {
- // return $this->ms_goods_qrcode();
- } elseif ($this->type == 2) {
- // return $this->pt_goods_qrcode();
- } elseif ($this->type == 3) {
- // return $this->yy_goods_qrcode();
- } elseif ($this->type == 4) {
- return $this->share_qrcode();
- } elseif ($this->type == 5) {
- // return $this->bargain_qrcode();
- } elseif ($this->type == 6) {
- return $this->topic_qrcode();
- } elseif ($this->type == 7) {
- // return $this->lottery_qrcode();
- } elseif ($this->type == 8) {
- // return $this->step_qrcode();
- } elseif ($this->type == 9) {
- return $this->new_share_qrcode();
- } elseif ($this->type == 10) {
- return $this->video_goods_qrcode();
- } else {
- return [
- 'code' => 1,
- 'msg' => 'error'
- ];
- }
- }
- /**
- * 专题海报
- * @return [type] [description]
- */
- public function aggregateSaasQrcode($saas_qrcode, $business_no = null)
- {
- $store = Store::findOne($this->store_id);
- if (!$store) {
- return [
- 'code' => 1,
- 'msg' => '店铺不存在',
- ];
- }
- $store_name = $store->name;
- $store_logo = $store->logo;
- $qr_save_path = \Yii::$app->basePath . '/runtime/image/';
- $version = cyy_version();
- $qr_save_name = md5("aggregateSaasQrcode&v={$version}&store_id={$store->id}") . '.jpg';
- $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $qr_save_name);
- if (file_exists($qr_save_path . $qr_save_name)) {
- return [
- 'code' => 0,
- 'data' => [
- 'store_name' => $store_name,
- 'pic_url' => $pic_url . '?v=' . time(),
- ],
- ];
- }
- $logo_pic_path = $this->saveTempImage($store_logo);
- if (!$logo_pic_path) {
- return [
- 'code' => 1,
- 'msg' => '获取店铺logo失败',
- ];
- }
- $aggregateSaasQrcode_dst = \Yii::$app->basePath . '/web/v1/statics/images/aggregateSaasQrcode.png';
- $font_path = \Yii::$app->basePath . '/web/v1/statics/font/AaBanSong.ttf';
- $aggregateSaasQrcode = @(new \claviska\SimpleImage($aggregateSaasQrcode_dst));
- $saas_qrcode = @(new \claviska\SimpleImage($saas_qrcode));
- $store_logo = @(new \claviska\SimpleImage($logo_pic_path));
- $name_size = 50;
- $name_width = 660;
- $strlen = mb_strlen($store_name);
- $strlen = $strlen < 4 ? 4 : ($strlen >= 8 ? 8 : $strlen);
- $fwith = $name_size * $strlen + 200;
- $fleft = (747 - 100 - $fwith) / 2;
- $name = $this->autowrap($name_size, 0, $font_path, $store_name, $name_width, 1);
- $search = ['“', '”', ':', '’', '‘'];
- $name = str_replace($search, '', $name);
- $aggregateSaasQrcode->text($name, [
- 'fontFile' => $font_path,
- 'size' => $name_size,
- 'color' => '#E61A28',
- 'anchor' => 'top left',
- 'xOffset' => $fleft + 250,
- 'yOffset' => 50,
- 'shadow' => array(
- 'x' => 2,
- 'y' => 0,
- 'color' => '#E61A28',
- ),
- ]);
- $name = $this->autowrap($name_size, 0, $font_path, '扫码支付', $name_width, 1);
- $search = ['“', '”', ':', '’', '‘'];
- $name = str_replace($search, '', $name);
- $aggregateSaasQrcode->text($name, [
- 'fontFile' => $font_path,
- 'size' => $name_size,
- 'color' => '#E61A28',
- 'anchor' => 'top left',
- 'xOffset' => $fleft + 250,
- 'yOffset' => 120,
- 'shadow' => array(
- 'x' => 1,
- 'y' => 0,
- 'color' => '#E61A28',
- ),
- ]);
-
- $aggregateSaasQrcode->line($fleft + 220, 50, $fleft + 220, 166, '#cc1a28', 3);
- $store_logo->resize(130, 130);
- // $store_logo->border('#ff0000', 3);
- $aggregateSaasQrcode->overlay($store_logo, 'top left', 1, $fleft + 50, 40);
- $saas_qrcode->resize(520, 520);
- $aggregateSaasQrcode->overlay($saas_qrcode, 'top center', 1, 0, 500);
-
- if($business_no){
- $name = $this->autowrap(18, 0, $font_path, $business_no, $name_width, 1);
- $search = ['“', '”', ':', '’', '‘'];
- $name = str_replace($search, '', $name);
- $aggregateSaasQrcode->text($name, [
- 'fontFile' => $font_path,
- 'size' => 25,
- 'color' => '#ffffff',
- 'anchor' => 'top',
- 'xOffset' => 0,
- 'yOffset' => 0,
- 'shadow' => array(
- 'x' => 2,
- 'y' => 400,
- 'color' => '#ffffff',
- ),
- ]);
- }
- //保存图片
- $aggregateSaasQrcode->toFile($qr_save_path . $qr_save_name, 'image/jpeg', 85);
- //删除临时图片
- unlink($logo_pic_path);
- return [
- 'code' => 0,
- 'data' => [
- 'store_name' => $store_name,
- 'pic_url' => $pic_url . '?v=' . time(),
- ],
- ];
- }
- public function goods_qrcode()
- {
- if (!$this->goods_id) {
- return [
- 'code' => 1,
- 'msg' => '未知的商品'
- ];
- }
- $goods = Goods::findOne($this->goods_id);
- if (!$goods) {
- return [
- 'code' => 1,
- 'msg' => '商品不存在',
- ];
- }
- $store_name = Option::get('name', $this->store_id, 'store');
- $store_name = $store_name ? $store_name['value'] : '';
- $goods_pic_obj = $goods->getGoodsPic(0);
- if ($goods_pic_obj) {
- $goods_pic_url = $goods->getGoodsPic(0)->pic_url;
- } else {
- return [
- 'code' => 1,
- 'msg' => '获取商品图片失败',
- ];
- }
- $goods_pic_save_path = \Yii::$app->basePath . '/web/temp/';
- $version = cyy_version();
- $goods_pic_save_name = md5("v={$version}&goods_id={$goods->id}&goods_name={$goods->name}&store_name={$store_name}&saas_id={$this->saas_id}&goods_pic_url={$goods_pic_url}&type=0") . '.jpg';
- $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/temp/' . $goods_pic_save_name);
- if (file_exists($goods_pic_save_path . $goods_pic_save_name)) {
- return [
- 'code' => 0,
- 'data' => [
- 'goods_name' => $goods->name,
- 'pic_url' => $pic_url . '?v=' . time(),
- ],
- ];
- }
- $goods_pic_path = $this->saveTempImage($goods_pic_url);
- if (!$goods_pic_path) {
- return [
- 'code' => 1,
- 'msg' => '获取商品海报失败:商品图片丢失',
- ];
- }
- $goods_qrcode_dst = \Yii::$app->basePath . '/web/v1/statics/images/goods-qrcode-dst.jpg';
- $font_path = \Yii::$app->basePath . '/web/v1/statics/font/AaBanSong.ttf';
- $goods_qrcode = @(new \claviska\SimpleImage($goods_qrcode_dst));
- $goods_pic = @(new \claviska\SimpleImage($goods_pic_path));
- //获取小程序码图片
- $scene = "gid:{$goods->id},uid:{$this->saas_id},store_id:{$this->store_id}";
- if ($goods->product_type === 4) {
- $page = 'marketing/integral/goods/index';
- } else {
- $page = 'goods/goods/goods';
- }
- if (\is_app_platform()) {
- $page = \Yii::$app->request->hostInfo . '/h5/#/' . $page . '?scene=' . $scene . ',alliance:1';
- }
- $wxapp_qrcode_file_res = $this->getQrcode($scene, 240, $page);
- if (!isset($wxapp_qrcode_file_res['root_path'])) {
- unlink($goods_pic_path);
- return [
- 'code' => 1,
- 'msg' => '获取商品海报失败:获取小程序码失败',
- ];
- }
- $wxapp_qrcode_file_path = $wxapp_qrcode_file_res['root_path'];
- $wxapp_qrcode = @(new \claviska\SimpleImage($wxapp_qrcode_file_path));
- $name_size = 30;
- $name_width = 900;
- //商品名称处理换行
- $name = $this->autowrap($name_size, 0, $font_path, $goods->name, $name_width, 2);
- //加商品名称
- $search = ['“', '”', ':', '’', '‘'];
- $name = str_replace($search, '', $name);
- $goods_qrcode->text($name, [
- 'fontFile' => $font_path,
- 'size' => $name_size,
- 'color' => '#333333',
- 'anchor' => 'top left',
- 'xOffset' => 40,
- 'yOffset' => 750,
- ]);
- //裁剪商品图片
- $goods_pic->resize(670, 670);
- //附加商品图片
- $goods_qrcode->overlay($goods_pic, 'top left', 1, 40, 40);
- //加商品价格
- if ($goods->is_negotiable) {
- $goods_qrcode->text('价格面议', [
- 'fontFile' => $font_path,
- 'size' => 45,
- 'color' => '#ff4544',
- 'anchor' => 'top left',
- 'xOffset' => 30,
- 'yOffset' => 910,
- ]);
- } else {
- $goods_qrcode->text('¥' . $goods->price, [
- 'fontFile' => $font_path,
- 'size' => 45,
- 'color' => '#ff4544',
- 'anchor' => 'top left',
- 'xOffset' => 30,
- 'yOffset' => 910,
- ]);
- if (intval($goods->product_type) === 4) {
- $goods_qrcode->text('¥' . $goods->price . ' + ' . $goods->integral_price . '积分', [
- 'fontFile' => $font_path,
- 'size' => 45,
- 'color' => '#ff4544',
- 'anchor' => 'top left',
- 'xOffset' => 30,
- 'yOffset' => 910,
- ]);
- }
- }
- //加商城名称
- $goods_qrcode->text($store_name, [
- 'fontFile' => $font_path,
- 'size' => 20,
- 'color' => '#888888',
- 'anchor' => 'top left',
- 'xOffset' => 40,
- 'yOffset' => 1170,
- ]);
- //调整小程序码图片
- $wxapp_qrcode->resize(240, 240);
- //附加小程序码图片
- $goods_qrcode->overlay($wxapp_qrcode, 'top left', 1, 470, 1040);
- //保存图片
- $goods_qrcode->toFile($goods_pic_save_path . $goods_pic_save_name, 'image/jpeg', 85);
- //删除临时图片
- unlink($goods_pic_path);
- unlink($wxapp_qrcode_file_path);
- return [
- 'code' => 0,
- 'data' => [
- 'goods_name' => $goods->name,
- 'pic_url' => $pic_url . '?v=' . time(),
- ],
- ];
- }
- //分销海报
- public function share_qrcode()
- {
- $save_root = \Yii::$app->basePath . '/web/temp/';
- if (!is_dir($save_root)) {
- mkdir($save_root);
- file_put_contents($save_root . '.gitignore', "*\r\n!.gitignore");
- }
- $version = cyy_version();
- $save_name = md5("v={$version}&store_id={$this->store_id}&user_id={$this->saas_id}") . '.jpg';
- $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/temp/' . $save_name);
- if (file_exists($save_root . $save_name)) {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $pic_url . '?v=' . time()
- ];
- }
- $store_qrcode = Qrcode::findOne(['store_id' => $this->store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_SHARE]);
- if (!$store_qrcode) {
- return [
- 'code' => 1,
- 'msg' => '请先在后台设置分销海报'
- ];
- }
- //昵称位置
- $font_position = json_decode($store_qrcode->font_position, true);
- //小程序码位置
- $qrcode_position = json_decode($store_qrcode->qrcode_position, true);
- //头像位置
- $avatar_position = json_decode($store_qrcode->avatar_position, true);
- //头像大小
- $avatar_size = json_decode($store_qrcode->avatar_size, true);
- //小程序码大小
- $qrcode_size = json_decode($store_qrcode->qrcode_size, true);
- //昵称大小
- $font_size = json_decode($store_qrcode->font, true);
- //背景图下载到临时目录
- $qrcode_bg = $this->saveTempImage($store_qrcode->qrcode_bg);
- if (!$qrcode_bg) {
- return [
- 'code' => 1,
- 'msg' => '获取背景图片失败'
- ];
- }
- //用户头像下载到临时目录
- $user_avatar = $this->saveTempImage(get_saas_user()->avatar);
- if (!$user_avatar) {
- return [
- 'code' => 1,
- 'msg' => '获取用户头像失败'
- ];
- }
- //背景图宽高
- list($qrcode_bg_w, $qrcode_bg_h) = getimagesize($qrcode_bg);
- if ($qrcode_bg_w == 0) {
- return [
- 'code' => 1,
- 'msg' => '获取背景图片失败'
- ];
- }
- //文字字体
- $font_path = \Yii::$app->basePath . '/web/statics/font/AaBanSong.ttf';
- //比例尺
- $percent = 750 / 300;
- //获取小程序码图片
- $width = doubleval($qrcode_size['w'] * $percent);
- $scene = "uid:{$this->saas_id},store:{$this->store_id}";
- $is_platform = 0;
- if (\Yii::$app->prod_is_shangmeng() || \Yii::$app->prod_is_shangmengduli()) {
- $is_platform = 1;
- $scene = "uid:{$this->saas_id}";
- }
- $wxapp_qrcode_file_res = ShareQrcode::wxQrcode(null, $scene, $width, $is_platform);
- // $wxapp_qrcode_file_res = $this->getQrcode($scene);
- $wxapp_qrcode_file_path = $wxapp_qrcode_file_res['root_path'];
- if (! file_exists($wxapp_qrcode_file_path)) {
- return [
- 'code' => 1,
- 'msg' => '获取分享海报失败:获取小程序码失败',
- ];
- }
- //获取背景图
- $qrcode_bg_dst = @(new \claviska\SimpleImage($qrcode_bg));
- $qrcode_bg_dst->resize(750, 1200);
- //获取小程序码
- if (isset($qrcode_size['c']) && $qrcode_size['c'] == 'true') {
- list($w, $h) = getimagesize($wxapp_qrcode_file_path);
- $wxapp_qrcode_file_path = $this->test($wxapp_qrcode_file_path, $save_root, $w, $w);
- }
- $wxapp_qrcode_dst = @(new \claviska\SimpleImage($wxapp_qrcode_file_path));
- $wxapp_qrcode_dst->resize($width, $width);
- //将小程序码添加到背景图
- $qrcode_x = $qrcode_position['x'] * $percent;
- $qrcode_y = $qrcode_position['y'] * $percent;
- if ($qrcode_x >= 0 && $qrcode_x <= 750 && $qrcode_y >= 0 && $qrcode_y <= 1200) {
- $qrcode_bg_dst->overlay($wxapp_qrcode_dst, 'top left', 1, $qrcode_x, $qrcode_y);
- }
- if ($avatar_size['w'] > 0) {
- //获取头像
- $avatar_w = $avatar_size['w'] * $percent;
- $avatar_h = $avatar_size['h'] * $percent;
- $avatar_x = $avatar_position['x'] * $percent;
- $avatar_y = $avatar_position['y'] * $percent;
- if ($avatar_x >= 0 && $avatar_x <= 750 && $avatar_y >= 0 && $avatar_y <= 1200) {
- list($w, $h) = getimagesize($user_avatar);
- $user_avatar = $this->test($user_avatar, $save_root, $w, $h);
- $avatar_dst = @(new \claviska\SimpleImage($user_avatar));
- //裁剪头像
- $avatar_dst->resize($avatar_w, $avatar_h);
- //将头像添加到背景图
- $qrcode_bg_dst->overlay($avatar_dst, 'top left', 1, $avatar_x, $avatar_y);
- }
- }
- if ($font_size['size'] > 0) {
- // $color = \app\models\Color::find()->andWhere(['color' => (int)$font_size['color']])->asArray()->one();
- //附加用户昵称
- $font = $font_size['size'] * $percent * 0.74;
- $font_x = $font_position['x'] * $percent;
- $font_y = $font_position['y'] * $percent + 1;
- if ($font_x >= 0 && $font_x <= 750 && $font_y >= 0 && $font_y <= 1200) {
- $qrcode_bg_dst->text(get_saas_user()->name, [
- 'fontFile' => $font_path,
- 'size' => $font,
- 'color' => $font_size['color'],
- 'anchor' => 'top left',
- 'xOffset' => $font_x,
- 'yOffset' => $font_y,
- ]);
- }
- }
- //保存图片
- $qrcode_bg_dst->toFile($save_root . $save_name, 'image/jpeg', 85);
- //删除临时图片
- unlink($qrcode_bg);
- unlink($user_avatar);
- unlink($wxapp_qrcode_file_path);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $pic_url . '?v=' . time()
- ];
- }
- // 分销海报
- public function new_share_qrcode()
- {
- $save_root = \Yii::$app->basePath . '/web/temp/';
- if (!is_dir($save_root)) {
- mkdir($save_root);
- file_put_contents($save_root . '.gitignore', "*\r\n!.gitignore");
- }
- // 获取后台配置
- $option = Option::get('qrcode', $this->store_id, 'bonus_pool')['value'];
- $option = Json::decode($option);
- if (empty($option)) {
- return [
- 'code' => 1,
- 'msg' => '请先后台设置'
- ];
- }
- $save_name = md5("share=new&store_id={$this->store_id}&user_id={$this->user_id}") . '.jpg';
- $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/temp/' . $save_name);
-
- if (file_exists($save_root . $save_name)) {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => ['data' => $pic_url . '?v=' . time(), 'background' => $option['backgroundImage']]
- ];
- }
- //用户头像下载到临时目录
- $user_avatar = $this->saveTempImage(get_saas_user()->avatar);
- if (!$user_avatar) {
- return [
- 'code' => 1,
- 'msg' => '获取用户头像失败'
- ];
- }
- //文字字体
- $font_path = \Yii::$app->basePath . '/web/statics/font/AaBanSong.ttf';
- //获取小程序码图片
- $width = doubleval(80);
- $scene = "uid:{$this->user_id},store:{$this->store_id}";
- \Yii::warning($scene);
- $wxapp_qrcode_file_res = $this->getQrcode($scene);
- $wxapp_qrcode_file_path = $wxapp_qrcode_file_res['root_path'];
- if (! file_exists($wxapp_qrcode_file_path)) {
- return [
- 'code' => 1,
- 'msg' => '获取分享海报失败:获取小程序码失败',
- ];
- }
- list($w, $h) = getimagesize($wxapp_qrcode_file_path);
- $wxapp_qrcode_file_path = $this->test($wxapp_qrcode_file_path, $save_root, $w, $w);
- $test_color = SimpleImage::normalizeColor(!empty($option['backgroundColor'])?$option['backgroundColor']:"#ffffff");
- $image = imagecreatetruecolor(321, 349);//图片大小
- $white = imagecolorallocate($image, $test_color['red'], $test_color['green'], $test_color['blue']);//图片的rgb
- imagefill($image, 0, 0, $white);
- $share_image = \Yii::$app->basePath . '/web/temp/new_share.jpg';
- //保存图片
- imagegif($image, $share_image);
- //获取背景图
- $qrcode_bg_dst = @(new \claviska\SimpleImage($share_image));
- $qrcode_bg_dst->resize(321, 349);
- $wxapp_qrcode_dst = @(new \claviska\SimpleImage($wxapp_qrcode_file_path));
- $wxapp_qrcode_dst->resize($width, $width);
- //将小程序码添加到背景图
- $qrcode_x = 214;
- $qrcode_y = 242;
- if ($qrcode_x >= 0 && $qrcode_x <= 321 && $qrcode_y >= 0 && $qrcode_y <= 349) {
- $qrcode_bg_dst->overlay($wxapp_qrcode_dst, 'left top', 1, $qrcode_x, $qrcode_y);
- }
- //获取头像
- $avatar_w = 66;
- $avatar_h = 66;
- $avatar_x = 27;
- $avatar_y = 39;
- if ($avatar_x >= 0 && $avatar_x <= 321 && $avatar_y >= 0 && $avatar_y <= 349) {
- list($w, $h) = getimagesize($user_avatar);
- $user_avatar = $this->test($user_avatar, $save_root, $w, $h);
- $avatar_dst = @(new \claviska\SimpleImage($user_avatar));
- //裁剪头像
- $avatar_dst->resize($avatar_w, $avatar_h);
- //将头像添加到背景图
- $qrcode_bg_dst->overlay($avatar_dst, 'top left', 1, $avatar_x, $avatar_y);
- }
- // 添加横线
- $qrcode_bg_dst->rectangle(27,215, 295,215, $option['color']);
- //附加用户昵称
- $font = 23;
- $font_x = 27;
- $font_y = 132;
- if ($font_x >= 0 && $font_x <= 321 && $font_y >= 0 && $font_y <= 349) {
- $qrcode_bg_dst->text('我是' . get_saas_user()->name, [
- 'fontFile' => $font_path,
- 'size' => $font,
- 'color' => $option['color'],
- 'anchor' => 'top left',
- 'xOffset' => $font_x,
- 'yOffset' => $font_y,
- ]);
- $qrcode_bg_dst->text('代言真的有钱赚!', [
- 'fontFile' => $font_path,
- 'size' => 14,
- 'color' => $option['color'],
- 'anchor' => 'top left',
- 'xOffset' => 27,
- 'yOffset' => 170,
- ]);
- $qrcode_bg_dst->text('扫描二维码', [
- 'fontFile' => $font_path,
- 'size' => 14,
- 'color' => $option['color'],
- 'anchor' => 'top left',
- 'xOffset' => 27,
- 'yOffset' => 280,
- ]);
- $qrcode_bg_dst->text('和我一起赚钱', [
- 'fontFile' => $font_path,
- 'size' => 14,
- 'color' => $option['color'],
- 'anchor' => 'top left',
- 'xOffset' => 27,
- 'yOffset' => 300,
- ]);
- }
- //保存图片
- $qrcode_bg_dst->toFile($save_root . $save_name, 'image/jpeg', 100);
- //删除临时图片
- unlink($share_image);
- unlink($user_avatar);
- unlink($wxapp_qrcode_file_path);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $pic_url . '?v=' . time(),
- 'background' => $option['backgroundImage']
- ]
- ];
- }
- /**
- * 专题海报
- * @return [type] [description]
- */
- public function topic_qrcode()
- {
- $goods = Topic::findOne($this->goods_id);
- if (!$goods) {
- return [
- 'code' => 1,
- 'msg' => '专题不存在',
- ];
- }
- $store_name = UserCenterForm::get(OptionSetting::STORE_NAME, get_store_id(), 'store');
- $goods_pic_url = $goods->qrcode_pic ? $goods->qrcode_pic : $goods->cover_pic;
- $goods_pic_save_path = \Yii::$app->basePath . '/web/temp/';
- $version = cyy_version();
- $goods_pic_save_name = md5("v={$version}&goods_id={$goods->id}&goods_name={$goods->title}&store_name={$store_name}&user_id={$this->user_id}&goods_pic_url={$goods_pic_url}&type=0") . '.jpg';
- $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/web/temp/' . $goods_pic_save_name);
- if (file_exists($goods_pic_save_path . $goods_pic_save_name)) {
- return [
- 'code' => 0,
- 'data' => [
- 'goods_name' => $goods->title,
- 'pic_url' => $pic_url . '?v=' . time(),
- ],
- ];
- }
- $goods_pic_path = $this->saveTempImage($goods_pic_url);
- if (!$goods_pic_path) {
- return [
- 'code' => 1,
- 'msg' => '获取海报失败:显示图片丢失',
- ];
- }
- $goods_qrcode_dst = \Yii::$app->basePath . '/web/v1/statics/images/goods-qrcode-dst.jpg';
- $font_path = \Yii::$app->basePath . '/web/v1/statics/font/AaBanSong.ttf';
- $bargain_hb_down = \Yii::$app->basePath . '/web/v1/statics/images/topic-hb-down.png';
- $goods_qrcode = @(new \claviska\SimpleImage($goods_qrcode_dst));
- $goods_pic = @(new \claviska\SimpleImage($goods_pic_path));
- $goods_down = @(new \claviska\SimpleImage($bargain_hb_down));
- //获取小程序码图片
- $scene = "gid:{$goods->id},uid:{$this->user_id},store:{$this->store_id}";
- $wxapp_qrcode_file_res = $this->getQrcode($scene, 240, "other/special/special");
- if (!isset($wxapp_qrcode_file_res['root_path'])) {
- unlink($goods_pic_path);
- return [
- 'code' => 1,
- 'msg' => '获取专题海报失败:获取小程序码失败',
- ];
- }
- $wxapp_qrcode_file_path = $wxapp_qrcode_file_res['root_path'];
- $wxapp_qrcode = @(new \claviska\SimpleImage($wxapp_qrcode_file_path));
- $name_size = 25;
- $name_width = 670;
- //专题标题
- $name = $this->autowrap($name_size, 0, $font_path, $goods->title, $name_width, 2);
- $search = ['“', '”', ':', '’', '‘'];
- $name = str_replace($search, '', $name);
- $goods_qrcode->text($name, [
- 'fontFile' => $font_path,
- 'size' => $name_size,
- 'color' => '#353535',
- 'anchor' => 'top left',
- 'xOffset' => 40,
- 'yOffset' => 48,
- ]);
- //专题图片
- $goods_pic->resize(670, 394);
- $goods_qrcode->overlay($goods_pic, 'top center', 1, 0, 178);
- //专题浏览
- $read = $goods->virtual_read_count + $goods->read_count;
- if ($read > 10000) {
- $read = ($read / 10000) . '万+人浏览';
- } else {
- $read = $read . '人浏览';
- }
- $goods_qrcode->text($read, [
- 'fontFile' => $font_path,
- 'size' => 20,
- 'color' => '#919191',
- 'anchor' => 'top left',
- 'xOffset' => 40,
- 'yOffset' => 604,
- ]);
- //专题内容
- $content = strip_tags($goods->content);
- $content = str_replace(array("\r\n", "\r", "\n", " ", " "), "", $content);
- $content = $this->autowrap($name_size, 0, $font_path, $content, $name_width, 2);
- $content = str_replace($search, '', $content);
- $goods_qrcode->text($content, [
- 'fontFile' => $font_path,
- 'size' => $name_size,
- 'color' => '#353535',
- 'anchor' => 'top left',
- 'xOffset' => 40,
- 'yOffset' => 660,
- ]);
- $goods_qrcode->text('打开小程序阅读全文', [
- 'fontFile' => $font_path,
- 'size' => $name_size,
- 'color' => '#f87c21',
- 'anchor' => 'top left',
- 'xOffset' => 260,
- 'yOffset' => 768,
- ]);
- //调整小程序码图片
- $goods_down->resize(24, 14);
- //附加小程序码图片
- $goods_qrcode->overlay($goods_down, 'top center', 1, 0, 810);
- //加商城名称
- $goods_qrcode->text($store_name, [
- 'fontFile' => $font_path,
- 'size' => 20,
- 'color' => '#888888',
- 'anchor' => 'top left',
- 'xOffset' => 40,
- 'yOffset' => 1170,
- ]);
- //调整小程序码图片
- $wxapp_qrcode->resize(240, 240);
- //附加小程序码图片
- $goods_qrcode->overlay($wxapp_qrcode, 'top left', 1, 470, 1040);
- //保存图片
- $goods_qrcode->toFile($goods_pic_save_path . $goods_pic_save_name, 'image/jpeg', 85);
- //删除临时图片
- unlink($goods_pic_path);
- unlink($wxapp_qrcode_file_path);
- return [
- 'code' => 0,
- 'data' => [
- 'goods_name' => $goods->title,
- 'pic_url' => $pic_url . '?v=' . time(),
- ],
- ];
- }
- //获取网络图片到临时目录
- private function saveTempImage($url)
- {
- if (strpos($url,'http') === false) {
- $url = 'http:'. trim($url);
- }
- if (!is_dir(\Yii::$app->runtimePath . '/image')) {
- mkdir(\Yii::$app->runtimePath . '/image');
- }
- $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
- CurlHelper::download($url, $save_path);
- return $save_path;
- }
- //生成圆角图片
- public function test($url, $path = './', $w, $h, $is_true = 'true')
- {
- $original_path = $url;
- $dest_path = $path . uniqid('r', true) . '.png';
- $src = imagecreatefromstring(file_get_contents($original_path));
- if ($is_true == 'true') {
- $newpic = imagecreatetruecolor($w, $h);
- imagealphablending($newpic, false);
- $transparent = imagecolorallocatealpha($newpic, 0, 0, 0, 127);
- $r = $w / 2;
- for ($x = 0; $x < $w; $x++) {
- for ($y = 0; $y < $h; $y++) {
- $c = imagecolorat($src, $x, $y);
- $_x = $x - $w / 2;
- $_y = $y - $h / 2;
- if ((($_x * $_x) + ($_y * $_y)) < ($r * $r)) {
- imagesetpixel($newpic, $x, $y, $c);
- } else {
- imagesetpixel($newpic, $x, $y, $transparent);
- }
- }
- }
- imagesavealpha($newpic, true);
- imagepng($newpic, $dest_path);
- imagedestroy($newpic);
- imagedestroy($src);
- unlink($url);
- } else {
- imagesavealpha($src, true);
- imagepng($src, $dest_path);
- unlink($url);
- }
- return $dest_path;
- }
- /**
- * @param integer $fontsize 字体大小
- * @param integer $angle 角度
- * @param string $fontface 字体名称
- * @param string $string 字符串
- * @param integer $width 预设宽度
- */
- private function autowrap($fontsize, $angle, $fontface, $string, $width, $max_line = null)
- {
- // 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
- $content = "";
- // 将字符串拆分成一个个单字 保存到数组 letter 中
- $letter = [];
- for ($i = 0; $i < mb_strlen($string, 'UTF-8'); $i++) {
- $letter[] = mb_substr($string, $i, 1, 'UTF-8');
- }
- $line_count = 0;
- foreach ($letter as $l) {
- $teststr = $content . " " . $l;
- $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
- // 判断拼接后的字符串是否超过预设的宽度
- if (($testbox[2] > $width) && ($content !== "")) {
- $line_count++;
- if ($max_line && $line_count >= $max_line) {
- $content = mb_substr($content, 0, -1, 'UTF-8') . "...";
- break;
- }
- $content .= PHP_EOL;
- }
- $content .= $l;
- }
- return $content;
- }
- /**
- * @param integer $fontsize 字体大小
- * @param integer $angle 角度
- * @param string $fontface 字体名称
- * @param string $string 字符串
- * @param integer $width 预设宽度
- */
- public function setName($text)
- {
- if (mb_strlen($text, 'UTF-8') > 8) {
- $text = mb_substr($text, 0, 8, 'UTF-8') . '...';
- }
- return $text;
- }
- private function getQrcode($scene, $width = 240, $page = null)
- {
- return ShareQrcode::wxQrcode($page, $scene, $width);
- }
- /**
- * 作品海报
- */
- public function video_goods_qrcode()
- {
- if (!$this->vl_id) {
- return [
- 'code' => 1,
- 'msg' => '未知的作品'
- ];
- }
- $video_goods = VideoGoodsList::findOne($this->vl_id);
- if (!$video_goods) {
- return [
- 'code' => 1,
- 'msg' => '商品不存在',
- ];
- }
- $goods_pic_url = $video_goods->cover_pic ?: \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/video_goods_cover.jpg';
- $goods_pic_save_path = \Yii::$app->basePath . '/web/temp/';
- $version = cyy_version();
- $goods_pic_save_name = md5("v={$version}&vl_id={$video_goods->id}&user_id={$this->user_id}&goods_pic_url={$goods_pic_url}") . '.jpg';
- $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/temp/' . $goods_pic_save_name);
- if (file_exists($goods_pic_save_path . $goods_pic_save_name)) {
- return [
- 'code' => 0,
- 'data' => [
- 'pic_url' => $pic_url . '?v=' . time(),
- ],
- ];
- }
- $goods_pic_path = $this->saveTempImage($goods_pic_url);
- if (!$goods_pic_path) {
- return [
- 'code' => 1,
- 'msg' => '获取商品海报失败:商品图片丢失',
- ];
- }
- $goods_qrcode_dst = \Yii::$app->basePath . '/web/v1/statics/images/video-goods-qrcode-bg.jpg';
- $font_path = \Yii::$app->basePath . '/web/v1/statics/font/AaBanSong.ttf';
-
- $goods_qrcode = @(new \claviska\SimpleImage($goods_qrcode_dst));
- $goods_pic = @(new \claviska\SimpleImage($goods_pic_path));
- //获取小程序码图片
- $scene = "vid:{$video_goods->id},uid:{$this->user_id},store:{$this->store_id}";
- if ($video_goods->type == 1) {
- $wxapp_qrcode_file_res = $this->getQrcode($scene, 240, "videoGoods/videoDetails/index");
- } else {
- $wxapp_qrcode_file_res = $this->getQrcode($scene, 240, "videoGoods/imgDetails/index");
- }
- if (!isset($wxapp_qrcode_file_res['root_path'])) {
- unlink($goods_pic_path);
- return [
- 'code' => 1,
- 'msg' => '获取商品海报失败:获取小程序码失败',
- ];
- }
- $wxapp_qrcode_file_path = $wxapp_qrcode_file_res['root_path'];
- $wxapp_qrcode = @(new \claviska\SimpleImage($wxapp_qrcode_file_path));
- $name_size = 23;
- $name_width = 630;
- // 标题处理换行
- $name = $this->autowrap($name_size, 0, $font_path, mb_substr($video_goods->title, 0, 36, 'utf-8') . '...', $name_width, 2);
- // 标题
- $goods_qrcode->text($name, [
- 'fontFile' => $font_path,
- 'size' => $name_size,
- 'color' => '#333333',
- 'anchor' => 'top left',
- 'xOffset' => 40,
- 'yOffset' => 800,
- ]);
- $content_name_size = 17;
- $content_name_width = 630;
- // 内容处理换行
- $content = $this->autowrap($content_name_size, 0, $font_path, mb_substr($video_goods->content, 0, 100, 'utf-8') . '...', $content_name_width, 5);
- // 内容
- $goods_qrcode->text($content, [
- 'fontFile' => $font_path,
- 'size' => $content_name_size,
- 'color' => '#bbb',
- 'anchor' => 'top left',
- 'xOffset' => 40,
- 'yOffset' => 890,
- ]);
- //裁剪商品图片
- $goods_pic->resize(750, 750);
- //附加商品图片
- $goods_qrcode->overlay($goods_pic, 'top left', 1, 0, 0);
- //调整小程序码图片
- $wxapp_qrcode->resize(240, 240);
- //附加小程序码图片
- $goods_qrcode->overlay($wxapp_qrcode, 'top left', 1, 500, 765);
- //保存图片
- $goods_qrcode->toFile($goods_pic_save_path . $goods_pic_save_name, 'image/jpeg', 85);
- //删除临时图片
- unlink($goods_pic_path);
- unlink($wxapp_qrcode_file_path);
- return [
- 'code' => 0,
- 'data' => [
- 'pic_url' => $pic_url . '?v=' . time(),
- ],
- ];
- }
- }
|