View.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | 查看图片的几个方法
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace Upload\Src;
  8. use Dever;
  9. use Upload\Lib\Store\Handle;
  10. //use Upload\Lib\Lib\Img;
  11. use Dever\Support\Img;
  12. class View
  13. {
  14. public function getFileName()
  15. {
  16. $image['file'] = Dever::input('file');
  17. $image['key'] = Dever::input('key');
  18. //file_put_contents(DEVER_PATH . 'web/data/upload.txt', var_export($image, true));
  19. if ($image['file']) {
  20. $handle = new Handle($image);
  21. $this->output = $handle->name();
  22. }
  23. }
  24. public function manage()
  25. {
  26. $data = array();
  27. $param = Dever::input('param');
  28. $data['key'] = Dever::input('key');
  29. $data['state'] = Dever::input('state');
  30. $data['pic'] = Dever::input('pic');
  31. $data['wh'] = Dever::input('wh');
  32. $data['ratio'] = 16 / 9;
  33. if ($data['wh']) {
  34. if (strstr($data['wh'], '*')) {
  35. $data['ratio'] = $this->ratio('*', $data['wh']);
  36. } elseif (strstr($data['wh'], 'x')) {
  37. $data['ratio'] = $this->ratio('x', $data['wh']);
  38. } elseif (strstr($data['wh'], 'X')) {
  39. $data['ratio'] = $this->ratio('X', $data['wh']);
  40. } else {
  41. $data['ratio'] = 1;
  42. }
  43. }
  44. $data['search_cate'] = 1;
  45. $data['cate'] = Dever::db('upload/cate')->state();
  46. $data['param'] = '';
  47. if (strstr($data['pic'], '.cr_')) {
  48. $temp = explode('.cr_', $data['pic']);
  49. $param = $data['pic'];
  50. $data['pic'] = $temp[0];
  51. }
  52. if (strstr($param, '.cr_')) {
  53. $temp = explode('.cr_', $param);
  54. $data['param'] = str_replace('.jpg', '', $temp[1]);
  55. }
  56. $data = Dever::render('manage', $data);
  57. Dever::out($data);
  58. }
  59. private function ratio($str, $wh)
  60. {
  61. $temp = explode($str, $wh);
  62. $ratio = $temp[0] / $temp[1];
  63. return $ratio;
  64. }
  65. public function files($view)
  66. {
  67. $data = array();
  68. $key = Dever::input('key');
  69. $cate = Dever::input('cate', 1);
  70. $name = Dever::input('name');
  71. $tag = Dever::input('tag');
  72. $state = Dever::input('state', 1);
  73. $upload_search = Dever::input('upload_search', Dever::input('search'));
  74. $data['cur'] = Dever::input('cur');
  75. $data['value'] = Dever::input('value');
  76. $data['search_pg'] = Dever::input('pg', 1);
  77. $param = array();
  78. $param['upload'] = $key;
  79. $param['state'] = $state;
  80. if ($name) {
  81. $param['source_name'] = $name;
  82. $param['name'] = $name;
  83. }
  84. if ($cate && $cate > 0) {
  85. $param['cate'] = $cate;
  86. }
  87. if ($upload_search) {
  88. $param['search'] = $upload_search;
  89. }
  90. $data['search_cate'] = $cate;
  91. $data['search_tag'] = $tag;
  92. $data['search_name'] = $name;
  93. $data['search_name'] = $name;
  94. $data['upload_search'] = $upload_search;
  95. $data['key'] = $key;
  96. if (strstr($key, '_')) {
  97. $temp = explode('_', $key);
  98. if (is_numeric($temp[0]) && $temp[0] > 0) {
  99. $key = $temp[0];
  100. }
  101. }
  102. if (!is_numeric($key)) {
  103. $data += Dever::load($key, $data);
  104. } else {
  105. $data['name'] = '文件名';
  106. $data['file'] = Dever::db('upload/file')->getData($param);
  107. $data['cate'] = Dever::db('upload/cate')->state();
  108. $data['config'] = Dever::db('upload/upload')->one($key);
  109. if ($data['config'] && isset($data['config']['yun']) && $data['config']['yun']) {
  110. $data['yun'] = Dever::db('upload/yun')->one($data['config']['yun']);
  111. }
  112. }
  113. $data['total'] = Dever::total();
  114. if ($data['file']) {
  115. $pic = Dever::config('host')->script . 'img/video.jpg';
  116. foreach ($data['file'] as $k => $v) {
  117. if (!strstr($v['file'], 'http')) {
  118. if (isset($data['config']) && $data['config']['save_type'] >= 3 && isset($data['yun'])) {
  119. $data['file'][$k]['url'] = $data['yun']['host'] . $v['file'];
  120. } elseif (strstr($v['file'], '{uploadRes}')) {
  121. $data['file'][$k]['url'] = Dever::upload($v['file']);
  122. } else {
  123. $data['file'][$k]['url'] = Dever::upload('{uploadRes}' . $v['file']);
  124. }
  125. } else {
  126. $data['file'][$k]['url'] = $v['file'];
  127. }
  128. $data['file'][$k]['value_array'] = array();
  129. if (isset($v['value']) && $v['value']) {
  130. parse_str($v['value'], $data['file'][$k]['value_array']);
  131. }
  132. if (!$view) {
  133. if ($v['state'] == 1) {
  134. $data['file'][$k]['stateColor'] = 'red';
  135. $data['file'][$k]['stateName'] = '删除';
  136. $data['file'][$k]['stateUrl'] = Dever::url('view.setState?id=' . $v['id'] . '&state=2', 'upload');
  137. } else {
  138. $data['file'][$k]['stateColor'] = 'blue';
  139. $data['file'][$k]['stateName'] = '恢复';
  140. $data['file'][$k]['stateUrl'] = Dever::url('view.setState?id=' . $v['id'] . '&state=1', 'upload');
  141. }
  142. $data['file'][$k]['valueUrl'] = Dever::url('view.setValue?id=' . $v['id'], 'upload');
  143. }
  144. $data['file'][$k]['check'] = false;
  145. if ($data['value'] && $data['value'] == $v['id']) {
  146. $data['file'][$k]['check'] = true;
  147. } elseif ($data['cur'] && (isset($v['pic']) && $v['pic'] && strstr($data['cur'], $v['pic']) || isset($v['file']) && $v['file'] && strstr($data['cur'], $v['file']))) {
  148. $data['file'][$k]['check'] = true;
  149. }
  150. if (isset($v['pic']) && $v['pic']) {
  151. $data['file'][$k]['pic'] = $v['pic'];
  152. } elseif (!isset($v['ext'])) {
  153. $data['file'][$k]['pic'] = $pic;
  154. } else {
  155. if ($v['ext'] != '.jpg' && $v['ext'] != '.png' && $v['ext'] != '.bmp' && $v['ext'] != '.gif') {
  156. $data['file'][$k]['pic'] = $pic;
  157. } else {
  158. $data['file'][$k]['pic'] = $data['file'][$k]['url'];
  159. }
  160. }
  161. if (!isset($data['file'][$k]['pic'])) {
  162. $data['file'][$k]['pic'] = $pic;
  163. }
  164. }
  165. }
  166. if ($view) {
  167. return $data;
  168. }
  169. $data = Dever::render('show', $data);
  170. Dever::out($data);
  171. }
  172. public function setState()
  173. {
  174. $id = Dever::input('id');
  175. $state = Dever::input('state');
  176. $where['where_id'] = $id;
  177. $where['state'] = $state;
  178. Dever::db('upload/file')->update($where);
  179. return 'ok';
  180. }
  181. public function setValue()
  182. {
  183. $id = Dever::input('id');
  184. $value = Dever::input('value');
  185. $where['where_id'] = $id;
  186. $where['value'] = $value;
  187. Dever::db('upload/file')->update($where);
  188. return 'ok';
  189. }
  190. # 根据key search 查找文件
  191. public function search($key = 1, $search)
  192. {
  193. $key = Dever::input('key', $key);
  194. $search = Dever::input('search', $search);
  195. Dever::setInput('key', $key);
  196. Dever::setInput('upload_search', $search);
  197. $data = $this->files(true);
  198. return $data['file'];
  199. }
  200. public function kindeditorFile()
  201. {
  202. $key = Dever::input('key');
  203. $param['upload'] = $key;
  204. $file = Dever::db('upload/file')->state();
  205. $config = Dever::db('upload/upload')->one($key);
  206. $list = array();
  207. if ($file) {
  208. $i = 0;
  209. foreach ($file as $k => $v) {
  210. if ($v['name'] && $v['file']) {
  211. $list[$i] = array();
  212. $list[$i]['is_dir'] = false;
  213. $list[$i]['has_file'] = false;
  214. $list[$i]['filesize'] = $v['size'];
  215. $list[$i]['dir_path'] = '';
  216. $list[$i]['is_photo'] = true;
  217. $list[$i]['filetype'] = str_replace('.', '', $v['ext']);
  218. $list[$i]['filename'] = $v['source_name'];
  219. $list[$i]['path'] = '';
  220. $list[$i]['file'] = Dever::upload('{uploadRes}' . $v['file']);
  221. $list[$i]['datetime'] = date('Y-m-d H:i:s', filemtime(Dever::local($list[$i]['file'])));
  222. $i++;
  223. }
  224. }
  225. }
  226. $result = array();
  227. //相对于根目录的上一级目录
  228. $result['moveup_dir_path'] = $config['id'];
  229. //相对于根目录的当前目录
  230. $result['current_dir_path'] = '';
  231. //当前目录的URL
  232. $result['current_url'] = '';
  233. //文件数
  234. $result['total_count'] = count($list);
  235. //文件列表数组
  236. $result['file_list'] = $list;
  237. Dever::outDiy($result);
  238. }
  239. # webp
  240. public function webp($file)
  241. {
  242. $host = Dever::config('host')->uploadRes;
  243. $root = Dever::data() . 'upload/';
  244. $source = str_replace(array('{uploadRes}', $host), $root, $file);
  245. $dest = $source . '.webp';
  246. if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')) {
  247. if (!is_file($dest)) {
  248. Dever::run('cwebp '.$source.' -o ' . $dest . '');
  249. header('Content-type: image/webp');
  250. readfile($source);
  251. } else {
  252. header('Content-type: image/webp');
  253. readfile($dest);
  254. }
  255. } else {
  256. header('Content-type: image/webp');
  257. readfile($source);
  258. }
  259. }
  260. # 获取云端配置
  261. public function getYun($key)
  262. {
  263. $key = Dever::input('key', $key);
  264. $config = Dever::load('upload/lib/config')->get($key);
  265. if ($config && $config['yun']) {
  266. return $config['yun']['host'];
  267. }
  268. return false;
  269. }
  270. # 根据配置生成缩略图 暂时支持缩略图
  271. public function get($file = '')
  272. {
  273. $file = Dever::input('file', $file);
  274. $array = $this->getFile($file);
  275. $source = $array[0];
  276. $type = $array[1];
  277. $id = $array[2];
  278. $host = Dever::config('host')->uploadRes;
  279. $path = $dest = $file;
  280. $handle = true;
  281. $local = false;
  282. if (strstr($file, '{uploadRes}') || strstr($file, $host)) {
  283. # 本地的
  284. $local = true;
  285. $root = Dever::data() . 'upload/';
  286. $source = Dever::pic($source);
  287. $dest = str_replace(array('{uploadRes}', $host), $root, $file);
  288. $path = str_replace($root, '', $dest);
  289. if (strstr($source, $host)) {
  290. $source = $root . str_replace($host, '', $source);
  291. if (!is_file($source)) {
  292. $source = $array[0];
  293. }
  294. }
  295. if ($type == 'wp') {
  296. $temp = explode('.', $dest);
  297. $dest = $temp[0] . '.webp';
  298. $file = str_replace($root, $host, $dest);
  299. }
  300. if (is_file($dest)) {
  301. $file = $dest;
  302. $handle = false;
  303. }
  304. }
  305. if ($handle) {
  306. $temp = explode('/', $path);
  307. $key = $temp[0];
  308. $image['file'] = false;
  309. $image['key'] = intval($key);
  310. $handle = new Handle($image);
  311. if ($type) {
  312. $method = 'handle_' . $type;
  313. $file = $handle->$method($id, $source, $dest, $path);
  314. } elseif (!$local) {
  315. $yun = $handle->yun->getData();
  316. $file = $yun['host'] . $source;
  317. }
  318. }
  319. $file = Dever::uploadRes($file);
  320. return $file;
  321. }
  322. public function source($file)
  323. {
  324. # 搜索文件信息
  325. $array = $this->getFile($file);
  326. return $array[0];
  327. }
  328. private function getFile($file)
  329. {
  330. $type = $id = false;
  331. if (strstr($file, '_wp')) {
  332. $array = explode('_wp', $file);
  333. $type = 'wp';
  334. } elseif (strstr($file, '_p')) {
  335. $array = explode('_p', $file);
  336. $type = 'p';
  337. } elseif (strstr($file, '_c')) {
  338. $array = explode('_c', $file);
  339. $type = 'c';
  340. } elseif (strstr($file, '_t')) {
  341. $array = explode('_t', $file);
  342. $type = 't';
  343. }
  344. if (isset($array)) {
  345. $temp = explode('.', $array[1]);
  346. $id = $temp[0];
  347. $file = $array[0] . '.' . $temp[1];
  348. }
  349. return array($file, $type, $id);
  350. }
  351. /**
  352. * create 生成图片
  353. * @param array $config
  354. * @param string $filename
  355. *
  356. * @return mixed
  357. */
  358. public function create($config = array(), $filename = '', $type = 'gd', $update = 2)
  359. {
  360. $this->img = isset($this->img) ? $this->img : new Img();
  361. /*
  362. $config = array
  363. (
  364. 'background' => '/www/grow/data/upload/1/2018/07/19/a49fe8914df0eada4d4b7d530d7fa5ba.jpg',
  365. 'param' => array
  366. (
  367. 0 => array
  368. (
  369. 'method' => 'mark',
  370. 'water' => '/www/grow/data/upload/1/2018/07/19/46d5b0c5c25c800cf197ea4f64f503f2.jpg',
  371. //left,top
  372. 'position' => array(100,200),
  373. 'width' => 100,
  374. 'height' => 100,
  375. ),
  376. 1 => array
  377. (
  378. 'method' => 'txt',
  379. 'name' => 'test文字',
  380. 'color' => '#FF0000',
  381. 'position' => array(100,200),
  382. 'size' => 50,
  383. 'angle' => 0,
  384. 'font' => '/www/grow/config/fonts/simsun.ttc',
  385. ),
  386. ),
  387. );
  388. */
  389. if (!$filename) {
  390. $filename = Dever::id();
  391. }
  392. $file = Dever::pathDay('upload/poster', false) . md5($filename) . '.jpg';
  393. if (!is_file($file) || $update == 1) {
  394. $this->img->setType($type);
  395. $result = $this->img->init($config['background'], $config['param'], true, $file);
  396. if ($result) {
  397. $file = array_pop($result);
  398. }
  399. } else {
  400. }
  401. $host = str_replace(DEVER_APP_NAME . '/', '', Dever::config('host')->base);
  402. return $host . str_replace(DEVER_PROJECT_PATH, '', $file);
  403. $file = Dever::pic($file);
  404. return $file;
  405. }
  406. public function creates($config = array(), $filename = '')
  407. {
  408. if(empty($filename)) header("content-type: image/png");
  409. $imageDefault = array(
  410. 'left'=>0,
  411. 'top'=>0,
  412. 'right'=>0,
  413. 'bottom'=>0,
  414. 'width'=>100,
  415. 'height'=>100,
  416. 'opacity'=>100
  417. );
  418. $textDefault = array(
  419. 'text'=>'',
  420. 'left'=>0,
  421. 'top'=>0,
  422. 'fontSize'=>32, //字号
  423. 'fontColor'=>'255,255,255', //字体颜色
  424. 'angle'=>0,
  425. );
  426. $background = $config['background'];//海报最底层得背景
  427. //背景方法
  428. $backgroundInfo = getimagesize($background);
  429. $backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false);
  430. $background = $backgroundFun($background);
  431. $backgroundWidth = imagesx($background); //背景宽度
  432. $backgroundHeight = imagesy($background); //背景高度
  433. $imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
  434. $color = imagecolorallocate($imageRes, 0, 0, 0);
  435. imagefill($imageRes, 0, 0, $color);
  436. // imageColorTransparent($imageRes, $color); //颜色透明
  437. imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));
  438. //处理了图片
  439. if (!empty($config['image'])) {
  440. foreach ($config['image'] as $key => $val) {
  441. $val = array_merge($imageDefault,$val);
  442. $info = getimagesize($val['url']);
  443. $function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
  444. if ($val['stream']) { //如果传的是字符串图像流
  445. $info = getimagesizefromstring($val['url']);
  446. $function = 'imagecreatefromstring';
  447. }
  448. if (isset($val['radius']) && $val['radius']) {
  449. $res = $this->radius($val['url'], $val['radius']);
  450. } else {
  451. $res = $function($val['url']);
  452. }
  453. $resWidth = $info[0];
  454. $resHeight = $info[1];
  455. //建立画板 ,缩放图片至指定尺寸
  456. $canvas = imagecreatetruecolor($val['width'], $val['height']);
  457. $color = imagecolorallocate($background, 202, 201, 201); // 为真彩色画布创建白色背景,再设置为透明
  458. imagefill($canvas, 0, 0, $color);
  459. imageColorTransparent($canvas, $color);
  460. //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
  461. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight);
  462. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
  463. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];
  464. //放置图像
  465. imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左,上,右,下,宽度,高度,透明度
  466. }
  467. }
  468. # 处理文字
  469. if (!empty($config['text'])) {
  470. foreach ($config['text'] as $key => $val) {
  471. $val = array_merge($textDefault,$val);
  472. list($R,$G,$B) = explode(',', $val['fontColor']);
  473. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  474. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
  475. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
  476. imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
  477. }
  478. }
  479. # 生成图片
  480. if (!empty($filename)) {
  481. $res = imagepng ($imageRes,$filename,9);
  482. imagedestroy($imageRes);
  483. if(!$res) return false;
  484. return $filename;
  485. } else {
  486. imagepng ($imageRes, null, 9);
  487. imagedestroy($imageRes);
  488. }
  489. }
  490. }