PrintSMT.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\expand\controller;
  3. use app\common\service\HelperService;
  4. /**
  5. * 打印扫码贴
  6. * Class Baidu
  7. * @package app\expand\controller
  8. */
  9. class PrintSMT extends BaseAuth
  10. {
  11. private $_smtAccount = [];
  12. public function __construct(){
  13. parent::__construct();
  14. $this->_smtAccount = $this->getKey($this->_apiCode);
  15. //验证是否具有访问这个接口的权限
  16. if( !isset($this->_smtAccount['printSMT']) ){
  17. HelperService::returnJson(['code'=>400,'msg'=>'smt interface unauthorized access','data'=>[]]);
  18. }
  19. }
  20. /**
  21. * 打印关西店
  22. */
  23. public function mu(){
  24. $muPath = "static/image/mu";
  25. $fileList = scandir($muPath);
  26. foreach($fileList as $fileItem){
  27. if(strpos($fileItem,'.')!==0){
  28. $nameArr = explode("_", $fileItem);
  29. $enName = end($nameArr);
  30. $this->printMu("$muPath/$fileItem", "$muPath.png", "环贸广场店".current(explode(".", $enName)));
  31. //exit;
  32. }
  33. }
  34. }
  35. /**
  36. * 打印关西店
  37. */
  38. public function bear(){
  39. $muPath = "static/image/xiong";
  40. $fileList = scandir($muPath);
  41. foreach($fileList as $fileItem){
  42. if(strpos($fileItem,'.')!==0){
  43. $nameArr = explode("_", $fileItem);
  44. $enName = end($nameArr);
  45. $this->printBear("$muPath/$fileItem", "$muPath.png", "189店".current(explode(".", $enName)));
  46. //exit;
  47. }
  48. }
  49. }
  50. /**
  51. * 打印扫码贴关西
  52. * @param $fileName
  53. * @param $bgName
  54. * @param $storeName
  55. */
  56. private function printMu($fileName,$bgName,$storeName){
  57. //裁剪
  58. $qrImage = stripos($fileName,'jpg')?imagecreatefromjpeg($fileName): imagecreatefrompng($fileName);
  59. $bgImage = stripos($bgName,'jpg')?imagecreatefromjpeg($bgName):imagecreatefrompng($bgName);
  60. $width = imagesx($bgImage);
  61. $newFileName = str_replace('mu/', 'mu2/', $fileName);
  62. //320
  63. imagecopymerge($bgImage, $qrImage, 190, 300, 0, 0, imagesx($qrImage), imagesy($qrImage), 100);
  64. stripos($fileName,'jpg')?imagejpeg($bgImage,$newFileName):imagepng($bgImage,$newFileName);
  65. $new_bgImage = stripos($newFileName,'jpg')?imagecreatefromjpeg($newFileName):imagecreatefrompng($newFileName);
  66. //$white = imagecolorallocate($new_bgImage, 255, 255, 255);
  67. $black = imagecolorallocate($new_bgImage, 0, 0, 0);
  68. $fontBox = imagettfbbox(18, 0, './font/NotoSansHans-DemiLight.ttf', $storeName);//水平居中实质
  69. imagettftext($new_bgImage,18,0,ceil(($width - $fontBox[2]) / 2)+34,536,$black,'./font/NotoSansHans-DemiLight.ttf',$storeName);
  70. imagettftext($new_bgImage,18,0,ceil(($width - $fontBox[2]) / 2)+35,536,$black,'./font/NotoSansHans-DemiLight.ttf',$storeName);
  71. //imagettftext($new_bgImage,24,0,30,30,$black,'./font/NotoSansHans-DemiLight.ttf',$storeName);
  72. //imagettftext($new_bgImage,50,0,430,420,$white,'./NotoSansHans-DemiLight.ttf',$tableName);
  73. stripos($newFileName,'jpg')?imagejpeg($new_bgImage, $newFileName):imagepng($new_bgImage, $newFileName);
  74. //system("rm -rf $tableName.png");
  75. //$this->downloadFile($newFileName);
  76. }
  77. /**
  78. * 打印扫码贴关西
  79. * @param $fileName
  80. * @param $bgName
  81. * @param $storeName
  82. */
  83. private function printBear($fileName,$bgName,$storeName){
  84. //裁剪
  85. $qrImage = stripos($fileName,'jpg')?imagecreatefromjpeg($fileName): imagecreatefrompng($fileName);
  86. $bgImage = stripos($bgName,'jpg')?imagecreatefromjpeg($bgName):imagecreatefrompng($bgName);
  87. $width = imagesx($bgImage);
  88. $newFileName = str_replace('xiong/', 'xiong2/', $fileName);
  89. //320
  90. imagecopymerge($bgImage, $qrImage, 152, 286, 0, 0, imagesx($qrImage), imagesy($qrImage), 100);
  91. stripos($fileName,'jpg')?imagejpeg($bgImage,$newFileName):imagepng($bgImage,$newFileName);
  92. $new_bgImage = stripos($newFileName,'jpg')?imagecreatefromjpeg($newFileName):imagecreatefrompng($newFileName);
  93. //$white = imagecolorallocate($new_bgImage, 255, 255, 255);
  94. $black = imagecolorallocate($new_bgImage, 0, 0, 0);
  95. $fontBox = imagettfbbox(20, 0, './font/NotoSansHans-DemiLight.ttf', $storeName);//水平居中实质
  96. imagettftext($new_bgImage,20,0,ceil(($width - $fontBox[2]) / 2),520,$black,'./font/NotoSansHans-DemiLight.ttf',$storeName);
  97. imagettftext($new_bgImage,20,0,ceil(($width - $fontBox[2]) / 2)+1,520,$black,'./font/NotoSansHans-DemiLight.ttf',$storeName);
  98. //imagettftext($new_bgImage,24,0,30,30,$black,'./font/NotoSansHans-DemiLight.ttf',$storeName);
  99. //imagettftext($new_bgImage,50,0,430,420,$white,'./NotoSansHans-DemiLight.ttf',$tableName);
  100. stripos($newFileName,'jpg')?imagejpeg($new_bgImage, $newFileName):imagepng($new_bgImage, $newFileName);
  101. //system("rm -rf $tableName.png");
  102. //$this->downloadFile($newFileName);
  103. }
  104. private function downloadFile($file){
  105. $file_name = $file;
  106. $mime = 'application/force-download';
  107. header('Pragma: public'); // required
  108. header('Expires: 0'); // no cache
  109. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  110. header('Cache-Control: private',false);
  111. header('Content-Type: '.$mime);
  112. header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
  113. header('Content-Transfer-Encoding: binary');
  114. header('Connection: close');
  115. readfile($file_name); // push it out
  116. exit();
  117. }
  118. }