sharePath = THUMB_PATH."share".DIRECTORY_SEPARATOR; dir_create($this->sharePath); //字体 $this->font = BASE_PATH.'/static/fonts/msyh.ttc'; } //生成文档分享海报 public function actionSharedoc() { $id = $this->post['id']; $doc = DocReal::findOne($id); if($doc) { $background = getFileUrl($this->posterconfig['doc']); if($this->user) { $avatar = getFileUrl($this->userInfo['avatar']); if(empty($avatar)) { $avatar = getFileUrl($this->imageconfig['noavatar']); } $nick_name = $this->userInfo['nick_name']; } else { $avatar = getFileUrl($this->imageconfig['wapshortlogo']); $nick_name = $this->baseconfig['brand']; } $title = $doc->title; $thumb = Yii::$app->controller->makeThumb($doc->thumb,320,362); $thumbinfo = getimagesize($thumb); $thumbwidth = $thumbinfo[0]; $thumbheight = $thumbinfo[1]; if($thumbheight>$thumbwidth) { $thumbleft = 90+ intval(abs(416-256)/2); $thumbtop = 296+ intval(abs(290-279)/2); $thumbCofig = array( 'url'=>$thumb, 'stream'=>0, 'left'=>$thumbleft, 'top'=>$thumbtop, 'right'=>0, 'bottom'=>0, 'width'=>256, 'height'=>279, 'opacity'=>100 ); } else { $thumbleft = 90 + intval(abs(416-320)/2); $thumbtop = 296 + intval(abs(290-180)/2); $thumbCofig = array( 'url'=>$thumb, 'stream'=>0, 'left'=>$thumbleft, 'top'=>$thumbtop, 'right'=>0, 'bottom'=>0, 'width'=>320, 'height'=>180, 'opacity'=>100 ); } if($background) { //生成底图 $bgImg = $this->sharePath.'docshare.png'; if(!file_exists($bgImg))file_put_contents($bgImg,https_request($background)); //二维码 if($this->user) { $shareLog = createShareNo(DocReal::shortTableName(),$id,$this->user->user_id); $shareUrl = safe_replace($this->post['url']).'?'.Yii::$app->params['shareArgName'].'='.$shareLog->share_no; } else { $shareUrl = safe_replace($this->post['url']); } $qrcode = new \app\components\qrcode\qrcode($shareUrl); $qrcodeImg = $this->sharePath.md5($shareUrl).'.png'; $qrcode->create($qrcodeImg); //文案处理 if(!empty($title)) { $pos = imagettfbbox(18,0,$this->font,$title); $textConfig = array( 'text' => ($pos[2]-$pos[0])>380?str_cut($title,0,16,'...'):$title, 'left' => 90, 'top' => 270, 'fontSize' => 18,//字号 'fontColor' => '65,113,179', //字体颜色 'angle' => 0, 'fontPath'=>$this->font, ); $text1Config = array( 'text'=>'手机扫一扫,立即下载', 'left'=>150, 'top'=>670, 'fontSize'=>15,//字号 'fontColor'=>'102,102,102', //字体颜色 'angle'=>0, 'fontPath'=>$this->font, ); } else { $textConfig = array(); $text1Config = array(); } //头像和昵称配置 $avatarConfig = []; $nicknameConfig = []; $avatarConfig = array( 'url'=>$avatar, 'stream'=>0, 'left'=>50, 'top'=>50, 'right'=>0, 'bottom'=>0, 'width'=>50, 'height'=>50, 'opacity'=>100 ); $nicknameConfig = array( 'text'=>$nick_name, 'left'=>110, 'top'=>90, 'fontSize'=>24, //字号 'fontColor'=>'255,255,255', //字体颜色 'angle'=>0, 'fontPath'=>$this->font, ); //海报目标文件 $posterImg = $this->sharePath.md5($shareUrl).'-1.png'; $noimage = 0; $config = array( 'image'=>array( array( 'url'=>$qrcodeImg,//二维码资源 'stream'=>0, 'left'=>373, 'top'=>620, 'right'=>0, 'bottom'=>0, 'width'=>128, 'height'=>128, 'opacity'=>100 ) ), 'avatar'=>array( $avatarConfig ), 'nickname'=>array( $nicknameConfig ), 'text'=>array( $textConfig ), 'text1'=>array( $text1Config ), 'thumb'=>array( $thumbCofig ), 'background'=>$bgImg ); //生成海报 if(!file_exists($posterImg)) { createPoster($config,$posterImg); } $posterUrl = APP_URL.'thumb/'.str_replace(DIRECTORY_SEPARATOR,'/',str_replace(THUMB_PATH,'',$posterImg)); return [ 'error' => 0, 'msg' => '生成成功', 'data' => ['posterUrl'=>$posterUrl], 'code'=>200 ]; } else { return [ 'error' => 1, 'msg' => '未设置海报底图', 'data' => [], 'code'=>200 ]; } } else { return [ 'error' => 1, 'msg' => '系统错误', 'data' => [], 'code'=>200 ]; } } //通用分享 public function actionSharecom() { $type = $this->post['type']; $url = $this->post['url']; if(empty($type)||empty($url)) { return [ 'error' => 1, 'msg' => '系统错误', 'data' => [], 'code'=>200 ]; } $background = getFileUrl($this->posterconfig['common'.$type]); $nick_name = '我是'.$this->userInfo['nick_name']; $title = "我为【".$this->baseconfig['brand'].'】代言'; //生成底图 $bgImg = $this->sharePath.'common_'.$type.'.png'; if(!file_exists($bgImg))file_put_contents($bgImg,https_request($background)); //二维码 $qrcode = new \app\components\qrcode\qrcode($url); $qrcodeImg = $this->sharePath.md5($url).'.png'; $qrcode->create($qrcodeImg); //文案处理 if($type==1) { //昵称 $textConfig = array( 'text' => $nick_name, 'left' => 31, 'top' => 945, 'fontSize' => 18,//字号 'fontColor' => '0,0,0', //字体颜色 'angle' => 0, 'fontPath'=>$this->font, ); //标题 $text1Config = array( 'text'=> $title, 'left'=>31, 'top'=>1030, 'fontSize'=>18,//字号 'fontColor'=>'0,0,0', //字体颜色 'angle'=>0, 'fontPath'=>$this->font, ); //二维码 $imageConfig = array( 'url'=>$qrcodeImg, 'stream'=>0, 'left'=>490, 'top'=>958, 'right'=>0, 'bottom'=>0, 'width'=>110, 'height'=>110, 'opacity'=>100 ); }else if($type==2){ $pos = imagettfbbox(18,0,$this->font,$nick_name); //昵称 $textConfig = array( 'text' => $nick_name, 'left' => (640-($pos[2]-$pos[0]))/2, 'top' => 820, 'fontSize' => 18,//字号 'fontColor' => '0,0,0', //字体颜色 'angle' => 0, 'fontPath'=>$this->font, ); $pos = imagettfbbox(18,0,$this->font,$title); //标题 $text1Config = array( 'text'=> $title, 'left'=>(640-($pos[2]-$pos[0]))/2, 'top'=>870, 'fontSize'=>18,//字号 'fontColor'=>'0,0,0', //字体颜色 'angle'=>0, 'fontPath'=>$this->font, ); //二维码 $imageConfig = array( 'url'=>$qrcodeImg, 'stream'=>0, 'left'=>(640-110)/2, 'top'=>925, 'right'=>0, 'bottom'=>0, 'width'=>110, 'height'=>110, 'opacity'=>100 ); }else if($type==3){ $pos = imagettfbbox(18,0,$this->font,$nick_name); //昵称 $textConfig = array( 'text' => $nick_name, 'left' => -($pos[2]-$pos[0]+45), 'top' => 920, 'fontSize' => 18,//字号 'fontColor' => '0,0,0', //字体颜色 'angle' => 0, 'fontPath'=>$this->font, ); $pos = imagettfbbox(18,0,$this->font,$title); //标题 $text1Config = array( 'text'=> $title, 'left'=> -($pos[2]-$pos[0]+45), 'top'=>966, 'fontSize'=>18,//字号 'fontColor'=>'0,0,0', //字体颜色 'angle'=>0, 'fontPath'=>$this->font, ); //二维码 $imageConfig = array( 'url'=>$qrcodeImg, 'stream'=>0, 'left'=>75, 'top'=>880, 'right'=>0, 'bottom'=>0, 'width'=>127, 'height'=>127, 'opacity'=>100 ); }else if($type==4){ $pos = imagettfbbox(18,0,$this->font,$nick_name); //昵称 $textConfig = array( 'text' => $nick_name, 'left' => (640-($pos[2]-$pos[0]))/2, 'top' => 942, 'fontSize' => 18,//字号 'fontColor' => '255,255,255', //字体颜色 'angle' => 0, 'fontPath'=>$this->font, ); $pos = imagettfbbox(18,0,$this->font,$title); //标题 $text1Config = array( 'text'=> $title, 'left'=>(640-($pos[2]-$pos[0]))/2, 'top'=>985, 'fontSize'=>18,//字号 'fontColor'=>'255,255,255', //字体颜色 'angle'=>0, 'fontPath'=>$this->font, ); //二维码 $imageConfig = array( 'url'=>$qrcodeImg, 'stream'=>0, 'left'=>(640-148)/2, 'top'=>744, 'right'=>0, 'bottom'=>0, 'width'=>148, 'height'=>148, 'opacity'=>100 ); }else if($type==5){ $avatar = getFileUrl($this->userInfo['avatar']); if(empty($avatar)) { $avatar = getFileUrl($this->imageconfig['noavatar']); } $nick_name = $this->userInfo['nick_name']; $title = $this->posterconfig['title']?$this->posterconfig['title']:'手机扫一扫立即加入'; $textConfig = array( 'text'=>$title, 'left'=>185, 'top'=>675, 'fontSize'=>15,//字号 'fontColor'=>'255,255,255', //字体颜色 'angle'=>0, 'fontPath'=>$this->font ); $nicknameConfig = array( 'text'=>$nick_name."推荐", 'left'=>190, 'top'=>725, 'fontSize'=>20,//字号 'fontColor'=>'255,255,255', //字体颜色 'angle'=>0, 'fontPath'=>$this->font ); $avatarConfig = array( 'url'=>$avatar, 'stream'=>0, 'left'=>120, 'top'=>685, 'right'=>0, 'bottom'=>0, 'width'=>60, 'height'=>60, 'opacity'=>100 ); $imageConfig = array( 'url'=>$qrcodeImg,//二维码资源 'stream'=>0, 'left'=>420, 'top'=>620, 'right'=>0, 'bottom'=>0, 'width'=>127, 'height'=>127, 'opacity'=>100 ); } $config = array( 'image'=>array( $imageConfig ), 'text'=>array( $textConfig ), 'text1'=>array( $text1Config ), 'nickname'=>array( $nicknameConfig ), 'avatar'=>array( $avatarConfig ), 'background'=>$bgImg ); //海报目标文件 $posterImg = $this->sharePath.md5($url.$type).'-1.png'; //生成海报 if(!file_exists($posterImg)) { createPoster($config,$posterImg); } $posterUrl = APP_URL.'thumb/'.str_replace(DIRECTORY_SEPARATOR,'/',str_replace(THUMB_PATH,'',$posterImg)); return [ 'error' => 0, 'msg' => '生成成功', 'data' => ['posterUrl'=>$posterUrl], 'code'=>200 ]; } }