123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490 |
- <?php
- namespace api\controllers;
- use app\common\controllers\AController;
- use app\common\helpers\Identify;
- use app\modules\doc\models\DocReal;
- use app\modules\ucenter\models\User;
- use Yii;
- class ShareController extends AController
- {
- public $sharePath;
- public $font;
- public function init()
- {
- parent::init();
- $this->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
- ];
- }
- }
|