| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\constants\OptionSetting;
- use app\models\common\InviteCode;
- use app\models\material\Material;
- use app\models\material\MaterialResource;
- use app\models\Md;
- use app\models\Qrcode;
- use app\models\SaasUser;
- use app\models\Topic;
- use app\models\User;
- use app\models\VideoGoodsList;
- use app\modules\admin\models\UserCenterForm;
- use app\modules\client\models\ApiModel;
- use app\utils\CurlHelper;
- use claviska\SimpleImage;
- use yii\base\Model;
- use app\utils\ShareQrcode;
- use app\models\Goods;
- use app\models\Option;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Json;
- use app\models\Mch;
- use app\modules\admin\models\StorageForm;
- use app\models\LiveRoom;
- use app\models\LiveAnchor;
- class ShareQrcodeForm extends ApiModel
- {
- public $store_id;
- public $user;
- public $user_id;
- public $goods_id;
- public $is_pt = false;
- public $is_seckill = false;
- public $vl_id;
- public $num;
- public $type; //0--商城海报 1--秒杀海报 2--拼团海报 3--预约海报 4--分销海报 5--砍价海报 6--专题海报 7--抽奖海报 8--布数宝海报 11--门店海报 12--入驻商海报 15--素材海报
- public $md_id;
- public $mch_id;
- public $id;
- public function rules()
- {
- return [
- [['type'], 'required'],
- [['type'], 'in', 'range' => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, Qrcode::TYPE_GROUP_PURCHASE,Qrcode::TYPE_MATERIAL, Qrcode::TYPE_SUPER_SALES]],
- [['goods_id', 'num', 'vl_id'], 'integer'],
- [['mch_id'], 'safe'],
- [['is_pt', 'is_seckill'], 'safe'],
- [['id'], 'safe'],
- ];
- }
-
- public function beforeValidate() {
- if(parent::beforeValidate()){
- if($this->is_pt === 'false'){
- $this->is_pt = false;
- }
-
- return true;
- }
- return false;
- }
- 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();
- } elseif ($this->type == 11) {
- return $this->md_qrcode();
- } elseif ($this->type == Qrcode::TYPE_LIVE) {
- return $this->live_qrcode();
- } elseif ($this->type == Qrcode::TYPE_MCH) {
- return $this->mch_qrcode();
- } elseif ($this->type == Qrcode::TYPE_GROUP_PURCHASE) {
- return $this->group_purchase_qrcode();
- } elseif ($this->type == Qrcode::TYPE_MATERIAL) {
- return $this->material_qrcode();
- } elseif ($this->type == Qrcode::TYPE_SUPER_SALES) {
- return $this->super_sales_qrcode();
- } else {
- return [
- 'code' => 1,
- 'msg' => 'error'
- ];
- }
- }
- public function goods_qrcode($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/';
- $is_pt = $this->is_pt;
- $is_seckill = $this->is_seckill;
- $version = cyy_version();
- $goods_pic_save_name = md5("v={$version}&goods_id={$goods->id}&goods_name={$goods->name}&store_name={$store_name}&user_id={$this->user_id}&goods_pic_url={$goods_pic_url}&type=0&is_pt={$is_pt}&qrcode={$qrcode}") . '.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' => (new StorageForm())->local2cloudLink($goods_pic_save_path . $goods_pic_save_name) ?? $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->user_id},store:{$this->store_id}";
- if ($is_pt) {
- $scene = "id:{$goods->id},u:{$this->user_id},s:{$this->store_id},p:1";
- }
- if ($is_seckill) {
- $scene = "id:{$goods->id},u:{$this->user_id},s:{$this->store_id},m:1";
- }
- if (is_h5() || (!is_alipay_platform() && !is_wechat_platform())) {
- $is_login = 0;
- if (!is_alipay_platform() && !is_wechat_platform() && !is_h5()) {
- $is_login = 1;
- }
- // 如果是h5环境,就生成普通二维码
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/goods/goods/goods?scene=' . $scene . '&is_login=' . $is_login);
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } else {
- if ($goods->product_type == Goods::GOODS_TYPE_ADOPT) {
- $scene = "id:{$goods->id},u:{$this->user_id},s:{$this->store_id}";
- $wxapp_qrcode_file_res = $this->getQrcode($scene, 240, "livestockAdoption/adopt_detail/adopt_detail");
- } else {
- if ($goods->product_type === 4) {
- $page = 'integral/integralStore/goods';
- } else {
- $page = 'goods/goods/goods';
- }
- $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));
- if($qrcode){
- $qrcode_path = $this->saveTempImage($qrcode);
- if (!$qrcode_path) {
- return [
- 'code' => 1,
- 'msg' => '获取分享商品海报失败:商品图片丢失',
- ];
- }
- $wxapp_qrcode = @(new \claviska\SimpleImage($qrcode_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,
- ]);
- // 加载图片
- // 获取原始图片的宽度和高度
- $original_width = $goods_pic->getWidth();
- $original_height = $goods_pic->getHeight();
- // 设置目标宽度(保持不变)
- $targetWidth = 670; // 例如,宽度保持为 670 像素
- // 计算新的高度以保持宽高比
- $targetHeight = round(($targetWidth / $original_width) * $original_height);
- //裁剪商品图片
- $goods_pic->resize($targetWidth, $targetHeight);
- // 获取调整后的图像宽度和高度
- $resizedWidth = $goods_pic->getWidth();
- $resizedHeight = $goods_pic->getHeight();
- // 设置最终的目标高度
- $finalHeight = 900; // 例如,最终高度为 600 像素
- // 计算剪裁的起始位置
- $cropX = 0; // 宽度不变,不需要剪裁
- $cropY = max(0, ($resizedHeight - $finalHeight) / 2); // 居中剪裁
- // 剪裁图像
- $goods_pic->crop($cropX, $cropY, $targetWidth, $finalHeight);
- //附加商品图片
- $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' => (new StorageForm())->local2cloudLink($goods_pic_save_path . $goods_pic_save_name) ?? $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();
- if (is_alipay_platform()) {
- $save_name = md5("v={$version}&store_id={$this->store_id}&user_id={$this->user->id}&alipay") . '.jpg';
- } else {
- $save_name = md5("v={$version}&store_id={$this->store_id}&user_id={$this->user->id}") . '.jpg';
- if (is_wechat_platform()) {
- $save_name = md5("v={$version}&store_id={$this->store_id}&user_id={$this->user->id}&wechat") . '.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' => (new StorageForm())->local2cloudLink($save_root . $save_name) ?? $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);
- //邀请码位置
- $invite_position = json_decode($store_qrcode->invite_position, true);
- if (!$qrcode_bg) {
- return [
- 'code' => 1,
- 'msg' => '获取背景图片失败'
- ];
- }
- if ((!is_h5() && !is_alipay_platform() && !is_wechat_platform())) {
- $qrcode_size['c'] = 'false';
- }
- //用户头像下载到临时目录
- $user_avatar = $this->saveTempImage(get_saas_user()->avatar);
- if (!$user_avatar) {
- return [
- 'code' => 1,
- 'msg' => '获取用户头像失败'
- ];
- }
- $img_src_user_avatar = @(new \claviska\SimpleImage($user_avatar));
- $img_src_user_avatar->resize(200, 200);
- $img_src_user_avatar->toFile($user_avatar);
- //背景图宽高
- [$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);
- //获取小程序码图片
- if (is_h5()) {
- // 如果是h5环境,就生成普通二维码
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/?store_id=' . get_store_id() . '&user_id=' . get_user_id() . '&page_type=2');
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } elseif (is_app_platform()) {
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/?store_id=' . get_store_id() . '&user_id=' . get_user_id() . '&is_login=1&page_type=2');
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } else {
- $scene = "uid:{$this->user_id},store:{$this->store_id}";//page_type=2,
- $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') {
- $wxapp_qrcode_file_path2 = $wxapp_qrcode_file_path . '2.jpg';
- $c = copy($wxapp_qrcode_file_path, $wxapp_qrcode_file_path2);
- [$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 (isset($qrcode_size['c']) && $qrcode_size['c'] == 'true' && is_h5()) {
- $c = 0.7;
- $width = $width * $c - 15;
- $w2 = (1 - $c) * $width / 2 + 30;
- $wxapp_qrcode_dst2 = @(new \claviska\SimpleImage($wxapp_qrcode_file_path2));
- $wxapp_qrcode_dst2->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_dst2, 'top left', 1, $qrcode_x + $w2, $qrcode_y + $w2);
- }
- @unlink($wxapp_qrcode_file_path2);
- }
- 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) {
- [$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,
- ]);
- }
- }
- //附加邀请码
- if($invite_position){
- $uid = get_user_id();
- if($uid){
- //转化成邀请码
- $inviteCode = InviteCode::id2Code($uid);
- $font = $font_size['size'] * $percent * 0.74;
- $invite_x = $invite_position['x'] * $percent;
- $invite_y = $invite_position['y'] * $percent + 1;
- if ($invite_x >= 0 && $invite_x <= 750 && $invite_y >= 0 && $invite_y <= 1200) {
- $qrcode_bg_dst->text($inviteCode, [
- 'fontFile' => $font_path,
- 'size' => $font,
- 'color' => $font_size['color'],
- 'anchor' => 'top left',
- 'xOffset' => $invite_x,
- 'yOffset' => $invite_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',
- 'page' => $page ?? $scene,
- 'data' => (new StorageForm())->local2cloudLink($save_root . $save_name) ?? $pic_url . '?v=' . time()
- ];
- }
- public function material_qrcode():array
- {
- if(empty($this->id)){
- return [
- 'code' => 1,
- 'msg' => '缺少参数',
- ];
- }
- $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();
- if (is_alipay_platform()) {
- $save_name = md5("v={$version}&store_id={$this->store_id}&user_id={$this->user->id}&m_id=".$this->id."&alipay") . '.jpg';
- } else {
- $save_name = md5("v={$version}&store_id={$this->store_id}&user_id={$this->user->id}&m_id=".$this->id) . '.jpg';
- if (is_wechat_platform()) {
- $save_name = md5("v={$version}&store_id={$this->store_id}&user_id={$this->user->id}&m_id=".$this->id."&wechat") . '.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' => (new StorageForm())->local2cloudLink($save_root . $save_name) ?? $pic_url . '?v=' . time()
- ];
- }
- $store_qrcode = Qrcode::findOne(['store_id' => $this->store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_MATERIAL]);
- 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' => '获取背景图片失败'
- ];
- }
- if ((!is_h5() && !is_alipay_platform() && !is_wechat_platform())) {
- $qrcode_size['c'] = 'false';
- }
- // todo 素材第一张下载到临时目录
- $material = Material::find()->where(['id' => $this->id,'status'=>1])->andWhere(['OR', ['delete_time'=>0], ['IS', 'delete_time', NULL]])->one();
- if(empty($material)){
- return [
- 'code' => 1,
- 'msg' => '素材未设置推广图'
- ];
- }
- $user_avatar = $this->saveTempImage($material->promotion_image);
- if (!$user_avatar) {
- return [
- 'code' => 1,
- 'msg' => '获取推广图失败'
- ];
- }
- $img_src_user_avatar = @(new \claviska\SimpleImage($user_avatar));
- $img_src_user_avatar->resize(200, 200);
- $img_src_user_avatar->toFile($user_avatar);
- //背景图宽高
- [$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);
- //获取小程序码图片
- if (is_h5()) {
- // 如果是h5环境,就生成普通二维码
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/tools/quickPost/item/index?store_id=' . get_store_id() . '&user_id=' . get_user_id() . '&m_id='.$this->id.'&page_type=2');
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } elseif (is_app_platform()) {
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/tools/quickPost/item/index?store_id=' . get_store_id() . '&user_id=' . get_user_id() . '&m_id='.$this->id.'&is_login=1&page_type=2');
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } else {
- $page = 'tools/quickPost/item/index';
- $scene = "uid:{$this->user_id},store:{$this->store_id},m_id:{$this->id}";//page_type=2,
- $wxapp_qrcode_file_res = $this->getQrcode($scene, $qrcode_size['w'], $page);
- }
- $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') {
- $wxapp_qrcode_file_path2 = $wxapp_qrcode_file_path . '2.jpg';
- $c = copy($wxapp_qrcode_file_path, $wxapp_qrcode_file_path2);
- [$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 (isset($qrcode_size['c']) && $qrcode_size['c'] == 'true' && is_h5()) {
- $c = 0.7;
- $width = $width * $c - 15;
- $w2 = (1 - $c) * $width / 2 + 30;
- $wxapp_qrcode_dst2 = @(new \claviska\SimpleImage($wxapp_qrcode_file_path2));
- $wxapp_qrcode_dst2->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_dst2, 'top left', 1, $qrcode_x + $w2, $qrcode_y + $w2);
- }
- @unlink($wxapp_qrcode_file_path2);
- }
- 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) {
- [$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) {
- // 超多多少个文字的推广文案 截取前面的文字并将后面的文字替换为...
- $promotion = $material->promotion;
- if (mb_strlen($promotion) > 15) {
- $promotion = mb_substr($promotion, 0, 15) . '...';
- }
- $qrcode_bg_dst->text($promotion, [
- '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',
- 'page' => $page ?? $scene,
- 'data' => (new StorageForm())->local2cloudLink($save_root . $save_name) ?? $pic_url . '?v=' . time()
- ];
- }
- /**
- * 门店海报
- * @return array
- * @throws \Exception
- * @author: hankaige
- * @Time: 2024/3/20 14:19
- */
- public function md_qrcode() {
- if(empty($this->md_id)){
- return [
- 'code' => 1,
- 'msg' => '门店信息错误'
- ];
- }
- $md = Md::findOne($this->md_id);
- $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();
- if (is_alipay_platform()) {
- $save_name = md5("v={$version}&store_id={$this->store_id}&md_id={$md->id}&user_id{$md->user_id}&alipay") . '.jpg';
- } else {
- $save_name = md5("v={$version}&store_id={$this->store_id}&md_id={$md->id}&user_id{$md->user_id}") . '.jpg';
- if (is_wechat_platform()) {
- $save_name = md5("v={$version}&store_id={$this->store_id}&md_id={$md->id}&user_id{$md->user_id}&wechat") . '.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' => (new StorageForm())->local2cloudLink($save_root . $save_name) ?? $pic_url . '?v=' . time()
- ];
- }
- $store_qrcode = Qrcode::findOne(['store_id' => $this->store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_MD]);
- 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' => '获取背景图片失败'
- ];
- }
- if ((!is_h5() && !is_alipay_platform() && !is_wechat_platform())) {
- $qrcode_size['c'] = 'false';
- }
- //门店的logo下载到临时目录
- $user_avatar = $this->saveTempImage($md->cover_url);
- if (!$user_avatar) {
- return [
- 'code' => 1,
- 'msg' => '获取门店LOGO失败'
- ];
- }
- $img_src_user_avatar = @(new \claviska\SimpleImage($user_avatar));
- $img_src_user_avatar->resize(200, 200);
- $img_src_user_avatar->toFile($user_avatar);
- //背景图宽高
- [$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);
- //获取小程序码图片
- if (is_h5()) {
- // 如果是h5环境,就生成普通二维码
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/?store_id=' . get_store_id() . '&user_id=' . $md->user_id . '&md_id='. $md->id .'&page_type=2');
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } elseif (is_app_platform()) {
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/?store_id=' . get_store_id() . '&user_id=' . $md->user_id. '&md_id='. $md->id . '&is_login=1&page_type=2');
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } else {
- $scene = "uid:{$md->user_id},store:{$this->store_id},mid:{$md->id}";//page_type=2,
- $wxapp_qrcode_file_res = $this->getQrcode($scene,240,'pages/home/home');
- }
- $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') {
- $wxapp_qrcode_file_path2 = $wxapp_qrcode_file_path . '2.jpg';
- $c = copy($wxapp_qrcode_file_path, $wxapp_qrcode_file_path2);
- [$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 (isset($qrcode_size['c']) && $qrcode_size['c'] == 'true' && is_h5()) {
- $c = 0.7;
- $width = $width * $c - 15;
- $w2 = (1 - $c) * $width / 2 + 30;
- $wxapp_qrcode_dst2 = @(new \claviska\SimpleImage($wxapp_qrcode_file_path2));
- $wxapp_qrcode_dst2->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_dst2, 'top left', 1, $qrcode_x + $w2, $qrcode_y + $w2);
- }
- @unlink($wxapp_qrcode_file_path2);
- }
- 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) {
- [$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($md->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',
- 'page' => $page ?? $scene,
- 'data' => (new StorageForm())->local2cloudLink($save_root . $save_name) ?? $pic_url . '?v=' . time()
- ];
- }
- /**
- * 入驻商海报
- */
- public function mch_qrcode() {
- if(empty($this->mch_id)){
- return [
- 'code' => 1,
- 'msg' => '入驻商信息错误'
- ];
- }
- $mch = Mch::findOne($this->mch_id);
- $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();
- if (is_alipay_platform()) {
- $save_name = md5("v={$version}&store_id={$this->store_id}&mch_id={$mch->id}&user_id{$mch->user_id}&alipay") . '.jpg';
- } else {
- $save_name = md5("v={$version}&store_id={$this->store_id}&mch_id={$mch->id}&user_id{$mch->user_id}") . '.jpg';
- if (is_wechat_platform()) {
- $save_name = md5("v={$version}&store_id={$this->store_id}&mch_id={$mch->id}&user_id{$mch->user_id}&wechat") . '.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' => (new StorageForm())->local2cloudLink($save_root . $save_name) ?? $pic_url . '?v=' . time()
- ];
- }
- // $store_qrcode = Qrcode::findOne(['store_id' => $this->store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_MCH]);
- // if (!$store_qrcode) {
- // return [
- // 'code' => 1,
- // 'msg' => '请先在后台设置分销海报'
- // ];
- // }
- $qrcode_bg = \Yii::$app->basePath . '/web/v1/statics/images/mch-home/qr_bg.png';
- //昵称位置
- $font_position = ['x' => '110', 'y' => '40'];
- //小程序码位置
- $qrcode_position = ['x' => '65', 'y' => '180'];
- //头像位置
- $avatar_position = ['x' => '20', 'y' => '20'];
- //头像大小
- $avatar_size = ['w' => '80', 'h' => '80'];
- //小程序码大小
- $qrcode_size = ['w' => '170', 'h' => '170', 'c' => false];
- //昵称大小
- $font_size = ['size' => '21', 'color' => '#333333'];
- //背景图下载到临时目录
- // $qrcode_bg = $this->saveTempImage($store_qrcode->qrcode_bg);
- // if (!$qrcode_bg) {
- // return [
- // 'code' => 1,
- // 'msg' => '获取背景图片失败'
- // ];
- // }
- if ((!is_h5() && !is_alipay_platform() && !is_wechat_platform())) {
- $qrcode_size['c'] = 'false';
- }
- //门店的logo下载到临时目录
- if(empty($mch->logo)){
- return [
- 'code' => 1,
- 'msg' => '请先设置入驻商LOGO'
- ];
- }
- $user_avatar = $this->saveTempImage($mch->logo);
- if (!$user_avatar) {
- return [
- 'code' => 1,
- 'msg' => '获取LOGO失败'
- ];
- }
- $img_src_user_avatar = @(new \claviska\SimpleImage($user_avatar));
- $img_src_user_avatar->resize(200, 200);
- $img_src_user_avatar->toFile($user_avatar);
- //背景图宽高
- [$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);
- $link = 'mch_store/mchCommodity/mchCommodity';
- //获取小程序码图片
- if (is_h5()) {
- // 如果是h5环境,就生成普通二维码
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/' . $link . '?store_id=' . get_store_id() . '&user_id=' . $mch->user_id . '&mch_id='. $mch->id .'&page_type=2');
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } elseif (is_app_platform()) {
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/' . $link . '?store_id=' . get_store_id() . '&user_id=' . $mch->user_id. '&mch_id='. $mch->id . '&is_login=1&page_type=2');
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } else {
- $scene = "u:{$mch->user_id},s:{$this->store_id},mch_id:{$mch->id}";
- $wxapp_qrcode_file_res = $this->getQrcode($scene,240,$link);
- }
- $wxapp_qrcode_file_path = $wxapp_qrcode_file_res['root_path'];
- if (! file_exists($wxapp_qrcode_file_path)) {
- \Yii::error([$wxapp_qrcode_file_res, $link, $scene]);
- return [
- 'code' => 1,
- 'msg' => '获取二维码失败.' . json_encode($wxapp_qrcode_file_res),
- ];
- }
- //获取背景图
- $qrcode_bg_dst = @(new \claviska\SimpleImage($qrcode_bg));
- $qrcode_bg_dst->resize(750, 1200);
- //获取小程序码
- if (isset($qrcode_size['c']) && $qrcode_size['c'] == 'true') {
- $wxapp_qrcode_file_path2 = $wxapp_qrcode_file_path . '2.jpg';
- $c = copy($wxapp_qrcode_file_path, $wxapp_qrcode_file_path2);
- [$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 (isset($qrcode_size['c']) && $qrcode_size['c'] == 'true' && is_h5()) {
- $c = 0.7;
- $width = $width * $c - 15;
- $w2 = (1 - $c) * $width / 2 + 30;
- $wxapp_qrcode_dst2 = @(new \claviska\SimpleImage($wxapp_qrcode_file_path2));
- $wxapp_qrcode_dst2->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_dst2, 'top left', 1, $qrcode_x + $w2, $qrcode_y + $w2);
- }
- @unlink($wxapp_qrcode_file_path2);
- }
- 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) {
- [$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($mch->name, [
- 'fontFile' => $font_path,
- 'size' => $font,
- 'color' => $font_size['color'],
- 'anchor' => 'top left',
- 'xOffset' => $font_x,
- 'yOffset' => $font_y,
- ]);
- $qrcode_bg_dst->text($mch->realname, [
- 'fontFile' => $font_path,
- 'size' => $font * 0.65,
- 'color' => '#999999',
- 'anchor' => 'top left',
- 'xOffset' => $font_x,
- 'yOffset' => $font_y + $font * 1.5,
- ]);
- $qrcode_bg_dst->text('打开手机扫一扫查看店铺首页', [
- 'fontFile' => $font_path,
- 'size' => $font * 0.65,
- 'color' => '#999999',
- 'anchor' => 'top left',
- 'xOffset' => 215,
- 'yOffset' => 1000,
- ]);
- }
- }
- //保存图片
- $qrcode_bg_dst->toFile($save_root . $save_name, 'image/png', 85);
- //删除临时图片
- // unlink($qrcode_bg);
- unlink($user_avatar);
- unlink($wxapp_qrcode_file_path);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'page' => $page ?? $scene,
- 'data' => (new StorageForm())->local2cloudLink($save_root . $save_name) ?? $pic_url . '?v=' . time()
- ];
- }
- /**
- * 直播间海报
- */
- public function live_qrcode() {
- if(empty($this->id)){
- return [
- 'code' => 1,
- 'msg' => '直播间信息错误'
- ];
- }
- $room = LiveRoom::findOne($this->id);
- if(empty($room)){
- return [
- 'code' => 1,
- 'msg' => '直播间信息错误'
- ];
- }
- $anchor = \app\models\LiveAnchor::findOne($room->anchor_id);
- $anchor_user = User::findOne($anchor['user_id']);
- $anchor_saasUser = SaasUser::findOne(['mobile' => $anchor_user['binding']]);
- $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();
- if (is_alipay_platform()) {
- $save_name = md5("v={$version}&store_id={$this->store_id}&room_id={$room->id}&user_id{$this->user_id}&alipay") . '.jpg';
- } else {
- $save_name = md5("v={$version}&store_id={$this->store_id}&room_id={$room->id}&user_id{$this->user_id}") . '.jpg';
- if (is_wechat_platform()) {
- $save_name = md5("v={$version}&store_id={$this->store_id}&room_id={$room->id}&user_id{$this->user_id}&wechat") . '.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' => (new StorageForm())->local2cloudLink($save_root . $save_name) ?? $pic_url . '?v=' . time()
- ];
- }
- // $store_qrcode = Qrcode::findOne(['store_id' => $this->store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_MCH]);
- // if (!$store_qrcode) {
- // return [
- // 'code' => 1,
- // 'msg' => '请先在后台设置分销海报'
- // ];
- // }
- $qrcode_bg = \Yii::$app->basePath . '/web/v1/statics/images/live/qr_bg.png';
- //昵称位置
- $font_position = ['x' => '25', 'y' => '310'];
- //小程序码位置
- $qrcode_position = ['x' => '190', 'y' => '370'];
- //头像位置
- $avatar_position = ['x' => '20', 'y' => '380'];
- //头像大小
- $avatar_size = ['w' => '20', 'h' => '20'];
- //头像位置
- $room_position = ['x' => '0', 'y' => '0'];
- //头像大小
- $room_size = ['w' => '300', 'h' => '292'];
- //小程序码大小
- $qrcode_size = ['w' => '90', 'h' => '90', 'c' => false];
- //昵称大小
- $font_size = ['size' => '21', 'color' => '#333333'];
- //背景图下载到临时目录
- // $qrcode_bg = $this->saveTempImage($store_qrcode->qrcode_bg);
- // if (!$qrcode_bg) {
- // return [
- // 'code' => 1,
- // 'msg' => '获取背景图片失败'
- // ];
- // }
- if ((!is_h5() && !is_alipay_platform() && !is_wechat_platform())) {
- $qrcode_size['c'] = 'false';
- }
- if (!empty(trim($room->pic_url))) {
- $room_pic = $this->saveTempImage($room->pic_url);
- if (!$room_pic) {
- return [
- 'code' => 1,
- 'msg' => '获取直播间图片失败'
- ];
- }
- $img_src_room_pic = @(new \claviska\SimpleImage($room_pic));
- $img_src_room_pic->resize(500, 400);
- $img_src_room_pic->toFile($room_pic);
- }
- //门店的logo下载到临时目录
- $user_avatar = $this->saveTempImage($anchor_saasUser->avatar);
- if (!$user_avatar) {
- return [
- 'code' => 1,
- 'msg' => '获取LOGO失败'
- ];
- }
- $img_src_user_avatar = @(new \claviska\SimpleImage($user_avatar));
- $img_src_user_avatar->resize(200, 200);
- $img_src_user_avatar->toFile($user_avatar);
- //背景图宽高
- [$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);
- $link = 'tools/live/player/index';
- //获取小程序码图片
- if (is_h5()) {
- // 如果是h5环境,就生成普通二维码
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/' . $link . '?store_id=' . get_store_id() . '&user_id=' . $this->user_id . '&id='. $room->id .'&page_type=2');
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } elseif (is_app_platform()) {
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/' . $link . '?store_id=' . get_store_id() . '&user_id=' . $this->user_id. '&id='. $room->id . '&is_login=1&page_type=2');
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } else {
- $scene = "u:{$this->user_id},s:{$this->store_id},id:{$room->id}";
- $wxapp_qrcode_file_res = $this->getQrcode($scene,240,$link);
- }
- $wxapp_qrcode_file_path = $wxapp_qrcode_file_res['root_path'];
- if (! file_exists($wxapp_qrcode_file_path)) {
- \Yii::error([$wxapp_qrcode_file_res, $link, $scene]);
- return [
- 'code' => 1,
- 'msg' => '获取二维码失败.' . json_encode($wxapp_qrcode_file_res),
- ];
- }
- //获取背景图
- $qrcode_bg_dst = @(new \claviska\SimpleImage($qrcode_bg));
- $qrcode_bg_dst->resize(750, 1200);
- //获取小程序码
- if (isset($qrcode_size['c']) && $qrcode_size['c'] == 'true') {
- $wxapp_qrcode_file_path2 = $wxapp_qrcode_file_path . '2.jpg';
- $c = copy($wxapp_qrcode_file_path, $wxapp_qrcode_file_path2);
- [$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 (isset($qrcode_size['c']) && $qrcode_size['c'] == 'true' && is_h5()) {
- $c = 0.7;
- $width = $width * $c - 15;
- $w2 = (1 - $c) * $width / 2 + 30;
- $wxapp_qrcode_dst2 = @(new \claviska\SimpleImage($wxapp_qrcode_file_path2));
- $wxapp_qrcode_dst2->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_dst2, 'top left', 1, $qrcode_x + $w2, $qrcode_y + $w2);
- }
- @unlink($wxapp_qrcode_file_path2);
- }
- 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) {
- [$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 ($room_size['w'] > 0) {
- //获取头像
- $avatar_w = $room_size['w'] * $percent;
- $avatar_h = $room_size['h'] * $percent;
- $avatar_x = $room_position['x'] * $percent;
- $avatar_y = $room_position['y'] * $percent;
- if ($avatar_x >= 0 && $avatar_x <= 750 && $avatar_y >= 0 && $avatar_y <= 1200) {
- if (!empty($room_pic)) {
- [$w, $h] = getimagesize($room_pic);
- // $room_pic = $this->test($room_pic, $save_root, $w, $h);
- $avatar_dst = @(new \claviska\SimpleImage($room_pic));
- //裁剪头像
- $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($room->name, [
- 'fontFile' => $font_path,
- 'size' => $font,
- 'color' => $font_size['color'],
- 'anchor' => 'top left',
- 'xOffset' => $font_x,
- 'yOffset' => $font_y,
- ]);
- $qrcode_bg_dst->text($anchor->name, [
- 'fontFile' => $font_path,
- 'size' => $font * 0.65,
- // 'color' => '#999999',
- 'anchor' => 'top left',
- 'xOffset' => $font_x + 60,
- 'yOffset' => $font_y + 190,
- ]);
- // $qrcode_bg_dst->text('打开手机扫一扫查看店铺首页', [
- // 'fontFile' => $font_path,
- // 'size' => $font * 0.65,
- // 'color' => '#999999',
- // 'anchor' => 'top left',
- // 'xOffset' => 215,
- // 'yOffset' => 1000,
- // ]);
- }
- }
- //保存图片
- $qrcode_bg_dst->toFile($save_root . $save_name, 'image/png', 85);
- //删除临时图片
- // unlink($qrcode_bg);
- unlink($user_avatar);
- unlink($wxapp_qrcode_file_path);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'page' => $page ?? $scene,
- 'data' => (new StorageForm())->local2cloudLink($save_root . $save_name) ?? $pic_url . '?v=' . time()
- ];
- }
- public function group_purchase_qrcode() {
- try {
- $share_group_setting = Option::get('share_group_setting', $this->store_id, 'share_group')['value'];
- $share_group_setting = json_decode($share_group_setting ?? '', true);
- $user = User::findOne($this->user_id);
- $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
- $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();
- if (is_alipay_platform()) {
- $save_name = md5("v={$version}&store_id={$this->store_id}&user_id{$user->id}&alipay&groupPurchase") . '.jpg';
- } else {
- $save_name = md5("v={$version}&store_id={$this->store_id}&user_id{$user->id}&groupPurchase") . '.jpg';
- if (is_wechat_platform()) {
- $save_name = md5("v={$version}&store_id={$this->store_id}&user_id{$user->id}&wechat&groupPurchase") . '.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' => (new StorageForm())->local2cloudLink($save_root . $save_name) ?? $pic_url . '?v=' . time()
- ];
- }
- //昵称位置
- $font_position = ['x' => '110', 'y' => '40'];
- //小程序码位置
- $qrcode_position = ['x' => '65', 'y' => '180'];
- //头像位置
- $avatar_position = ['x' => '20', 'y' => '20'];
- //头像大小
- $avatar_size = ['w' => '80', 'h' => '80'];
- //小程序码大小
- $qrcode_size = ['w' => '170', 'h' => '170', 'c' => false];
- //昵称大小
- $font_size = ['size' => '21', 'color' => '#333333'];
- //背景图下载到临时目录
- $qrcode_bg = $share_group_setting['share_group_bg'] ?: 'https://'.\Yii::$app->request->hostName . '/web/v1/statics/images/step-qrcode-bj.jpg';
- if (!$qrcode_bg) {
- return [
- 'code' => 1,
- 'msg' => '获取背景图片失败'
- ];
- }
- if ((!is_h5() && !is_alipay_platform() && !is_wechat_platform())) {
- $qrcode_size['c'] = 'false';
- }
- //门店的logo下载到临时目录
- $user_avatar = $this->saveTempImage($saas_user->avatar);
- if (!$user_avatar) {
- return [
- 'code' => 1,
- 'msg' => '获取LOGO失败'
- ];
- }
- $img_src_user_avatar = @(new \claviska\SimpleImage($user_avatar));
- $img_src_user_avatar->resize(200, 200);
- $img_src_user_avatar->toFile($user_avatar);
- //背景图宽高
- //门店的logo下载到临时目录
- $qrcode_bg = $this->saveTempImage($qrcode_bg);
- if (!$qrcode_bg) {
- return [
- 'code' => 1,
- 'msg' => '获取LOGO失败1'
- ];
- }
- [$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);
- $link = 'pages/home/home';
- //获取小程序码图片
- if (is_h5()) {
- // 如果是h5环境,就生成普通二维码
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/' . $link . '?store_id=' . get_store_id() . '&user_id=' . $user->id .'&page_type=2');
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } elseif (is_app_platform()) {
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/' . $link . '?store_id=' . get_store_id() . '&user_id=' . $user->id . '&is_login=1&page_type=2');
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } else {
- $scene = "u:{$user->id},s:{$this->store_id}";
- $wxapp_qrcode_file_res = $this->getQrcode($scene,240,$link);
- }
- $wxapp_qrcode_file_path = $wxapp_qrcode_file_res['root_path'];
- if (! file_exists($wxapp_qrcode_file_path)) {
- \Yii::error([$wxapp_qrcode_file_res, $link, $scene]);
- return [
- 'code' => 1,
- 'msg' => '获取二维码失败.' . json_encode($wxapp_qrcode_file_res),
- ];
- }
- //获取背景图
- $qrcode_bg_dst = @(new \claviska\SimpleImage($qrcode_bg));//
- $qrcode_bg_dst->resize(750, 1334);
- //获取小程序码
- if (isset($qrcode_size['c']) && $qrcode_size['c'] == 'true') {
- $wxapp_qrcode_file_path2 = $wxapp_qrcode_file_path . '2.jpg';
- $c = copy($wxapp_qrcode_file_path, $wxapp_qrcode_file_path2);
- [$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 (isset($qrcode_size['c']) && $qrcode_size['c'] == 'true' && is_h5()) {
- $c = 0.7;
- $width = $width * $c - 15;
- $w2 = (1 - $c) * $width / 2 + 30;
- $wxapp_qrcode_dst2 = @(new \claviska\SimpleImage($wxapp_qrcode_file_path2));
- $wxapp_qrcode_dst2->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_dst2, 'top left', 1, $qrcode_x + $w2, $qrcode_y + $w2);
- }
- @unlink($wxapp_qrcode_file_path2);
- }
- 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) {
- [$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($saas_user->name, [
- 'fontFile' => $font_path,
- 'size' => $font,
- 'color' => $font_size['color'],
- 'anchor' => 'top left',
- 'xOffset' => $font_x,
- 'yOffset' => $font_y,
- ]);
- $qrcode_bg_dst->text('打开手机扫一扫来和我一起拼购!', [
- 'fontFile' => $font_path,
- 'size' => $font * 0.65,
- 'color' => '#999999',
- 'anchor' => 'top left',
- 'xOffset' => 215,
- 'yOffset' => 1000,
- ]);
- }
- }
- //保存图片
- $qrcode_bg_dst->toFile($save_root . $save_name, 'image/png', 85);
- //删除临时图片
- // unlink($qrcode_bg);
- unlink($user_avatar);
- unlink($wxapp_qrcode_file_path);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'page' => $page ?? $scene,
- 'data' => $pic_url . '?v=' . time()
- ];
- } catch (\Exception $e) {
- var_dump($e->getMessage() . $e->getLine() . $e->getFile());die;
- }
- }
- public function super_sales_qrcode() {
- try {
- $share_group_setting = Option::get('super_sales_setting', $this->store_id, 'super_sales')['value'];
- $share_group_setting = json_decode($share_group_setting ?? '', true);
- $user = User::findOne($this->user_id);
- $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
- $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();
- if (is_alipay_platform()) {
- $save_name = md5("v={$version}&store_id={$this->store_id}&user_id{$user->id}&alipay&superSales") . '.jpg';
- } else {
- $save_name = md5("v={$version}&store_id={$this->store_id}&user_id{$user->id}&superSales") . '.jpg';
- if (is_wechat_platform()) {
- $save_name = md5("v={$version}&store_id={$this->store_id}&user_id{$user->id}&wechat&superSales") . '.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()
- ];
- }
- //昵称位置
- $font_position = ['x' => '110', 'y' => '40'];
- //小程序码位置
- $qrcode_position = ['x' => '65', 'y' => '180'];
- //头像位置
- $avatar_position = ['x' => '20', 'y' => '20'];
- //头像大小
- $avatar_size = ['w' => '80', 'h' => '80'];
- //小程序码大小
- $qrcode_size = ['w' => '170', 'h' => '170', 'c' => false];
- //昵称大小
- $font_size = ['size' => '21', 'color' => '#333333'];
- //背景图下载到临时目录
- $qrcode_bg = $share_group_setting['super_sales_bg'] ?: 'https://'.\Yii::$app->request->hostName . '/web/v1/statics/images/step-qrcode-bj.jpg';
- if (!$qrcode_bg) {
- return [
- 'code' => 1,
- 'msg' => '获取背景图片失败'
- ];
- }
- if ((!is_h5() && !is_alipay_platform() && !is_wechat_platform())) {
- $qrcode_size['c'] = 'false';
- }
- //门店的logo下载到临时目录
- $user_avatar = $this->saveTempImage($saas_user->avatar);
- if (!$user_avatar) {
- return [
- 'code' => 1,
- 'msg' => '获取LOGO失败'
- ];
- }
- $img_src_user_avatar = @(new \claviska\SimpleImage($user_avatar));
- $img_src_user_avatar->resize(200, 200);
- $img_src_user_avatar->toFile($user_avatar);
- //背景图宽高
- //门店的logo下载到临时目录
- $qrcode_bg = $this->saveTempImage($qrcode_bg);
- if (!$qrcode_bg) {
- return [
- 'code' => 1,
- 'msg' => '获取LOGO失败1'
- ];
- }
- [$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);
- $link = 'pages/home/home';
- //获取小程序码图片
- if (is_h5()) {
- // 如果是h5环境,就生成普通二维码
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/' . $link . '?store_id=' . get_store_id() . '&user_id=' . $user->id .'&page_type=2');
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } elseif (is_app_platform()) {
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/' . $link . '?store_id=' . get_store_id() . '&user_id=' . $user->id . '&is_login=1&page_type=2');
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } else {
- $scene = "u:{$user->id},s:{$this->store_id}";
- $wxapp_qrcode_file_res = $this->getQrcode($scene,240,$link);
- }
- $wxapp_qrcode_file_path = $wxapp_qrcode_file_res['root_path'];
- if (! file_exists($wxapp_qrcode_file_path)) {
- \Yii::error([$wxapp_qrcode_file_res, $link, $scene]);
- return [
- 'code' => 1,
- 'msg' => '获取二维码失败.' . json_encode($wxapp_qrcode_file_res),
- ];
- }
- //获取背景图
- $qrcode_bg_dst = @(new \claviska\SimpleImage($qrcode_bg));//
- $qrcode_bg_dst->resize(750, 1334);
- //获取小程序码
- if (isset($qrcode_size['c']) && $qrcode_size['c'] == 'true') {
- $wxapp_qrcode_file_path2 = $wxapp_qrcode_file_path . '2.jpg';
- $c = copy($wxapp_qrcode_file_path, $wxapp_qrcode_file_path2);
- [$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 (isset($qrcode_size['c']) && $qrcode_size['c'] == 'true' && is_h5()) {
- $c = 0.7;
- $width = $width * $c - 15;
- $w2 = (1 - $c) * $width / 2 + 30;
- $wxapp_qrcode_dst2 = @(new \claviska\SimpleImage($wxapp_qrcode_file_path2));
- $wxapp_qrcode_dst2->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_dst2, 'top left', 1, $qrcode_x + $w2, $qrcode_y + $w2);
- }
- @unlink($wxapp_qrcode_file_path2);
- }
- 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) {
- [$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($saas_user->name, [
- 'fontFile' => $font_path,
- 'size' => $font,
- 'color' => $font_size['color'],
- 'anchor' => 'top left',
- 'xOffset' => $font_x,
- 'yOffset' => $font_y,
- ]);
- $qrcode_bg_dst->text('打开手机扫一扫来和我一起拼购!', [
- 'fontFile' => $font_path,
- 'size' => $font * 0.65,
- 'color' => '#999999',
- 'anchor' => 'top left',
- 'xOffset' => 215,
- 'yOffset' => 1000,
- ]);
- }
- }
- //保存图片
- $qrcode_bg_dst->toFile($save_root . $save_name, 'image/png', 85);
- //删除临时图片
- // unlink($qrcode_bg);
- unlink($user_avatar);
- unlink($wxapp_qrcode_file_path);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'page' => $page ?? $scene,
- 'data' => (new StorageForm())->local2cloudLink($save_root . $save_name) ?? $pic_url . '?v=' . time()
- ];
- } catch (\Exception $e) {
- var_dump($e->getMessage() . $e->getLine() . $e->getFile());die;
- }
- }
- // 分销海报
- 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' => '请先后台设置'
- ];
- }
- if (is_alipay_platform()) {
- $save_name = md5("share=new&store_id={$this->store_id}&user_id={$this->user_id}&alipay") . '.jpg';
- } else {
- $save_name = md5("share=new&store_id={$this->store_id}&user_id={$this->user_id}") . '.jpg';
- if (is_wechat_platform()) {
- $save_name = md5("share=new&store_id={$this->store_id}&user_id={$this->user_id}&wechat") . '.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' => (new StorageForm())->local2cloudLink($save_root . $save_name) ?? $pic_url . '?v=' . time(), 'background' => $option['backgroundImage']]
- ];
- }
- $backgroundImage = $option['backgroundImage'] ?: 'https://'.\Yii::$app->request->hostName . '/web/v1/statics/images/step-qrcode-bj.jpg';
- //下载到临时目录
- $share_holder_bg = $this->saveTempImage($backgroundImage);
- if (!$share_holder_bg) {
- return [
- 'code' => 1,
- 'msg' => '获取股东背景失败'
- ];
- }
- //用户头像下载到临时目录
- $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(300);
-
- if (is_h5() || (!is_alipay_platform() && !is_wechat_platform())) {
- $is_login = 0;
- if (!is_alipay_platform() && !is_wechat_platform() && !is_h5()) {
- $is_login = 1;
- }
- // 如果是h5环境,就生成普通二维码
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/?store_id=' . get_store_id() . '&user_id=' . get_user_id() . '&is_login=' . $is_login) . '&page_type=2';
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } else {
- $scene = "uid:{$this->user_id},store:{$this->store_id}";//page_type=2,
- $wxapp_qrcode_file_res = $this->getQrcode($scene);
- }
- // $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' => '获取分享海报失败:获取小程序码失败',
- ];
- }
- [$w, $h] = getimagesize($wxapp_qrcode_file_path);
- if (!is_h5() && (is_alipay_platform() || is_wechat_platform())) {
- $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';
- //保存图片
- $this->createRoundedImage(321, 349, $test_color, 24, $share_image);
- //获取背景图
- $share_box_bg_dst = @(new \claviska\SimpleImage($share_holder_bg));
- $share_box_bg_dst->resize(750, 1334);
- //获取背景图
- $qrcode_bg_dst = @(new \claviska\SimpleImage($share_image));
- $qrcode_bg_dst->resize(642, 698);
- $wxapp_qrcode_dst = @(new \claviska\SimpleImage($wxapp_qrcode_file_path));
- $wxapp_qrcode_dst->resize($width, $width);
- //将小程序码添加到背景图
- $qrcode_x = 315;
- $qrcode_y = 364;
- if ($qrcode_x >= 0 && $qrcode_x <= 642 && $qrcode_y >= 0 && $qrcode_y <= 698) {
- $qrcode_bg_dst->overlay($wxapp_qrcode_dst, 'left top', 1, $qrcode_x, $qrcode_y);
- }
- //获取头像
- $avatar_w = 132;
- $avatar_h = 132;
- $avatar_x = 27;
- $avatar_y = 38;
- if ($avatar_x >= 0 && $avatar_x <= 642 && $avatar_y >= 0 && $avatar_y <= 698) {
- [$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,330, 617,330, $option['color']);
- //附加用户昵称
- $font = 46;
- $font_x = 27;
- $font_y = 200;
- if ($font_x >= 0 && $font_x <= 642 && $font_y >= 0 && $font_y <= 698) {
- $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($option['title'] ?: '代言真的有钱赚!', [
- 'fontFile' => $font_path,
- 'size' => 28,
- 'color' => $option['color'],
- 'anchor' => 'top left',
- 'xOffset' => 27,
- 'yOffset' => 260,
- ]);
- $qrcode_bg_dst->text('扫描二维码', [
- 'fontFile' => $font_path,
- 'size' => 28,
- 'color' => $option['color'],
- 'anchor' => 'top left',
- 'xOffset' => 27,
- 'yOffset' => 560,
- ]);
- $qrcode_bg_dst->text($option['content'] ?: '和我一起赚钱', [
- 'fontFile' => $font_path,
- 'size' => 28,
- 'color' => $option['color'],
- 'anchor' => 'top left',
- 'xOffset' => 27,
- 'yOffset' => 600,
- ]);
- }
- $share_box_bg_dst->overlay($qrcode_bg_dst, 'center top', 1, 0, 71, true);
- //保存图片
- $share_box_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',
- 'page' => $page ?? $scene,
- 'data' => [
- 'data' => (new StorageForm())->local2cloudLink($save_root . $save_name) ?? $pic_url . '?v=' . time(),
- 'background' => $option['backgroundImage']
- ]
- ];
- }
- public function createRoundedImage($width, $height, $color_hex, $radius, $share_image) {
- // 创建空白图像
- $image = imagecreatetruecolor($width, $height);
- // 将十六进制颜色转换为RGB
- $color = imagecolorallocate($image, $color_hex['red'], $color_hex['green'], $color_hex['blue']);
- // 透明背景(如果需要)
- $transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
- imagefill($image, 0, 0, $transparent);
- imagesavealpha($image, true);
- // 绘制圆角矩形
- imagefilledrectangle($image, $radius, 0, $width - $radius - 1, $height - 1, $color);
- imagefilledrectangle($image, 0, $radius, $width - 1, $height - $radius - 1, $color);
- // 绘制四个角的圆
- imagefilledellipse($image, $radius, $radius, $radius * 2, $radius * 2, $color);
- imagefilledellipse($image, $width - $radius - 1, $radius, $radius * 2, $radius * 2, $color);
- imagefilledellipse($image, $radius, $height - $radius - 1, $radius * 2, $radius * 2, $color);
- imagefilledellipse($image, $width - $radius - 1, $height - $radius - 1, $radius * 2, $radius * 2, $color);
- // 输出图像
- imagepng($image, $share_image);
- }
- /**
- * 专题海报
- * @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' => (new StorageForm())->local2cloudLink($goods_pic_save_path . $goods_pic_save_name) ?? $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));
- //获取小程序码图片
- if (is_h5()) {
- // 如果是h5环境,就生成普通二维码
- $page = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/h5/#/other/special/topic?id=' . $goods->id . 'store_id=' . $this->store_id . '&user_id=' . $this->user_id);
- $wxapp_qrcode_file_res = $this->getQrcode(null, 240, $page);
- } else {
- $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' => (new StorageForm())->local2cloudLink($goods_pic_save_path . $goods_pic_save_name) ?? $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)
- {
- if (is_alipay_platform()) {
- $res = ShareQrcode::getAlipayQrcode($page ?: 'pages/home/home', $scene);
- if (empty($res['code']) || $res['code'] != 10000) {
- return $res;
- }
- $res['url_path'] = $res['qr_code_url_circle_blue'];
- return $res;
- } else {
- 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' => (new StorageForm())->local2cloudLink($goods_pic_save_path . $goods_pic_save_name) ?? $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,
- ]);
- // 加载图片
- // 获取原始图片的宽度和高度
- $original_width = $goods_pic->getWidth();
- $original_height = $goods_pic->getHeight();
- // 设置目标宽度(保持不变)
- $targetWidth = 750; // 例如,宽度保持为 800 像素
- // 计算新的高度以保持宽高比
- $targetHeight = round(($targetWidth / $original_width) * $original_height);
- //裁剪商品图片
- $goods_pic->resize($targetWidth, $targetHeight);
- // 获取调整后的图像宽度和高度
- $resizedWidth = $goods_pic->getWidth();
- $resizedHeight = $goods_pic->getHeight();
- // 设置最终的目标高度
- $finalHeight = 900; // 例如,最终高度为 600 像素
- // 计算剪裁的起始位置
- $cropX = 0; // 宽度不变,不需要剪裁
- $cropY = max(0, ($resizedHeight - $finalHeight) / 2); // 居中剪裁
- // 剪裁图像
- $goods_pic->crop($cropX, $cropY, $targetWidth, $finalHeight);
- //附加商品图片
- $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', 100);
- //删除临时图片
- unlink($goods_pic_path);
- unlink($wxapp_qrcode_file_path);
- return [
- 'code' => 0,
- 'data' => [
- 'pic_url' => (new StorageForm())->local2cloudLink($goods_pic_save_path . $goods_pic_save_name) ?? $pic_url . '?v=' . time(),
- ],
- ];
- }
- }
|