circle_avatar?:$user->photo; $circle_avatar = $circle_avatar?:$user->wechat->avatar2; $circle_avatar = $circle_avatar?:$user->wechat->avatar; if (empty($circle_avatar)) { return $pic; } $background = "https://images.ufutx.com/202005/14/c1079252fb0888efa9965ca59cd66004.jpeg"; $new_image = $this->imageAddImage($background, $my_qrcode_rect, 260, 260, 244, 240, 'bottom-right'); $new_image = $this->imageAddImage($new_image, $circle_avatar, 168, 168, 300, 318, 'top-right'); $text = 'Hello, 我是'.$user->name; $new_image = $this->textAddImage($new_image, $text, 26, 370, 516); $file_path = storage_path("qrcode/".time()."_my_share_rect.png"); $new_image->save($file_path); if(file_exists($file_path)){ $pic = $this->uploadFile($file_path); try{ unlink($file_path); }catch(\Exception $e) { exit; } }else{ exit; } return $pic; } public function makeMyShareForType($user, $qrcode, $type) { try { $photo = ''; switch ($type) { case ShareImgLog::CHRISTMASTYPE: $photo = $this->makeMyShareChris($user, $qrcode); if (empty($photo)) throw new \Exception("圣诞图生成失败", 1); break; case ShareImgLog::NEWYEARTYPE: $photo = $this->makeMyShareNewYear($user, $qrcode); if (empty($photo)) throw new \Exception("元旦图生成失败", 1); break; case ShareImgLog::WOMENDAYTYPE: $photo = $this->makeMyShareWomenDay($user, $qrcode); if (empty($photo)) throw new \Exception("妇女节图生成失败", 1); break; case ShareImgLog::FULINKTYPE: $photo = $this->makeMyShareFulink($user, $qrcode); if (empty($photo)) throw new \Exception("福恋海报生成失败", 1); break; case ShareImgLog::WHITEDAYTYPE: $photo = $this->makeMyShareWhiteDay($user, $qrcode); if (empty($photo)) throw new \Exception("福恋海报生成失败", 1); break; case ShareImgLog::SINGLETRIPTYPE: $photo = $this->makeMyShareSingleTrip($user, $qrcode); if (empty($photo)) throw new \Exception("单身之旅海报生成失败", 1); break; default: throw new \Exception("类型不存在", 1); break; } return $photo; } catch (\Exception $e) { $this->getError($e); return false; } } public function makeMyShareNewYear($user, $qrcode) { try { //背景图 $background = ShareImgLog::NEWYEAR; $new_image = $this->imageAddImage($background, $qrcode, 180, 180, 282, 1023, 'top-left'); $nickname = str_limit($user->nickname, 5, '...'); if ($nickname) { $new_image = $this->textAddImage($new_image, "@".$nickname, 28, 36, 30, "#898ba4", "left", 'top'); } $file_path = storage_path("qrcode/".time()."_my_share.png"); $new_image->save($file_path); $pic = ''; if(file_exists($file_path)){ $pic = $this->uploadFile($file_path); unlink($file_path); } return $pic; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 圣诞图图 * @param [type] $user [description] * @param [type] $qrcode [description] * @return [type] [description] */ public function makeMyShareChris($user, $qrcode) { try { //背景图 $background = ShareImgLog::CHRISTMAS; $new_image = $this->imageAddImage($background, $qrcode, 260, 260, 244, 616, 'top-left'); $file_path = storage_path("qrcode/".time()."_my_share.png"); $new_image->save($file_path); $pic = ''; if(file_exists($file_path)){ $pic = $this->uploadFile($file_path); unlink($file_path); } return $pic; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 妇女节图 * @param [type] $user [description] * @param [type] $qrcode [description] * @return [type] [description] */ public function makeMyShareWomenDay($user, $qrcode) { try { //背景图 $background = ShareImgLog::WOMENDAY; $new_image = $this->imageAddImage($background, $qrcode, 180, 180, 282, 1000, 'top-left'); $nickname = str_limit($user->nickname, 5, '...'); if ($nickname) { $new_image = $this->textAddImage($new_image, "@".$nickname, 28, 550, 30, "#898ba4", "left", 'top'); } $file_path = storage_path("qrcode/".time()."_my_share.png"); $new_image->save($file_path); $pic = ''; if(file_exists($file_path)){ $pic = $this->uploadFile($file_path); unlink($file_path); } return $pic; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 白色情人节海报 * @param [type] $user [description] * @param [type] $qrcode [description] * @return [type] [description] */ public function makeMyShareWhiteDay($user, $qrcode) { try { //背景图 $background = ShareImgLog::WHITEDAY; $new_image = $this->imageAddImage($background, $qrcode, 180, 180, 282, 930, 'top-left'); // $nickname = $user->nickname; // if ($nickname) { // $new_image = $this->textAddImage($new_image, "@".$nickname, 28, 550, 30, "#898ba4", "left", 'top'); // } $file_path = storage_path("qrcode/".time()."_my_share.png"); $new_image->save($file_path); $pic = ''; if(file_exists($file_path)){ $pic = $this->uploadFile($file_path); unlink($file_path); } return $pic; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 福恋海报 * @param [type] $user [description] * @param [type] $qrcode [description] * @return [type] [description] */ public function makeMyShareFulink($user, $qrcode) { try { //背景图 $background = ShareImgLog::FULINK; $new_image = $this->imageAddImage($background, $qrcode, 180, 180, 283, 1000, 'top-left'); $nickname = str_limit($user->nickname, 6, '...'); if ($nickname) { $new_image = $this->textAddImage($new_image, "@".$nickname, 28, 550, 30, "#898ba4", "left", 'top'); } $file_path = storage_path("qrcode/".time()."_my_share.png"); $new_image->save($file_path); $pic = ''; if(file_exists($file_path)){ $pic = $this->uploadFile($file_path); unlink($file_path); } return $pic; } catch (\Exception $e) { $this->getError($e); return false; } } public function makeMyShareSingleTrip($user, $qrcode) { try { //背景图 $background = ShareImgLog::SINGLETRIP; $new_image = $this->imageAddImage($background, $qrcode, 180, 180, 82, 1101, 'top-left'); $nickname = str_limit($user->nickname, 8, '...'); if ($nickname) { $new_image = $this->textAddImage($new_image, "@".$nickname, 28, 550, 30, "#FF8193", "left", 'top'); } $file_path = storage_path("qrcode/".time().rand(0,100)."_my_share.png"); $new_image->save($file_path); $pic = ''; if(file_exists($file_path)){ $pic = $this->uploadFile($file_path); unlink($file_path); } return $pic; } catch (\Exception $e) { $this->getError($e); return false; } } public function makeMySharePoster($user, $qrcode, $background) { try { $new_image = $this->imageAddImage($background, $qrcode, 180, 180,524 , 1074, 'top-left'); // $new_image = $this->imageAddImage($background, $qrcode, 180, 180, 82, 1101, 'top-left'); $nickname = str_limit($user->nickname, 8, '...'); if ($nickname) { $new_image = $this->textAddImage($new_image, "@".$nickname, 28, 500, 30, "#FFA667", "left", 'top'); } $file_path = storage_path("qrcode/".time().rand(0,100)."_my_share.png"); $new_image->save($file_path); $pic = ''; if(file_exists($file_path)){ $pic = $this->uploadFile($file_path); unlink($file_path); } return $pic; } catch (\Exception $e) { $this->getError($e); return false; } } public function getQrCodeRect($user) { $my_qrcode_rect = $user->my_qrcode_rect; if (empty($my_qrcode_rect)) { $openid = $user->wechat?$user->wechat->openid:null; $app = EasyWechat::miniProgram(); $width = 260; $optional = [ 'page' => 'pages/tabBar/welcome', 'width' => 260, 'is_hyaline'=>true, 'auto_color'=>true, ]; $response = $app->app_code->getUnlimit('from_user_id='.$user->id, $optional); $qrcode_path = storage_path('qrcode'); $file_name = time().'_my_qrcode_rect.png'; $file_path = $qrcode_path.'/'.$file_name; // 保存小程序码到文件 if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) { $filename = $response->saveAs($qrcode_path, $file_name); } $qrcode = null; if(file_exists($file_path)){ $qrcode = $this->uploadFile($file_path); try{ unlink($file_path); }catch(\Exception $e) { $this->getError($e); throw new \Exception("二维码上传失败, 用户id-".$user->id, 500); exit; } }else{ throw new \Exception("二维码文件不存在, 用户id-".$user->id, 500); exit; } $my_qrcode_rect = $qrcode; } return $my_qrcode_rect; } public function getUnlimitQrcode($path, $scene) { $app = EasyWechat::miniProgram(); $optional = [ 'page' => $path, // 'page' => 'pages/users/whiteValentine', 'is_hyaline'=>true, ]; $response = $app->app_code->getUnlimit($scene, $optional); $qrcode_path = storage_path('qrcode'); $file_name = md5(microtime(true)).'.png'; $file_path = $qrcode_path.'/'.$file_name; // 保存小程序码到文件 if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) { $filename = $response->saveAs($qrcode_path, $file_name); } $qrcode = null; if(file_exists($file_path)) { $qrcode = $this->uploadFile($file_path); try { unlink($file_path); } catch (\Exception $e) { $this->getError($e); exit; } } return $qrcode; } /** * 获取小程序码 * @param [type] $user [description] * @return [type] [description] */ public function getQrCode($user) { $my_qrcode = $user->my_qrcode; if (empty($my_qrcode)) { $app = EasyWechat::miniProgram(); $optional = [ 'page' => 'pages/tabBar/welcome', // 'page' => 'pages/users/whiteValentine', 'is_hyaline'=>true, ]; $response = $app->app_code->getUnlimit('from_user_id='.$user->id, $optional); $qrcode_path = storage_path('qrcode'); $file_name = time().'_my_qrcode.png'; $file_path = $qrcode_path.'/'.$file_name; // 保存小程序码到文件 if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) { $filename = $response->saveAs($qrcode_path, $file_name); } // dd($filename); $qrcode = null; if(file_exists($file_path)){ $qrcode = $this->uploadFile($file_path); try{ // unlink($file_path); }catch(\Exception $e) { $this->getError($e); throw new \Exception("二维码上传失败, 用户id-".$user->id, 500); exit; } }else{ throw new \Exception("二维码文件不存在, 用户id-".$user->id, 500); exit; } $my_qrcode = $qrcode; } return $my_qrcode; } /** * 获取小程序码 * @param [type] $user [description] * @return [type] [description] */ public function getQrCodeV2($user,$page) { $app = EasyWechat::miniProgram(); $optional = [ 'page' => $page, 'is_hyaline'=>true, ]; $response = $app->app_code->getUnlimit('from_user_id='.$user->id, $optional); $qrcode_path = storage_path('qrcode'); $file_name = time().'_my_qrcode.png'; $file_path = $qrcode_path.'/'.$file_name; // 保存小程序码到文件 if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) { $filename = $response->saveAs($qrcode_path, $file_name); } // dd($filename); $qrcode = null; if(file_exists($file_path)){ $qrcode = $this->uploadFile($file_path); try{ unlink($file_path); }catch(\Exception $e) { $this->getError($e); throw new \Exception("二维码上传失败, 用户id-".$user->id, 500); exit; } }else{ throw new \Exception("二维码文件不存在, 用户id-".$user->id, 500); exit; } $my_qrcode = $qrcode; if ($user->my_qrcode != $my_qrcode) { User::where('id',$user->id)->update(['my_qrcode'=> $my_qrcode]); } return $my_qrcode; } //生成临时跳到小程序用户详情的码 public function getQrcodeToMini($user_id){ try { $path = 'pages/home/information?id='.$user_id; $app = EasyWechat::miniProgram(); $response = $app->app_code->get($path); $file_path = ''; $qrcode_path = storage_path('qrcode'); if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) { $filename = $response->save($qrcode_path, 'miniUser'.$user_id); $file_path = $qrcode_path.'/'.$filename; } if (empty($file_path)) return false; $qrcode = null; if(file_exists($file_path)){ try{ $qrcode = $this->uploadFile($file_path); $result = unlink($file_path); }catch(\Exception $e) { \Log::error($e->getMessage()); // throw new \Exception("二维码上传失败, 用户id-".$user_id, 500); return false; } }else{ \Log::error("二维码文件不存在"); return false; } return $qrcode; }catch (\Exception $e) { $this->getError($e); return false; } } public function getViewerMiniQrcode($user_id) { $path = 'pages/home/information'; $app = EasyWechat::miniProgram(); $scene = 'user_id='.$user_id; $response = $app->app_code->getUnlimit($scene, [ 'page'=>$path, 'is_hyaline'=>true ]); $qrcode_path = storage_path('qrcode'); if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) { $filename = $response->save($qrcode_path); } $file_path = $qrcode_path.'/'.$filename; $qrcode = null; if(file_exists($file_path)){ try{ $qrcode = $this->uploadFile($file_path); $result = unlink($file_path); }catch(\Exception $e) { \Log::error($e->getMessage()); // throw new \Exception("二维码上传失败, 用户id-".$user_id, 500); return false; } }else{ \Log::error("二维码文件不存在"); return false; } return $qrcode; } public function getServiceMiniQrcode($path) { // $path = 'pages/home/information?id='.$user_id; $app = EasyWechat::miniProgram(); $response = $app->app_code->get($path); $qrcode_path = storage_path('qrcode'); if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) { $filename = $response->save($qrcode_path, 'upgrade.jpeg'); } $file_path = $qrcode_path.'/'.$filename; $qrcode = null; if(file_exists($file_path)){ try{ $qrcode = $this->uploadFile($file_path); $result = unlink($file_path); }catch(\Exception $e) { \Log::error($e->getMessage()); // throw new \Exception("二维码上传失败, 用户id-".$user_id, 500); return false; } }else{ \Log::error("二维码文件不存在"); return false; } return $qrcode; } //生成跳转到小程序的二维码 public function getServiceToMiniQrcode($path) { $app = EasyWechat::miniProgram(); $width = 260; $response = $app->app_code->getQrCode($path, $width); $qrcode_path = storage_path('qrcode'); if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) { $filename = $response->save($qrcode_path, 'upgrade.jpeg'); } $file_path = $qrcode_path.'/'.$filename; $qrcode = null; if(file_exists($file_path)){ try{ $qrcode = $this->uploadFile($file_path); $result = unlink($file_path); }catch(\Exception $e) { \Log::error($e->getMessage()); // throw new \Exception("二维码上传失败, 用户id-".$user_id, 500); return false; } }else{ \Log::error("二维码文件不存在"); return false; } return $qrcode; } /** * 图片添加图片 * @param string $image1 第一张图片 * @param string $image2 第二张图片 * @param int $image2_w 图片宽度 * @param int $image2_h 图片高度 * @param integer $offset_x 文字X轴偏移 * @param integer $offset_y 文字Y轴偏移 * @param string $offset * @return image 新图片 */ public function imageAddImage($image1, $image2, $image2_w, $image2_h, $offset_x=0, $offset_y=0, $offset='top-left'){ Image::configure(['driver'=>'gd']); $image = Image::make($image1); $image_v2 = Image::make($image2)->resize($image2_w, $image2_h); $image_width = $image->width(); $image_height = $image->height(); $image_copy = $image->insert($image_v2, $offset, $offset_x, $offset_y); return $image_copy; } /** * 图片添加文字 * @param string $image 图片 * @param string $text 文字 * @param int $text_size 文字尺寸 * @param int $offset_x 文字X轴偏移 * @param int $offset_y 文字Y轴偏移 * @param string $color 文字颜色 * @param string $align 文字位置 * @param string $valign * @return image 新图片 */ public function textAddImage($image, $text, $text_size, $offset_x, $offset_y, $color='#000000', $align = 'center', $valign='top'){ Image::configure(['driver'=>'gd']); $image = Image::make($image); $image->text($text, $offset_x, $offset_y,function($font) use($text_size, $color, $align, $valign) { $font->file(storage_path().'/font/a.ttc'); $font->size($text_size); $font->color($color); $font->align($align); $font->valign($valign); }); return $image; } public function uploadFile($file){ $ossClient = UploadController::getOssClient(); //生成file $object = date('Y').date('m')."/".date('d')."/".basename($file); $url = 'https://'.config('alioss.picture_domain').'/'.$object; try { // $result = $ossClient->putObject(config('alioss.buckets.picture'), $object, $file); $result = $ossClient->uploadFile(config('alioss.buckets.picture'), $object, $file); } catch(\OSS\Core\OssException $e) { // return $this->failure('oss_put_failure', $e->getMessage()); } return $url; } public function getOfficialForeverQrcode($scene): string { $app = EasyWechat::officialAccount("new"); $result = $app->qrcode->forever($scene); $qrcode_url = $app->qrcode->url($result['ticket']); return $qrcode_url; } }