ShareController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <?php
  2. namespace api\controllers;
  3. use app\common\controllers\AController;
  4. use app\common\helpers\Identify;
  5. use app\modules\doc\models\DocReal;
  6. use app\modules\ucenter\models\User;
  7. use Yii;
  8. class ShareController extends AController
  9. {
  10. public $sharePath;
  11. public $font;
  12. public function init()
  13. {
  14. parent::init();
  15. $this->sharePath = THUMB_PATH."share".DIRECTORY_SEPARATOR;
  16. dir_create($this->sharePath);
  17. //字体
  18. $this->font = BASE_PATH.'/static/fonts/msyh.ttc';
  19. }
  20. //生成文档分享海报
  21. public function actionSharedoc()
  22. {
  23. $id = $this->post['id'];
  24. $doc = DocReal::findOne($id);
  25. if($doc)
  26. {
  27. $background = getFileUrl($this->posterconfig['doc']);
  28. if($this->user)
  29. {
  30. $avatar = getFileUrl($this->userInfo['avatar']);
  31. if(empty($avatar))
  32. {
  33. $avatar = getFileUrl($this->imageconfig['noavatar']);
  34. }
  35. $nick_name = $this->userInfo['nick_name'];
  36. }
  37. else
  38. {
  39. $avatar = getFileUrl($this->imageconfig['wapshortlogo']);
  40. $nick_name = $this->baseconfig['brand'];
  41. }
  42. $title = $doc->title;
  43. $thumb = Yii::$app->controller->makeThumb($doc->thumb,320,362);
  44. $thumbinfo = getimagesize($thumb);
  45. $thumbwidth = $thumbinfo[0];
  46. $thumbheight = $thumbinfo[1];
  47. if($thumbheight>$thumbwidth)
  48. {
  49. $thumbleft = 90+ intval(abs(416-256)/2);
  50. $thumbtop = 296+ intval(abs(290-279)/2);
  51. $thumbCofig = array(
  52. 'url'=>$thumb,
  53. 'stream'=>0,
  54. 'left'=>$thumbleft,
  55. 'top'=>$thumbtop,
  56. 'right'=>0,
  57. 'bottom'=>0,
  58. 'width'=>256,
  59. 'height'=>279,
  60. 'opacity'=>100
  61. );
  62. }
  63. else
  64. {
  65. $thumbleft = 90 + intval(abs(416-320)/2);
  66. $thumbtop = 296 + intval(abs(290-180)/2);
  67. $thumbCofig = array(
  68. 'url'=>$thumb,
  69. 'stream'=>0,
  70. 'left'=>$thumbleft,
  71. 'top'=>$thumbtop,
  72. 'right'=>0,
  73. 'bottom'=>0,
  74. 'width'=>320,
  75. 'height'=>180,
  76. 'opacity'=>100
  77. );
  78. }
  79. if($background)
  80. {
  81. //生成底图
  82. $bgImg = $this->sharePath.'docshare.png';
  83. if(!file_exists($bgImg))file_put_contents($bgImg,https_request($background));
  84. //二维码
  85. if($this->user)
  86. {
  87. $shareLog = createShareNo(DocReal::shortTableName(),$id,$this->user->user_id);
  88. $shareUrl = safe_replace($this->post['url']).'?'.Yii::$app->params['shareArgName'].'='.$shareLog->share_no;
  89. }
  90. else
  91. {
  92. $shareUrl = safe_replace($this->post['url']);
  93. }
  94. $qrcode = new \app\components\qrcode\qrcode($shareUrl);
  95. $qrcodeImg = $this->sharePath.md5($shareUrl).'.png';
  96. $qrcode->create($qrcodeImg);
  97. //文案处理
  98. if(!empty($title))
  99. {
  100. $pos = imagettfbbox(18,0,$this->font,$title);
  101. $textConfig = array(
  102. 'text' => ($pos[2]-$pos[0])>380?str_cut($title,0,16,'...'):$title,
  103. 'left' => 90,
  104. 'top' => 270,
  105. 'fontSize' => 18,//字号
  106. 'fontColor' => '65,113,179', //字体颜色
  107. 'angle' => 0,
  108. 'fontPath'=>$this->font,
  109. );
  110. $text1Config = array(
  111. 'text'=>'手机扫一扫,立即下载',
  112. 'left'=>150,
  113. 'top'=>670,
  114. 'fontSize'=>15,//字号
  115. 'fontColor'=>'102,102,102', //字体颜色
  116. 'angle'=>0,
  117. 'fontPath'=>$this->font,
  118. );
  119. }
  120. else
  121. {
  122. $textConfig = array();
  123. $text1Config = array();
  124. }
  125. //头像和昵称配置
  126. $avatarConfig = [];
  127. $nicknameConfig = [];
  128. $avatarConfig = array(
  129. 'url'=>$avatar,
  130. 'stream'=>0,
  131. 'left'=>50,
  132. 'top'=>50,
  133. 'right'=>0,
  134. 'bottom'=>0,
  135. 'width'=>50,
  136. 'height'=>50,
  137. 'opacity'=>100
  138. );
  139. $nicknameConfig = array(
  140. 'text'=>$nick_name,
  141. 'left'=>110,
  142. 'top'=>90,
  143. 'fontSize'=>24, //字号
  144. 'fontColor'=>'255,255,255', //字体颜色
  145. 'angle'=>0,
  146. 'fontPath'=>$this->font,
  147. );
  148. //海报目标文件
  149. $posterImg = $this->sharePath.md5($shareUrl).'-1.png';
  150. $noimage = 0;
  151. $config = array(
  152. 'image'=>array(
  153. array(
  154. 'url'=>$qrcodeImg,//二维码资源
  155. 'stream'=>0,
  156. 'left'=>373,
  157. 'top'=>620,
  158. 'right'=>0,
  159. 'bottom'=>0,
  160. 'width'=>128,
  161. 'height'=>128,
  162. 'opacity'=>100
  163. )
  164. ),
  165. 'avatar'=>array(
  166. $avatarConfig
  167. ),
  168. 'nickname'=>array(
  169. $nicknameConfig
  170. ),
  171. 'text'=>array(
  172. $textConfig
  173. ),
  174. 'text1'=>array(
  175. $text1Config
  176. ),
  177. 'thumb'=>array(
  178. $thumbCofig
  179. ),
  180. 'background'=>$bgImg
  181. );
  182. //生成海报
  183. if(!file_exists($posterImg))
  184. {
  185. createPoster($config,$posterImg);
  186. }
  187. $posterUrl = APP_URL.'thumb/'.str_replace(DIRECTORY_SEPARATOR,'/',str_replace(THUMB_PATH,'',$posterImg));
  188. return [
  189. 'error' => 0,
  190. 'msg' => '生成成功',
  191. 'data' => ['posterUrl'=>$posterUrl],
  192. 'code'=>200
  193. ];
  194. }
  195. else
  196. {
  197. return [
  198. 'error' => 1,
  199. 'msg' => '未设置海报底图',
  200. 'data' => [],
  201. 'code'=>200
  202. ];
  203. }
  204. }
  205. else
  206. {
  207. return [
  208. 'error' => 1,
  209. 'msg' => '系统错误',
  210. 'data' => [],
  211. 'code'=>200
  212. ];
  213. }
  214. }
  215. //通用分享
  216. public function actionSharecom()
  217. {
  218. $type = $this->post['type'];
  219. $url = $this->post['url'];
  220. if(empty($type)||empty($url))
  221. {
  222. return [
  223. 'error' => 1,
  224. 'msg' => '系统错误',
  225. 'data' => [],
  226. 'code'=>200
  227. ];
  228. }
  229. $background = getFileUrl($this->posterconfig['common'.$type]);
  230. $nick_name = '我是'.$this->userInfo['nick_name'];
  231. $title = "我为【".$this->baseconfig['brand'].'】代言';
  232. //生成底图
  233. $bgImg = $this->sharePath.'common_'.$type.'.png';
  234. if(!file_exists($bgImg))file_put_contents($bgImg,https_request($background));
  235. //二维码
  236. $qrcode = new \app\components\qrcode\qrcode($url);
  237. $qrcodeImg = $this->sharePath.md5($url).'.png';
  238. $qrcode->create($qrcodeImg);
  239. //文案处理
  240. if($type==1)
  241. {
  242. //昵称
  243. $textConfig = array(
  244. 'text' => $nick_name,
  245. 'left' => 31,
  246. 'top' => 945,
  247. 'fontSize' => 18,//字号
  248. 'fontColor' => '0,0,0', //字体颜色
  249. 'angle' => 0,
  250. 'fontPath'=>$this->font,
  251. );
  252. //标题
  253. $text1Config = array(
  254. 'text'=> $title,
  255. 'left'=>31,
  256. 'top'=>1030,
  257. 'fontSize'=>18,//字号
  258. 'fontColor'=>'0,0,0', //字体颜色
  259. 'angle'=>0,
  260. 'fontPath'=>$this->font,
  261. );
  262. //二维码
  263. $imageConfig = array(
  264. 'url'=>$qrcodeImg,
  265. 'stream'=>0,
  266. 'left'=>490,
  267. 'top'=>958,
  268. 'right'=>0,
  269. 'bottom'=>0,
  270. 'width'=>110,
  271. 'height'=>110,
  272. 'opacity'=>100
  273. );
  274. }else if($type==2){
  275. $pos = imagettfbbox(18,0,$this->font,$nick_name);
  276. //昵称
  277. $textConfig = array(
  278. 'text' => $nick_name,
  279. 'left' => (640-($pos[2]-$pos[0]))/2,
  280. 'top' => 820,
  281. 'fontSize' => 18,//字号
  282. 'fontColor' => '0,0,0', //字体颜色
  283. 'angle' => 0,
  284. 'fontPath'=>$this->font,
  285. );
  286. $pos = imagettfbbox(18,0,$this->font,$title);
  287. //标题
  288. $text1Config = array(
  289. 'text'=> $title,
  290. 'left'=>(640-($pos[2]-$pos[0]))/2,
  291. 'top'=>870,
  292. 'fontSize'=>18,//字号
  293. 'fontColor'=>'0,0,0', //字体颜色
  294. 'angle'=>0,
  295. 'fontPath'=>$this->font,
  296. );
  297. //二维码
  298. $imageConfig = array(
  299. 'url'=>$qrcodeImg,
  300. 'stream'=>0,
  301. 'left'=>(640-110)/2,
  302. 'top'=>925,
  303. 'right'=>0,
  304. 'bottom'=>0,
  305. 'width'=>110,
  306. 'height'=>110,
  307. 'opacity'=>100
  308. );
  309. }else if($type==3){
  310. $pos = imagettfbbox(18,0,$this->font,$nick_name);
  311. //昵称
  312. $textConfig = array(
  313. 'text' => $nick_name,
  314. 'left' => -($pos[2]-$pos[0]+45),
  315. 'top' => 920,
  316. 'fontSize' => 18,//字号
  317. 'fontColor' => '0,0,0', //字体颜色
  318. 'angle' => 0,
  319. 'fontPath'=>$this->font,
  320. );
  321. $pos = imagettfbbox(18,0,$this->font,$title);
  322. //标题
  323. $text1Config = array(
  324. 'text'=> $title,
  325. 'left'=> -($pos[2]-$pos[0]+45),
  326. 'top'=>966,
  327. 'fontSize'=>18,//字号
  328. 'fontColor'=>'0,0,0', //字体颜色
  329. 'angle'=>0,
  330. 'fontPath'=>$this->font,
  331. );
  332. //二维码
  333. $imageConfig = array(
  334. 'url'=>$qrcodeImg,
  335. 'stream'=>0,
  336. 'left'=>75,
  337. 'top'=>880,
  338. 'right'=>0,
  339. 'bottom'=>0,
  340. 'width'=>127,
  341. 'height'=>127,
  342. 'opacity'=>100
  343. );
  344. }else if($type==4){
  345. $pos = imagettfbbox(18,0,$this->font,$nick_name);
  346. //昵称
  347. $textConfig = array(
  348. 'text' => $nick_name,
  349. 'left' => (640-($pos[2]-$pos[0]))/2,
  350. 'top' => 942,
  351. 'fontSize' => 18,//字号
  352. 'fontColor' => '255,255,255', //字体颜色
  353. 'angle' => 0,
  354. 'fontPath'=>$this->font,
  355. );
  356. $pos = imagettfbbox(18,0,$this->font,$title);
  357. //标题
  358. $text1Config = array(
  359. 'text'=> $title,
  360. 'left'=>(640-($pos[2]-$pos[0]))/2,
  361. 'top'=>985,
  362. 'fontSize'=>18,//字号
  363. 'fontColor'=>'255,255,255', //字体颜色
  364. 'angle'=>0,
  365. 'fontPath'=>$this->font,
  366. );
  367. //二维码
  368. $imageConfig = array(
  369. 'url'=>$qrcodeImg,
  370. 'stream'=>0,
  371. 'left'=>(640-148)/2,
  372. 'top'=>744,
  373. 'right'=>0,
  374. 'bottom'=>0,
  375. 'width'=>148,
  376. 'height'=>148,
  377. 'opacity'=>100
  378. );
  379. }else if($type==5){
  380. $avatar = getFileUrl($this->userInfo['avatar']);
  381. if(empty($avatar))
  382. {
  383. $avatar = getFileUrl($this->imageconfig['noavatar']);
  384. }
  385. $nick_name = $this->userInfo['nick_name'];
  386. $title = $this->posterconfig['title']?$this->posterconfig['title']:'手机扫一扫立即加入';
  387. $textConfig = array(
  388. 'text'=>$title,
  389. 'left'=>185,
  390. 'top'=>675,
  391. 'fontSize'=>15,//字号
  392. 'fontColor'=>'255,255,255', //字体颜色
  393. 'angle'=>0,
  394. 'fontPath'=>$this->font
  395. );
  396. $nicknameConfig = array(
  397. 'text'=>$nick_name."推荐",
  398. 'left'=>190,
  399. 'top'=>725,
  400. 'fontSize'=>20,//字号
  401. 'fontColor'=>'255,255,255', //字体颜色
  402. 'angle'=>0,
  403. 'fontPath'=>$this->font
  404. );
  405. $avatarConfig = array(
  406. 'url'=>$avatar,
  407. 'stream'=>0,
  408. 'left'=>120,
  409. 'top'=>685,
  410. 'right'=>0,
  411. 'bottom'=>0,
  412. 'width'=>60,
  413. 'height'=>60,
  414. 'opacity'=>100
  415. );
  416. $imageConfig = array(
  417. 'url'=>$qrcodeImg,//二维码资源
  418. 'stream'=>0,
  419. 'left'=>420,
  420. 'top'=>620,
  421. 'right'=>0,
  422. 'bottom'=>0,
  423. 'width'=>127,
  424. 'height'=>127,
  425. 'opacity'=>100
  426. );
  427. }
  428. $config = array(
  429. 'image'=>array(
  430. $imageConfig
  431. ),
  432. 'text'=>array(
  433. $textConfig
  434. ),
  435. 'text1'=>array(
  436. $text1Config
  437. ),
  438. 'nickname'=>array(
  439. $nicknameConfig
  440. ),
  441. 'avatar'=>array(
  442. $avatarConfig
  443. ),
  444. 'background'=>$bgImg
  445. );
  446. //海报目标文件
  447. $posterImg = $this->sharePath.md5($url.$type).'-1.png';
  448. //生成海报
  449. if(!file_exists($posterImg))
  450. {
  451. createPoster($config,$posterImg);
  452. }
  453. $posterUrl = APP_URL.'thumb/'.str_replace(DIRECTORY_SEPARATOR,'/',str_replace(THUMB_PATH,'',$posterImg));
  454. return [
  455. 'error' => 0,
  456. 'msg' => '生成成功',
  457. 'data' => ['posterUrl'=>$posterUrl],
  458. 'code'=>200
  459. ];
  460. }
  461. }