Api.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace Poster\Src;
  3. use Dever;
  4. class Api
  5. {
  6. public function test()
  7. {
  8. return Dever::load('poster/api.gets', array('id' => 1));
  9. }
  10. public function gets()
  11. {
  12. $name = "专家介绍专家介绍专家介绍专家介绍专家介绍专家介绍专家介绍专家介绍专家介绍专家介绍专家介绍专家介绍";
  13. $name = Dever::addstr($name, 11, "11");
  14. return '<img src="' .$this->get('1', 'k1', array
  15. (
  16. 'desc' => '吞吞吐吐',
  17. //'pic' => 'http://wx.5dev.cn/wechat/data/upload/applet/code/2018/07/26/69_48f3901dc1d6f839a12967f9b49f5f9c.jpg',
  18. 'name' => $name,
  19. ), 1) . '" />';
  20. }
  21. # 获取同一个key下的多个模板
  22. public function getList($key)
  23. {
  24. $where['key'] = $key;
  25. $template = Dever::db('poster/template')->getAll($where);
  26. return $template;
  27. }
  28. # 获取某个模板
  29. public function get($name, $key, $set, $update = false, $num = 1)
  30. {
  31. if (strstr($key, ',') || is_numeric($key)) {
  32. $where['ids'] = $key;
  33. } else {
  34. $where['key'] = $key;
  35. }
  36. $template = Dever::db('poster/template')->getAll($where);
  37. $total = count($template);
  38. if ($num > $total) {
  39. $num = $total;
  40. }
  41. $key = array_rand($template, 1);
  42. if (is_array($key)) {
  43. $result = array();
  44. foreach ($key as $k => $v) {
  45. $result[] = $this->getTemplate($set, $template, $v, $name, $update);
  46. }
  47. return $result;
  48. } else {
  49. return $this->getTemplate($set, $template, $key, $name, $update);
  50. }
  51. return false;
  52. }
  53. private function getTemplate($set, $template, $key, $name, $update)
  54. {
  55. $template = $template[$key];
  56. if ($template) {
  57. if (!$update) {
  58. $update = $template['update'];
  59. }
  60. $where = array();
  61. $where['template_id'] = $template['id'];
  62. $model = Dever::db('poster/model')->state($where);
  63. if ($model) {
  64. $config = array();
  65. $config['background'] = '';
  66. if (isset($set['width']) && $set['width']) {
  67. $template['width'] = $set['width'];
  68. }
  69. if (isset($set['height']) && $set['height']) {
  70. $template['height'] = $set['height'];
  71. }
  72. if (isset($set['background']) && $set['background']) {
  73. $config['background'] = Dever::local($set['background']);
  74. unset($set['background']);
  75. } elseif ($template['create'] == 2 && $template['background']) {
  76. $config['background'] = Dever::local($template['background']);
  77. } elseif ($template['create'] == 1) {
  78. $config['background'] = array
  79. (
  80. $template['width'], $template['height']
  81. );
  82. }
  83. $config['param'] = array();
  84. foreach ($model as $k => $v) {
  85. $param = $this->getConfig($v, $set);
  86. if ($param) {
  87. array_push($config['param'], $param);
  88. }
  89. }
  90. if ($config['param']) {
  91. $result = Dever::load('poster/lib/img')->create($config, $name, $template['type'], $update);
  92. return Dever::pic($result);
  93. }
  94. } else {
  95. return Dever::pic($template['background']);
  96. }
  97. }
  98. return false;
  99. }
  100. private function getConfig($value, $set)
  101. {
  102. $key = $value['key'];
  103. $param = array();
  104. if (!isset($set[$key])) {
  105. if ($value['type'] == 1 && $value['value_pic']) {
  106. $set[$key] = Dever::local($value['value_pic']);
  107. } else {
  108. $set[$key] = $value['value'];
  109. }
  110. }
  111. if (isset($set[$key])) {
  112. if ($value['position_type'] == 10) {
  113. $position = explode(',', $value['position']);
  114. } else {
  115. $position = $value['position_type'];
  116. }
  117. $offset = explode(',', $value['offset']);
  118. if ($value['type'] == 1) {
  119. $param['method'] = 'mark';
  120. $param['water'] = $set[$key];
  121. $param['position'] = $position;
  122. $param['offset'] = $offset;
  123. $param['width'] = $value['width'];
  124. $param['height'] = $value['height'];
  125. } elseif ($value['type'] == 2) {
  126. $param['method'] = 'txt';
  127. $param['name'] = $set[$key];
  128. $param['color'] = $value['color'];
  129. $param['position'] = $position;
  130. $param['offset'] = $offset;
  131. //$param['top'] = $position[1];
  132. //$param['left'] = $position[2];
  133. $param['size'] = $value['size'];
  134. $param['angle'] = $value['angle'];
  135. if (isset($value['text_width']) && $value['text_width'] > 0) {
  136. $param['width'] = $value['text_width'];
  137. }
  138. $param['font'] = $this->getFont($value['fonts'], 1);
  139. }
  140. }
  141. return $param;
  142. }
  143. # 字体
  144. public function getFont($value, $type = 1)
  145. {
  146. $fonts = array
  147. (
  148. //1 => 'simsun.ttc',
  149. 1 => 'PingFang_Regular.ttf',
  150. 2 => 'PingFang_Bold.ttf',
  151. 3 => 'PingFang_Medium.ttf',
  152. );
  153. if ($type == 1) {
  154. return dirname(__FILE__) . DIRECTORY_SEPARATOR . '../fonts/'.$fonts[$value];
  155. } else {
  156. return dirname(__FILE__) . DIRECTORY_SEPARATOR . '../fonts/'.$fonts[$value];
  157. }
  158. }
  159. }