123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace Poster\Src;
- use Dever;
- class Api
- {
- public function test()
- {
- return Dever::load('poster/api.gets', array('id' => 1));
- }
- public function gets()
- {
- $name = "专家介绍专家介绍专家介绍专家介绍专家介绍专家介绍专家介绍专家介绍专家介绍专家介绍专家介绍专家介绍";
- $name = Dever::addstr($name, 11, "11");
- return '<img src="' .$this->get('1', 'k1', array
- (
- 'desc' => '吞吞吐吐',
- //'pic' => 'http://wx.5dev.cn/wechat/data/upload/applet/code/2018/07/26/69_48f3901dc1d6f839a12967f9b49f5f9c.jpg',
- 'name' => $name,
- ), 1) . '" />';
- }
- # 获取同一个key下的多个模板
- public function getList($key)
- {
- $where['key'] = $key;
- $template = Dever::db('poster/template')->getAll($where);
- return $template;
- }
- # 获取某个模板
- public function get($name, $key, $set, $update = false, $num = 1)
- {
- if (strstr($key, ',') || is_numeric($key)) {
- $where['ids'] = $key;
- } else {
- $where['key'] = $key;
- }
- $template = Dever::db('poster/template')->getAll($where);
- $total = count($template);
- if ($num > $total) {
- $num = $total;
- }
- $key = array_rand($template, 1);
- if (is_array($key)) {
- $result = array();
- foreach ($key as $k => $v) {
- $result[] = $this->getTemplate($set, $template, $v, $name, $update);
- }
- return $result;
- } else {
- return $this->getTemplate($set, $template, $key, $name, $update);
- }
-
- return false;
- }
- private function getTemplate($set, $template, $key, $name, $update)
- {
- $template = $template[$key];
- if ($template) {
- if (!$update) {
- $update = $template['update'];
- }
- $where = array();
- $where['template_id'] = $template['id'];
- $model = Dever::db('poster/model')->state($where);
- if ($model) {
- $config = array();
- $config['background'] = '';
- if (isset($set['width']) && $set['width']) {
- $template['width'] = $set['width'];
- }
- if (isset($set['height']) && $set['height']) {
- $template['height'] = $set['height'];
- }
-
- if (isset($set['background']) && $set['background']) {
- $config['background'] = Dever::local($set['background']);
- unset($set['background']);
- } elseif ($template['create'] == 2 && $template['background']) {
- $config['background'] = Dever::local($template['background']);
- } elseif ($template['create'] == 1) {
- $config['background'] = array
- (
- $template['width'], $template['height']
- );
- }
-
- $config['param'] = array();
- foreach ($model as $k => $v) {
- $param = $this->getConfig($v, $set);
- if ($param) {
- array_push($config['param'], $param);
- }
- }
- if ($config['param']) {
- $result = Dever::load('poster/lib/img')->create($config, $name, $template['type'], $update);
- return Dever::pic($result);
- }
- } else {
- return Dever::pic($template['background']);
- }
- }
- return false;
- }
- private function getConfig($value, $set)
- {
- $key = $value['key'];
- $param = array();
- if (!isset($set[$key])) {
- if ($value['type'] == 1 && $value['value_pic']) {
- $set[$key] = Dever::local($value['value_pic']);
- } else {
- $set[$key] = $value['value'];
- }
- }
- if (isset($set[$key])) {
- if ($value['position_type'] == 10) {
- $position = explode(',', $value['position']);
- } else {
- $position = $value['position_type'];
- }
- $offset = explode(',', $value['offset']);
-
- if ($value['type'] == 1) {
- $param['method'] = 'mark';
- $param['water'] = $set[$key];
- $param['position'] = $position;
- $param['offset'] = $offset;
- $param['width'] = $value['width'];
- $param['height'] = $value['height'];
- } elseif ($value['type'] == 2) {
- $param['method'] = 'txt';
- $param['name'] = $set[$key];
- $param['color'] = $value['color'];
- $param['position'] = $position;
- $param['offset'] = $offset;
- //$param['top'] = $position[1];
- //$param['left'] = $position[2];
- $param['size'] = $value['size'];
- $param['angle'] = $value['angle'];
- if (isset($value['text_width']) && $value['text_width'] > 0) {
- $param['width'] = $value['text_width'];
- }
- $param['font'] = $this->getFont($value['fonts'], 1);
- }
- }
- return $param;
- }
- # 字体
- public function getFont($value, $type = 1)
- {
- $fonts = array
- (
- //1 => 'simsun.ttc',
- 1 => 'PingFang_Regular.ttf',
- 2 => 'PingFang_Bold.ttf',
- 3 => 'PingFang_Medium.ttf',
- );
- if ($type == 1) {
- return dirname(__FILE__) . DIRECTORY_SEPARATOR . '../fonts/'.$fonts[$value];
- } else {
- return dirname(__FILE__) . DIRECTORY_SEPARATOR . '../fonts/'.$fonts[$value];
- }
- }
- }
|