| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819 |
- <?php
- /**
- * Created
- * Author: 芸众商城 www.yunzshop.com
- * Date: 2018/1/24
- * Time: 下午1:43
- */
- namespace app\frontend\modules\goods\controllers;
- use app\common\components\ApiController;
- use app\common\helpers\ImageHelper;
- use app\common\models\Member;
- use app\common\models\MemberLevel;
- use Setting;
- use app\common\services\Utils;
- use Yunshop\NewPoster\models\Poster;
- use Yunshop\NewPoster\services\CreateGoods;
- /**
- * 商品海报
- */
- class GoodsPosterController extends ApiController
- {
- private $shopSet;
- private $goodsModel;
- private $storeid;
- private $hotel_id;
- private $mid;
- private $type;
- private $ingress;
- private $shop_id;
- private $member;
- private $shopText = [
- 'left' => 75,
- 'top' => 50,
- 'type' => 1,
- 'size' => 24,
- 'max_width' => 500,
- 'br' => true,
- ];
- private $goodsText = [
- 'left' => 45,
- 'top' => 710,
- 'type' => 1,
- 'size' => 19,
- 'max_width' => 500,
- 'br' => false,
- ];
- /**
- * 字体路径
- *
- * @var string
- */
- private $fontPath;
- /**
- * 获取商品海报(前端生成)
- * @return \Illuminate\Http\JsonResponse
- */
- public function newGoodsPoster()
- {
- $id = intval(\YunShop::request()->id);
- $this->mid = \YunShop::app()->getMemberId();
- $this->storeid = intval(\YunShop::request()->storeid);
- $this->hotel_id = intval(\YunShop::request()->hotel_id);
- $this->shop_id = intval(\YunShop::request()->shop_id);
- $this->type = intval(\YunShop::request()->type);
- $this->member = $member = Member::uniacid()->select('uid', 'avatar', 'nickname')->ofUid(\YunShop::app()->getMemberId())->with('yzMember')->first();
- $goods_model = \app\common\modules\shop\ShopConfig::current()->get('goods.models.commodity_classification');
- $goods_model = new $goods_model;
- $this->goodsModel = $goods_model->uniacid()
- ->with(['hasOneShare', 'hasManyGoodsDiscount','hasManyOptions'])
- ->where('status', 1)->find($id);
- if (empty($this->goodsModel)) {
- return $this->errorJson('该商品不存在');
- }
- if (empty($this->storeid)) {
- $this->shopSet = \Setting::get('shop.shop');
- } else {
- if (app('plugins')->isEnabled('store-cashier') && !empty($this->storeid)) {
- $store = \app\common\models\Store::find($this->storeid);
- $this->shopSet['name'] = $store->store_name;
- $this->shopSet['logo'] = $store->thumb;
- }
- if (app('plugins')->isEnabled('hotel') && !empty($this->hotel_id)) {
- $hotel = \Yunshop\Hotel\common\models\Hotel::find($this->hotel_id);
- $this->shopSet['name'] = $hotel->hotel_name;
- $this->shopSet['logo'] = $hotel->thumb;
- }
- }
- if (\YunShop::plugin()->get('new-poster')) {
- $result = $this->newPoster();
- if ($result) {
- return $this->successJson('ok', $result);
- }
- }
- $thumb = yz_tomedia($this->goodsModel->thumb);
- if ($this->goodsModel->hasOneShare->share_thumb) {
- $thumb = yz_tomedia($this->goodsModel->hasOneShare->share_thumb);
- }
- $shareTitle = $this->goodsModel->title;
- if ($this->goodsModel->hasOneShare->share_title) {
- $shareTitle = $this->goodsModel->hasOneShare->share_title;
- }
- $goodsPrice = $this->goodsModel->price;
- if ($this->goodsModel->has_option) {
- //有规格
- $min = $this->goodsModel->hasManyOptions->min('product_price');
- $max = $this->goodsModel->hasManyOptions->max('product_price');
- if ($min == $max) {
- $goodsPrice = $min;
- } else {
- $goodsPrice = $min.'-'.$max;
- }
- }
- // 现在大于原价则为0
- $market_price = (int)$goodsPrice > (int)$this->goodsModel->market_price ? 0 : $this->goodsModel->market_price;
- return $this->successJson('请求接口成功',[
- 'logo' => yz_tomedia($this->shopSet['logo']),
- 'shopName' => $this->shopSet['name'],
- 'thumb' => $thumb,
- 'price' => $goodsPrice,
- 'market_price' => $market_price,
- 'shareTitle' => $shareTitle,
- 'backgroundImg'=> config('app.framework') == 'platform' ? request()->getSchemeAndHttpHost().'/static/post/images/post.png' : request()->getSchemeAndHttpHost().'/addons/yun_shop/static/post/images/post.png',
- 'qrcode' => $this->getCodeLink(200),
- 'new' => false,
- ]);
- }
- protected function newPoster()
- {
- $poster = Poster::uniacid()->where(['status' => 1, 'poster_type' => 15])->first();
- if (!$poster) {
- return false;
- }
- $thumb = yz_tomedia($this->goodsModel->thumb);
- $shareTitle = $this->goodsModel->title;
- if ($this->goodsModel->hasOneShare->share_title && $this->goodsModel->hasOneShare->share_thumb) {
- $thumb = yz_tomedia($this->goodsModel->hasOneShare->share_thumb);
- $shareTitle = $this->goodsModel->hasOneShare->share_title;
- }
- $default_logo = yz_tomedia($this->shopSet['logo']);
- $poster_style = json_decode($poster['style_data'], true);
- foreach ($poster_style as &$item) {
- $item = $this->getRealParams($item);
- switch ($item['type']) {
- case 'price' :
- $goodsPrice = $this->goodsModel->price;
- if ($this->goodsModel->has_option) {
- //有规格
- $min = $this->goodsModel->hasManyOptions->min('product_price');
- $max = $this->goodsModel->hasManyOptions->max('product_price');
- if ($min == $max) {
- $goodsPrice = $min;
- } else {
- $goodsPrice = $min.'-'.$max;
- }
- }
- $item['src'] = $goodsPrice;
- break;
- case 'market_price' :
- if ($this->goodsModel->market_price == 0 || $this->goodsModel->price == $this->goodsModel->market_price) {
- $market_price = 0;
- } else {
- $market_price = $this->goodsModel->market_price;
- }
- $item['src'] = $market_price;
- break;
- case 'member_price' :
- $item['src'] = $this->changeMemberGoodsPrice();
- break;
- case 'head' :
- if (strexists(ImageHelper::fix_wechatAvatar($this->member->avatar), 'https')) {
- $head = ImageHelper::fix_wechatAvatar($this->member->avatar);
- } else {
- $head = str_replace('http', 'https', ImageHelper::fix_wechatAvatar($this->member->avatar));
- }
- $item['src'] = $head;
- break;
- case 'nickname' :
- $item['src'] = $this->member->nickname;
- break;
- case 'title' :
- $item['src'] = $shareTitle;
- break;
- case 'qr' :
- $item['src'] = $this->getCodeLink(300) ?: yz_tomedia($this->goodsModel->thumb);
- break;
- case 'logo' :
- if ($item['logo_type'] == 1) {
- $logo = yz_tomedia($item['src']);
- } else {
- $logo = yz_tomedia($default_logo);
- }
- $item['src'] = $logo;
- break;
- case 'img' :
- $item['src'] = $thumb;
- break;
- case 'other_img' :
- $item['src'] = yz_tomedia($item['src']);
- break;
- case 'invite' :
- $item['src'] = $this->member->yzMember->invite_code;
- break;
- case 'mid' :
- $item['src'] = $this->member->uid;
- break;
- }
- }
- $background = [
- 'type' => $poster['color'] ? 'color' : 'background',
- 'src' => $poster['color'] ?: yz_tomedia($poster['background']),
- ];
- if ($background['type'] == 'background' && !$background['src']) {
- $background['src'] = config('app.framework') == 'platform' ? request()->getSchemeAndHttpHost().'/static/post/images/post.png' : request()->getSchemeAndHttpHost().'/addons/yun_shop/static/post/images/post.png';
- }
- return [
- 'poster_data' => $poster_style,
- 'background' => $background,
- 'new' => true,
- ];
- }
- public function generateGoodsPoster()
- {
- $id = intval(\YunShop::request()->id);
- if (\YunShop::plugin()->get('new-poster')) {
- $poster = Poster::uniacid()->where(['poster_type' => 15, 'status' => 1])->first();
- if ($poster) {
- $member = Member::uniacid()->with('yzMember')->where('uid', \YunShop::app()->getMemberId())->first();
- $createService = new CreateGoods($poster, $member, $this->type, request()->getSchemeAndHttpHost(), $id);
- $poster_result = $createService->getUrl();
- if ($poster_result['error_code'] != 0) {
- return $this->errorJson('生成失败', $poster_result);
- }
- $poster_result['new'] = true;
- return $this->successJson('ok', $poster_result);
- }
- }
- $this->fontPath = $this->defaultFontPath();
- $this->mid = \YunShop::app()->getMemberId();
- $this->storeid = intval(\YunShop::request()->storeid);
- $this->hotel_id = intval(\YunShop::request()->hotel_id);
- $this->shop_id = intval(\YunShop::request()->shop_id);
- $this->type = intval(\YunShop::request()->type);
- $this->ingress = \YunShop::request()->ingress ?: '';
- if (!$id) {
- return $this->errorJson('请传入正确参数.');
- }
- if (empty($this->storeid)) {
- $this->shopSet = \Setting::get('shop.shop');
- } else {
- if (app('plugins')->isEnabled('store-cashier') && !empty($this->storeid)) {
- $store = \app\common\models\Store::find($this->storeid);
- $this->shopSet['name'] = $store->store_name;
- $this->shopSet['logo'] = $store->thumb;
- }
- if (app('plugins')->isEnabled('hotel') && !empty($this->hotel_id)) {
- $hotel = \Yunshop\Hotel\common\models\Hotel::find($this->hotel_id);
- $this->shopSet['name'] = $hotel->hotel_name;
- $this->shopSet['logo'] = $hotel->thumb;
- }
- }
- if ($this->type == 2 && $this->ingress == 'weChatApplet') {
- if (!app('plugins')->isEnabled('min-app')) {
- return $this->errorJson('未开启小程序插件');
- }
- }
- $goods_model = \app\common\modules\shop\ShopConfig::current()->get('goods.models.commodity_classification');
- $goods_model = new $goods_model;
- $this->goodsModel = $goods_model->uniacid()->with('hasOneShare')->where('status', 1)->find($id);
- if (empty($this->goodsModel)) {
- return $this->errorJson('该商品不存在');
- }
- $imgPath = $this->get_lt();
- if (config('app.framework') == 'platform') {
- $urlPath = request()->getSchemeAndHttpHost() . '/' . substr($imgPath, strpos($imgPath, 'storage'));
- } else {
- $urlPath = request()->getSchemeAndHttpHost() . '/' . substr($imgPath, strpos($imgPath, 'addons'));
- }
- $data = $this->base64EncodeImage($imgPath);
- $data['image_url'] = $urlPath;
- $data['new'] = false;
- return $this->successJson('ok', $data);
- }
- /**
- * 圆角图片
- * @param $radius 角度
- * @return img
- */
- public function get_lt_rounder_corner($radius)
- {
- $img = imagecreatetruecolor($radius, $radius); // 创建一个正方形的图像
- $bgcolor = imagecolorallocate($img, 1, 1, 1); // 图像的背景
- $fgcolor = imagecolorallocate($img, 0, 0, 0);
- imagefill($img, 0, 0, $bgcolor);
- // $radius,$radius:以图像的右下角开始画弧
- // $radius*2, $radius*2:已宽度、高度画弧
- // 180, 270:指定了角度的起始和结束点
- // fgcolor:指定颜色
- imagefilledarc($img, $radius, $radius, $radius * 2, $radius * 2, 180, 270, $fgcolor, IMG_ARC_PIE);
- // 将弧角图片的颜色设置为透明
- imagecolortransparent($img, $fgcolor);
- return $img;
- }
- public function roundRadius($resource, $image_width, $image_height, $radius = 8)
- {
- // lt(左上角)
- $lt_corner = $this->get_lt_rounder_corner($radius);
- // header('Content-Type: image/png');
- // imagepng($lt_corner);
- // exit;
- imagecopymerge($resource, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100);
- // lb(左下角)
- $lb_corner = imagerotate($lt_corner, 90, 0);
- imagecopymerge($resource, $lb_corner, 0, $image_height - $radius, 0, 0, $radius, $radius, 100);
- // rb(右上角)
- $rb_corner = imagerotate($lt_corner, 180, 0);
- imagecopymerge($resource, $rb_corner, $image_width - $radius, $image_height - $radius, 0, 0, $radius, $radius, 100);
- // rt(右下角)
- $rt_corner = imagerotate($lt_corner, 270, 0);
- imagecopymerge($resource, $rt_corner, $image_width - $radius, 0, 0, 0, $radius, $radius, 100);
- // header('Content-Type: image/png');
- // imagepng($resource);
- // exit;
- return $resource;
- }
- //图片返回base64 格式
- public function base64EncodeImage($strTmpName)
- {
- $base64Image = '';
- $imageInfo = getimagesize($strTmpName);
- //$imageData = fread(fopen($strTmpName , 'r'), filesize($strTmpName));
- //$base64Image = 'data:' . $imageInfo['mime'] . ';base64,' . chunk_split(base64_encode($imageData));
- $base64Image = base64_encode(file_get_contents($strTmpName));
- return [
- 'mime' => $imageInfo['mime'],
- 'base64Image' => $base64Image,
- ];
- }
- /**
- * 生成商品海报
- * @return string 商品海报绝对路径
- */
- public function get_lt()
- {
- set_time_limit(0);
- @ini_set('memory_limit', '256M');
- $image_width = 600; //335
- $image_height = 1000; //485
- $target = imagecreatefrompng(base_path('/static/post/images/post.png'));
- $target = $this->roundRadius($target, $image_width, $image_height);
- $target = $this->createShopImage($target);
- if ($this->goodsModel->hasOneShare->share_thumb) {
- $goodsThumb = $this->goodsModel->hasOneShare->share_thumb;
- } else {
- $goodsThumb = $this->goodsModel->thumb;
- }
- $target = $this->mergeGoodsImage($target, $goodsThumb);
- //商品二维码
- $goodsQr = $this->generateQr();
- if ($this->goodsModel->hasOneShare->share_title) {
- $text = $this->goodsModel->hasOneShare->share_title;
- } else {
- $text = $this->goodsModel->title;
- }
- $target = $this->mergeQrImage($target, $goodsQr);
- $target = $this->mergeText($target, $this->goodsText, $text);
- $target = $this->mergePriceText($target);
- imagepng($target, $this->getGoodsPosterPath());
- imagedestroy($target);
- return $this->getGoodsPosterPath();
- }
- //商城logo 与 商城名称处理
- protected function createShopImage($target)
- {
- //计算商城名称的宽度
- $testbox = imagettfbbox($this->shopText['size'], 0, $this->fontPath, $this->shopSet['name']);
- $shopTextWidth = $testbox[2] > 500 ? 500 : $testbox[2];
- $image_width = $shopTextWidth + 80;
- $image_height = 80;
- $img = imagecreatetruecolor($image_width, $image_height);
- $white = imagecolorallocate($img, 255, 255, 255);
- //设置白色背景色
- imagefill($img, 0, 0, $white);
- $img = $this->mergeLogoImage($img);
- if (empty($this->shopSet['logo'])) {
- $this->shopText['left'] = 45;
- }
- $img = $this->mergeText($img, $this->shopText, $this->shopSet['name']);
- imagecopyresized($target, $img, (600 - $image_width) / 2, 20, 0, 0, $image_width, $image_height, imagesx($img), imagesy($img));
- imagedestroy($img);
- return $target;
- }
- private function getGoodsPosterPath()
- {
- $path = storage_path('app/public/goods/' . \YunShop::app()->uniacid) . "/";
- Utils::mkdirs($path);
- $file_name = \YunShop::app()->uniacid . '-' . \YunShop::app()->getMemberId() . '-' . $this->goodsModel->id . '.png';
- return $path . $file_name;
- }
- /**
- * 合并商品图片到 $target
- * @param $target
- * @param $img
- * @return mixed
- */
- private function mergeGoodsImage($target, $thumb)
- {
- $thumb = $this->HttpAgreement(yz_tomedia($thumb));
- $img = imagecreatefromstring(\Curl::to($thumb)->get());
- $width = imagesx($img);
- $height = imagesy($img);
- imagecopyresized($target, $img, 45, 120, 0, 0, 480, 500, $width, $height);
- imagedestroy($img);
- return $target;
- }
- /**
- * 合并商城Logo 到 $target
- * @param [type] $target [description]
- * @param [type] $img [description]
- */
- private function mergeLogoImage($target)
- {
- $logo = $this->HttpAgreement(yz_tomedia($this->shopSet['logo']));
- $img = imagecreatefromstring(\Curl::to($logo)->get());
- if (!$img) {
- $this->shopText['left'] = 45;
- return $target;
- }
- $width = imagesx($img);//原图的宽度
- $height = imagesy($img);//原图的高度
- $fix = 65;
- //$percent = ($fix/$width);
- //缩放尺寸
- //$newwidth = $width * $percent;
- //$newheight = $height * $percent;
- // imagecopyresized($target, $img, 0, 5, 0, 0, 50, 50, $width, $height);
- // imagedestroy($img);
- $compress = imagecreatetruecolor($fix, $fix);//压缩图片
- $white = imagecolorallocate($compress, 255, 255, 255);
- imagefill($compress, 0, 0, $white);
- imagecopyresized($compress, $img, 0, 0, 0, 0, $fix, $fix, $width, $height);
- //根据压缩图生成圆形图
- $circular = imagecreatetruecolor($fix,$fix);
- $cc_white = imagecolorallocate($circular, 255, 255, 255);
- imagefill($circular, 0, 0, $cc_white);
- imagealphablending($circular,false);
- $transparent = imagecolorallocatealpha($circular, 255, 255, 255, 127);
- $r=$fix/2;
- for($x=0;$x<$fix;$x++) {
- for ($y = 0; $y < $fix; $y++) {
- $c = imagecolorat($compress, $x, $y);
- $_x = $x - $fix / 2;
- $_y = $y - $fix / 2;
- if ((($_x * $_x) + ($_y * $_y)) < ($r * $r)) {
- imagesetpixel($circular, $x, $y, $c);
- } else {
- imagesetpixel($circular, $x, $y, $transparent);
- }
- }
- }
- imagesavealpha($circular, true);
- imagecopy($target,$circular,0,5,0,0,$fix,$fix);
- imagedestroy($circular);
- imagedestroy($compress);
- imagedestroy($img);
- return $target;
- }
- /**
- * 合并商品二维码 到 $target
- * @param [type] $target [description]
- * @param [type] $img [description]
- */
- private function mergeQrImage($target, $img, $dst_x = 340, $dst_y = 780)
- {
- $width = imagesx($img);
- $height = imagesy($img);
- if ($this->type == 2 && $this->ingress == 'weChatApplet') {
- imagecopyresized($target, $img, 340, $dst_y, 0, 0, 200, 200, $width, $height);
- } else {
- imagecopy($target, $img, $dst_x, $dst_y, 0, 0, $width, $height);
- }
- // imagecopy($target, $img, $dst_x, $dst_y, 0, 0, $width, $height);
- imagedestroy($img);
- return $target;
- }
- /**
- * 合并名称
- * @param $target
- * @param $params
- * @param $text
- * @return mixed
- */
- private function mergeText($target, $params, $text)
- {
- if ($params['type']) {
- $text = $this->autowrap($params['size'], 0, $this->fontPath, $text, $params['max_width'], $params['br']);
- }
- $black = imagecolorallocate($target, 51, 51, 51);//文字颜色
- imagettftext($target, $params['size'], 0, $params['left'], $params['top'], $black, $this->fontPath, $text);
- return $target;
- }
- /**
- * 合并商品价格
- * @return [type] [description]
- */
- private function mergePriceText($target)
- {
- $color = imagecolorallocate($target, 107, 107, 107);
- $this->goodsModel->vip_level_status;
- $price_display = '';
- if ($this->goodsModel->vip_level_status['status']){
- $price_display = $this->goodsModel->vip_level_status['word'];
- }else{
- $price_display = $this->goodsModel->price;
- }
- //$price = '现价:¥' .$price_display;// $this->goodsModel->price;
- $price = '¥' .$price_display;// $this->goodsModel->price;
- $market_price = '原价:¥' . $this->goodsModel->market_price;
- $black = imagecolorallocate($target, 241, 83, 83);//当前价格颜色
- $price_box = imagettfbbox(18, 0, $this->fontPath, $price);
- $market_price_box = imagettfbbox(24, 0, $this->fontPath, $market_price);
- $gray = imagecolorallocate($target, 107, 107, 107);//原价颜色
- //设置删除线条
- // imageline($target, $price_box[2] + 12, 900, $price_box[2]+$market_price_box[2] + 14, 900, $color);
- $next = $price_box[2] + 120;
- imagettftext($target, 24, 0, 45, 670, $black, $this->fontPath, $price);
- if ($this->goodsModel->market_price>$price_display) {
- imagettftext($target, 16, 0, $next, 670, $gray, $this->fontPath, $market_price);
- imageline($target, $price_box[2] + 180, 663, $price_box[2] + $market_price_box[2] + 60, 663, $color);
- }
- return $target;
- }
- /**
- * 生成商品二维码
- * @return [type] [description]
- */
- private function generateQr()
- {
- if ($this->type == 2 && $this->ingress == 'weChatApplet') {
- //小程序海报生成
- $url = "packageA/detail_v2/detail_v2";
- $img = $this->getWxacode($url);
- return $img;
- }
- if (empty($this->storeid)) {
- //商城商品二维码
- //微店商品二维码
- if ($this->shop_id) {
- $url = yzAppFullUrl('/goods/' . $this->goodsModel->id, ['mid' => $this->mid,'shop_id'=>$this->shop_id]);
- $file = 'shop-mid-' . $this->mid . '-shop_id-'.$this->shop_id. '-goods-' . $this->goodsModel->id . '.png';
- } else {
- $url = yzAppFullUrl('/goods/' . $this->goodsModel->id, ['mid' => $this->mid]);
- $file = 'shop-mid-' . $this->mid . '-goods-' . $this->goodsModel->id . '.png';
- }
- } else {
- //门店商品二维码
- $url = yzAppFullUrl('/goods/' . $this->goodsModel->id . '/o2o/' . $this->storeid, ['mid' => $this->mid]);
- $file = 'store-' . $this->storeid . '-mid-' . $this->mid . '-goods-' . $this->goodsModel->id . '.png';
- }
- $path = storage_path('app/public/goods/qrcode/' . \YunShop::app()->uniacid);
- Utils::mkdirs($path);
- if (!is_file($path . '/' . $file)) {
- \QrCode::format('png')->size(220)->generate($url, $path . '/' . $file);
- }
- $img = imagecreatefromstring(file_get_contents($path . '/' . $file));
- return $img;
- }
- /**
- * 字体换行
- * @param [int] $fontsize [字体大小]
- * @param [int] $angle [角度]
- * @param [string] $fontface [字体类型]
- * @param [string] $string [字符串]
- * @param [int] $width [预设宽度]
- * @param [int] $br [大于$width是否换行]
- * @return [string] [处理好的字符串]
- */
- private function autowrap($fontsize, $angle, $fontface, $string, $width, $br)
- {
- $content = "";
- $num = 0;
- // 将字符串拆分成一个个单字 保存到数组 letter 中
- for ($i = 0; $i < mb_strlen($string); $i++) {
- $letter[] = mb_substr($string, $i, 1);
- }
- foreach ($letter as $l) {
- $teststr = $content . " " . $l;
- $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
- // 判断拼接后的字符串是否超过预设的宽度
- if (($testbox[2] > $width) && ($content !== "")) {
- $num += 1;
- if ($num > 1 || $br) {
- $content .= '..';
- // dd($content);
- return $content;
- }
- $content .= "\n";
- }
- $content .= $l;
- }
- return $content;
- }
- /**
- * 补全http协议
- * @param [string] $src 图片地址
- * @return [string]
- */
- protected function HttpAgreement($src)
- {
- $t = strtolower($src);
- if (strexists($t, 'http://') || strexists($t, 'https://')) {
- return $src;
- }
- $src = 'https://' . ltrim($src, '//');
- return $src;
- }
- //生成小程序二维码
- function getWxacode($goods_url)
- {
- $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?";
- $token = $this->getToken();
- \Log::debug('===========access_token===========', $token);
- $url .= "access_token=" . $token;
- $postdata = [
- "scene" => 'id=' . $this->goodsModel->id . ',mid=' . \YunShop::app()->getMemberId(),
- "page" => $goods_url,
- ];
- if ($this->shop_id) {
- $postdata['scene'] .= ',shop_id='.$this->shop_id;
- }
- $path = storage_path('app/public/goods/qrcode/' . \YunShop::app()->uniacid);
- if (!is_dir($path)) {
- Utils::mkdirs($path);
- }
- \Log::debug('=====地址信息=======', $postdata);
- $res = $this->curl_post($url, json_encode($postdata), $options = array());
- $erroe = json_decode($res);
- if (isset($erroe->errcode)) {
- return $this->errorJson('错误码' . $erroe->errcode . ';错误信息' . $erroe->errmsg);
- }
- \Log::debug('===========生成二维码===========', $res);
- $file = 'mid-' . $this->mid . '-goods-' . $this->goodsModel->id . '.png';
- file_put_contents($path . '/' . $file, $res);
- $img = imagecreatefromstring(file_get_contents($path . '/' . $file));
- return $img;
- }
- public function getCodeLink($size)
- {
- if ($this->type == 2) {
- $img = $this->getWXcodeLink();
- return $img;
- }
- if (empty($this->storeid)) {
- //商城商品二维码
- //微店商品二维码
- if ($this->shop_id) {
- $url = yzAppFullUrl('/goods/' . $this->goodsModel->id, ['mid' => $this->mid,'shop_id'=>$this->shop_id]);
- $file = 'shop-mid-' . $this->mid . '-shop_id-'.$this->shop_id. '-goods-' . $this->goodsModel->id . '.png';
- } else {
- $url = yzAppFullUrl('/goods/' . $this->goodsModel->id, ['mid' => $this->mid]);
- $file = 'shop-mid-' . $this->mid . '-goods-' . $this->goodsModel->id . '.png';
- }
- } else {
- //门店商品二维码
- $url = yzAppFullUrl('/goods/' . $this->goodsModel->id . '/o2o/' . $this->storeid, ['mid' => $this->mid]);
- $file = 'store-' . $this->storeid . '-mid-' . $this->mid . '-goods-' . $this->goodsModel->id . '.png';
- }
- $path = storage_path('app/public/goods/qrcode/' . \YunShop::app()->uniacid);
- Utils::mkdirs($path);
- if (!is_file($path . '/' . $file)) {
- \QrCode::format('png')->size($size)->generate($url, $path . '/' . $file);
- }
- $img = config('app.framework') == 'platform' ? request()->getSchemeAndHttpHost().'/storage/app/public/goods/qrcode/' . \YunShop::app()->uniacid . '/' . $file : request()->getSchemeAndHttpHost().'/addons/yun_shop/storage/app/public/goods/qrcode/' . \YunShop::app()->uniacid . '/' . $file ;
- return $img;
- }
- //生成小程序二维码
- public function getWXcodeLink()
- {
- $goods_url = "packageA/detail_v2/detail_v2";
- $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?";
- $token = $this->getToken();
- \Log::debug('===========access_token===========', $token);
- $url .= "access_token=" . $token;
- $postdata = [
- "scene" => 'id=' . $this->goodsModel->id . ',mid=' . \YunShop::app()->getMemberId(),
- "page" => $goods_url,
- ];
- if ($this->shop_id) {
- $postdata['scene'] .= ',shop_id='.$this->shop_id;
- }
- $path = storage_path('app/public/goods/qrcode/' . \YunShop::app()->uniacid);
- if (!is_dir($path)) {
- Utils::mkdirs($path);
- }
- $res = $this->curl_post($url, json_encode($postdata), $options = array());
- $erroe = json_decode($res, true);
- if (isset($erroe->errcode)) {
- \Log::debug('---------生成小程序码错误--------', '错误码' . $erroe->errcode . ';错误信息' . $erroe->errmsg);
- return '';
- }
- $file = 'mid-' . $this->mid . '-goods-' . $this->goodsModel->id . '.png';
- file_put_contents($path . '/' . $file, $res);
- return config('app.framework') == 'platform' ? request()->getSchemeAndHttpHost().'/storage/app/public/goods/qrcode/'.\YunShop::app()->uniacid.'/'.$file : request()->getSchemeAndHttpHost().'/addons/yun_shop/storage/app/public/goods/qrcode/'.\YunShop::app()->uniacid.'/'.$file;
- }
- //发送获取token请求,获取token(2小时)
- public function getToken()
- {
- $url = $this->getTokenUrlStr();
- $res = $this->curl_post($url, $postdata = '', $options = array());
- $data = json_decode($res, JSON_FORCE_OBJECT);
- return $data['access_token'];
- }
- //获取token的url参数拼接
- public function getTokenUrlStr()
- {
- $set = Setting::get('plugin.min_app');
- $getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?"; //获取token的url
- $WXappid = $set['key']; //APPID
- $WXsecret = $set['secret']; //secret
- $str = $getTokenUrl;
- $str .= "grant_type=client_credential&";
- $str .= "appid=" . $WXappid . "&";
- $str .= "secret=" . $WXsecret;
- return $str;
- }
- public function curl_post($url = '', $postdata = '', $options = array())
- {
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
- curl_setopt($ch, CURLOPT_TIMEOUT, 5);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- if (!empty($options)) {
- curl_setopt_array($ch, $options);
- }
- $data = curl_exec($ch);
- curl_close($ch);
- return $data;
- }
- /**
- * 默认字体路径
- *
- * @return string
- */
- private function defaultFontPath()
- {
- return base_path() . DIRECTORY_SEPARATOR . "static" . DIRECTORY_SEPARATOR . "fonts" . DIRECTORY_SEPARATOR . "source_han_sans.ttf";
- }
- protected function changeMemberGoodsPrice()
- {
- $price = 0;
- if ($this->goodsModel->vip_price === null) {
- return $price;
- }
- $price = $this->goodsModel->next_level_price;
- return $price;
- }
- protected function getRealParams($params)
- {
- $params['left'] = intval(str_replace('px', '', $params['left'])) * 2;
- $params['top'] = intval(str_replace('px', '', $params['top'])) * 2;
- $params['width'] = intval(str_replace('px', '', $params['width'])) * 2;
- $params['height'] = intval(str_replace('px', '', $params['height'])) * 2;
- $params['size'] = intval(str_replace('px', '', $params['size'])) * 2;
- $params['src'] = yz_tomedia($params['src']);
- return $params;
- }
- }
|