ImageService.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2019/1/22
  6. * Time: 下午8:27
  7. */
  8. namespace app\common\service;
  9. use app\common\constants\AppConstants;
  10. use app\common\constants\ErrorCodeConstants;
  11. use app\common\helper\ArrayHelper;
  12. use app\common\library\Redis;
  13. use Endroid\QrCode\QrCode;
  14. use think\Config;
  15. class ImageService extends BaseService
  16. {
  17. /**
  18. * @var ImageService
  19. */
  20. private static $self;
  21. /**
  22. * @return $this|ImageService
  23. */
  24. public static function instance()
  25. {
  26. if (self::$self == NULL) {
  27. self::$self = new self();
  28. }
  29. return self::$self;
  30. }
  31. /**
  32. * 获取二维码
  33. * @param $url
  34. * @param $width
  35. * @param int $margin
  36. * @param string $path_dir
  37. * @return \app\common\model\object\ReturnObject
  38. */
  39. public function getQrImage($url, $width = '164', $margin = 10, $path_dir = '')
  40. {
  41. try {
  42. $qrCode = new QrCode($url);
  43. $qrCode->setSize($width); //二维码尺寸
  44. $qrCode->setWriterByName('png');
  45. $qrCode->setEncoding('UTF-8');
  46. $qrCode->setMargin($margin);
  47. $tmpFile = AppConstants::getTmpFilePath($path_dir);
  48. $qrCode->writeFile($tmpFile);
  49. return $this->setData($tmpFile)->getReturn();
  50. } catch (\Exception $e) {
  51. return $this->setCode(ErrorCodeConstants::EXCEPTION)->setMsg($e->getMessage())->getReturn();
  52. }
  53. }
  54. /**
  55. * @param $width
  56. * @param $height
  57. * @return \app\common\model\object\ReturnObject
  58. */
  59. public function getBackGroundImage($width, $height)
  60. {
  61. try {
  62. $image = imagecreatetruecolor($width, $height);
  63. $background = imagecolorallocate($image, 255, 255, 255);
  64. imagefill($image,0,0,$background);
  65. return $this->setData($image)->getReturn();
  66. } catch (\Exception $e) {
  67. return $this->setCode(ErrorCodeConstants::EXCEPTION)->setMsg($e->getMessage())->getReturn();
  68. }
  69. }
  70. public function setImageText($image, $content, $font_size = 5, $x = 0, $y = 0)
  71. {
  72. try {
  73. $color = imagecolorallocate($image, 255, 0, 0);
  74. $font_file = ROOT_PATH . 'public/assets/fonts/ht.ttf';
  75. imagettftext($image, $font_size, 0, $x, $y, $color, $font_file, $content);
  76. return $this->setData($image)->getReturn();
  77. } catch (\Exception $e) {
  78. return $this->setCode(ErrorCodeConstants::EXCEPTION)->setMsg($e->getMessage())->getReturn();
  79. }
  80. }
  81. /**
  82. * 获取文字行
  83. * @param $line
  84. * @return \app\common\model\object\ReturnObject
  85. */
  86. public function getLine($line)
  87. {
  88. $list = [];
  89. if (mb_strlen($line) > 16) {
  90. $list[] = mb_substr($line, 0, 16);
  91. $left = mb_substr($line, 16);
  92. if (mb_strlen($left) > 16) {
  93. $list[] = mb_substr($left, 0, 10) . "....";
  94. } else {
  95. $list[] = $left;
  96. }
  97. } else {
  98. $list[] = $line;
  99. }
  100. return $this->setData($list)->getReturn();
  101. }
  102. /**
  103. * 获取推广链接图
  104. * @param $id
  105. * @param $channel_id
  106. * @return \app\common\model\object\ReturnObject
  107. */
  108. public function getReferralImage($id, $channel_id)
  109. {
  110. $referral = ReferralService::instance()->getReferralModel()->where('id', '=', $id)->find();
  111. if (!$referral) {
  112. return $this->setCode(ErrorCodeConstants::PERMISSION_DENY)->getReturn();
  113. }
  114. $cache = 'QRIMAGE:' . $id;
  115. if ($path = Redis::instance()->get($cache)) {
  116. return $this->setData($path)->getReturn();
  117. }
  118. $jmp_url = getCurrentDomain($channel_id, '/t/' . $id);
  119. $mergeImage = ImageService::instance()->getMergeImage($jmp_url, $referral->getData("share_title"));
  120. if ($mergeImage->code == ErrorCodeConstants::SUCCESS) {
  121. Redis::instance()->set($cache, $mergeImage->data, 600);
  122. }
  123. return $mergeImage;
  124. }
  125. /**
  126. * 生成大图
  127. * @param $url
  128. * @param $content
  129. * @return \app\common\model\object\ReturnObject
  130. */
  131. public function getMergeImage($url, $content)
  132. {
  133. $returnQrcode = ImageService::instance()->getQrImage($url, 110, 0);
  134. $qrImage = imagecreatefrompng($returnQrcode->data);
  135. $cover = ResourceService::instance()->getRandomCover();
  136. $filename = $cover->data;
  137. $fileInfo = pathinfo($filename);
  138. $extension = ArrayHelper::arrayGet($fileInfo, 'extension');
  139. switch ($extension) {
  140. case 'png':
  141. $coverImage = imagecreatefrompng($filename);
  142. break;
  143. case 'jpg':
  144. case 'jpeg':
  145. $coverImage = imagecreatefromjpeg($filename);
  146. break;
  147. default:
  148. return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg("只支持JPEG,PNG图片")->getReturn();
  149. }
  150. list($width, $height) = getimagesize($filename);
  151. $return = ImageService::instance()->getBackGroundImage(500, 469);
  152. $bgImage = $return->data;
  153. imagecopyresampled($bgImage, $qrImage, 6, 346, 0, 0, 110, 110, 110, 110);
  154. $color = imagecolorallocate($bgImage, 255, 0, 0);
  155. $font_file = ROOT_PATH . 'public/assets/fonts/weiruanyahei.ttf';
  156. $lines = $this->getLine($content)->data;
  157. foreach ($lines as $index => $line) {
  158. $offsetY = 365 + $index * 30;
  159. imagettftext($bgImage, 16, 0, 131, $offsetY, $color, $font_file, $line);
  160. }
  161. $color = imagecolorallocate($bgImage, 121, 121, 121);
  162. imagettftext($bgImage, 13, 0, 131, 445, $color, $font_file, "长按识别二维码查看精彩内容");
  163. imagecopyresampled($bgImage, $coverImage, 0, 0, 0, 0, 500, 313, $width, $height);
  164. $uploadPath = AppConstants::getUploadQrPath();
  165. imagepng($bgImage, $uploadPath, 9);
  166. imagedestroy($bgImage);
  167. imagedestroy($qrImage);
  168. imagedestroy($coverImage);
  169. $url = AppConstants::getPublicUrl($uploadPath);
  170. return $this->setData($url)->getReturn();
  171. }
  172. }