View.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. # 视频截图
  173. public function videoCover()
  174. {
  175. $data = array();
  176. $data['key'] = Dever::input('key');
  177. $data['cur'] = Dever::input('cur');
  178. $data['cover'] = Dever::input('cover');
  179. $data['cover_pic'] = Dever::load('upload/yun')->cover($data['key'], $data['cur']);
  180. $data['video'] = Dever::url('view.video?video=' . Dever::encode($data['cur']), 'upload');
  181. $data['cover_pic_url'] = Dever::url('view.coverPic?key='.$data['key'].'&video=' . Dever::encode($data['cur']), 'upload');
  182. $data = Dever::render('cover', $data);
  183. Dever::out($data);
  184. }
  185. # 展示视频
  186. public function video()
  187. {
  188. $video = Dever::decode(Dever::input('video'));
  189. return '<video src="'.$video.'" height="200" controls="true"/>';
  190. }
  191. # 展示视频截图
  192. public function coverPic()
  193. {
  194. $num = Dever::input('num');
  195. $key = Dever::input('key');
  196. $video = Dever::decode(Dever::input('video'));
  197. $pic = Dever::load('upload/yun')->cover($key, $video, $num);
  198. return $pic;
  199. }
  200. public function setState()
  201. {
  202. $id = Dever::input('id');
  203. $state = Dever::input('state');
  204. $where['where_id'] = $id;
  205. $where['state'] = $state;
  206. Dever::db('upload/file')->update($where);
  207. return 'ok';
  208. }
  209. public function setValue()
  210. {
  211. $id = Dever::input('id');
  212. $value = Dever::input('value');
  213. $where['where_id'] = $id;
  214. $where['value'] = $value;
  215. Dever::db('upload/file')->update($where);
  216. return 'ok';
  217. }
  218. # 根据key search 查找文件
  219. public function search($key = 1, $search)
  220. {
  221. $key = Dever::input('key', $key);
  222. $search = Dever::input('search', $search);
  223. Dever::setInput('key', $key);
  224. Dever::setInput('upload_search', $search);
  225. $data = $this->files(true);
  226. return $data['file'];
  227. }
  228. public function kindeditorFile()
  229. {
  230. $key = Dever::input('key');
  231. $param['upload'] = $key;
  232. $file = Dever::db('upload/file')->state();
  233. $config = Dever::db('upload/upload')->one($key);
  234. $list = array();
  235. if ($file) {
  236. $i = 0;
  237. foreach ($file as $k => $v) {
  238. if ($v['name'] && $v['file']) {
  239. $list[$i] = array();
  240. $list[$i]['is_dir'] = false;
  241. $list[$i]['has_file'] = false;
  242. $list[$i]['filesize'] = $v['size'];
  243. $list[$i]['dir_path'] = '';
  244. $list[$i]['is_photo'] = true;
  245. $list[$i]['filetype'] = str_replace('.', '', $v['ext']);
  246. $list[$i]['filename'] = $v['source_name'];
  247. $list[$i]['path'] = '';
  248. $list[$i]['file'] = Dever::upload('{uploadRes}' . $v['file']);
  249. $list[$i]['datetime'] = date('Y-m-d H:i:s', filemtime(Dever::local($list[$i]['file'])));
  250. $i++;
  251. }
  252. }
  253. }
  254. $result = array();
  255. //相对于根目录的上一级目录
  256. $result['moveup_dir_path'] = $config['id'];
  257. //相对于根目录的当前目录
  258. $result['current_dir_path'] = '';
  259. //当前目录的URL
  260. $result['current_url'] = '';
  261. //文件数
  262. $result['total_count'] = count($list);
  263. //文件列表数组
  264. $result['file_list'] = $list;
  265. Dever::outDiy($result);
  266. }
  267. # webp
  268. public function webp($file)
  269. {
  270. $host = Dever::config('host')->uploadRes;
  271. $root = Dever::data() . 'upload/';
  272. $source = str_replace(array('{uploadRes}', $host), $root, $file);
  273. $dest = $source . '.webp';
  274. if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')) {
  275. if (!is_file($dest)) {
  276. Dever::run('cwebp '.$source.' -o ' . $dest . '');
  277. header('Content-type: image/webp');
  278. readfile($source);
  279. } else {
  280. header('Content-type: image/webp');
  281. readfile($dest);
  282. }
  283. } else {
  284. header('Content-type: image/webp');
  285. readfile($source);
  286. }
  287. }
  288. # 获取云端配置
  289. public function getYun($key)
  290. {
  291. $key = Dever::input('key', $key);
  292. $config = Dever::load('upload/lib/config')->get($key);
  293. if ($config && $config['yun']) {
  294. return $config['yun']['host'];
  295. }
  296. return false;
  297. }
  298. # 根据配置生成缩略图、裁切图
  299. public function get($file = '', $type = '', $id = '')
  300. {
  301. $file = Dever::input('file', $file);
  302. $array = $this->getFile($file, $type, $id);
  303. $source = $array[0];
  304. $type = $array[1];
  305. $id = $array[2];
  306. $host = Dever::config('host')->uploadRes;
  307. $path = $dest = $file;
  308. $handle = true;
  309. $local = false;
  310. $image = array();
  311. if (strstr($file, '{uploadRes}') || strstr($file, $host)) {
  312. # 本地的
  313. $local = true;
  314. $root = Dever::data() . 'upload/';
  315. $source = Dever::pic($source);
  316. $dest = str_replace(array('{uploadRes}', $host), $root, $file);
  317. $path = str_replace($root, '', $dest);
  318. if (strstr($source, $host)) {
  319. $source = $root . str_replace($host, '', $source);
  320. if (!is_file($source)) {
  321. $source = $array[0];
  322. }
  323. }
  324. if ($type == 'wp') {
  325. $temp = explode('.', $dest);
  326. $dest = $temp[0] . '.webp';
  327. $file = str_replace($root, $host, $dest);
  328. }
  329. if (is_file($dest)) {
  330. $file = $dest;
  331. $handle = false;
  332. }
  333. } elseif (strstr($file, 'http')) {
  334. $data = parse_url($file);
  335. $path = ltrim($data['path'], '/');
  336. $host = $data['scheme'] . '://' . $data['host'] . '/';
  337. $image['yun'] = Dever::db('upload/yun')->one(array('host' => $host));
  338. }
  339. if ($handle) {
  340. $temp = explode('/', $path);
  341. $key = $temp[0];
  342. $image['file'] = false;
  343. $image['key'] = intval($key);
  344. $handle = new Handle($image);
  345. if ($type) {
  346. $method = 'handle_' . $type;
  347. $file = $handle->$method($id, $source, $dest, $path);
  348. } elseif (!$local && $handle->yun) {
  349. $yun = $handle->yun->getData();
  350. $file = $yun['host'] . $source;
  351. }
  352. }
  353. $file = Dever::uploadRes($file);
  354. return $file;
  355. }
  356. public function source($file)
  357. {
  358. # 搜索文件信息
  359. $array = $this->getFile($file);
  360. return $array[0];
  361. }
  362. private function getFile($file, $type = false, $id = false)
  363. {
  364. if (strstr($file, '_wp')) {
  365. $array = explode('_wp', $file);
  366. $type = 'wp';
  367. } elseif (strstr($file, '_p')) {
  368. $array = explode('_p', $file);
  369. $type = 'p';
  370. } elseif (strstr($file, '_c')) {
  371. $array = explode('_c', $file);
  372. $type = 'c';
  373. } elseif (strstr($file, '_t')) {
  374. $array = explode('_t', $file);
  375. $type = 't';
  376. }
  377. if (isset($array)) {
  378. $temp = explode('.', $array[1]);
  379. $id = $temp[0];
  380. $file = $array[0] . '.' . $temp[1];
  381. }
  382. return array($file, $type, $id);
  383. }
  384. /**
  385. * create 生成图片
  386. * @param array $config
  387. * @param string $filename
  388. *
  389. * @return mixed
  390. */
  391. public function create($config = array(), $filename = '', $type = 'gd', $update = 2)
  392. {
  393. $this->img = isset($this->img) ? $this->img : new Img();
  394. /*
  395. $config = array
  396. (
  397. 'background' => '/www/grow/data/upload/1/2018/07/19/a49fe8914df0eada4d4b7d530d7fa5ba.jpg',
  398. 'param' => array
  399. (
  400. 0 => array
  401. (
  402. 'method' => 'mark',
  403. 'water' => '/www/grow/data/upload/1/2018/07/19/46d5b0c5c25c800cf197ea4f64f503f2.jpg',
  404. //left,top
  405. 'position' => array(100,200),
  406. 'width' => 100,
  407. 'height' => 100,
  408. ),
  409. 1 => array
  410. (
  411. 'method' => 'txt',
  412. 'name' => 'test文字',
  413. 'color' => '#FF0000',
  414. 'position' => array(100,200),
  415. 'size' => 50,
  416. 'angle' => 0,
  417. 'font' => '/www/grow/config/fonts/simsun.ttc',
  418. ),
  419. ),
  420. );
  421. */
  422. if (!$filename) {
  423. $filename = Dever::id();
  424. }
  425. $file = Dever::pathDay('upload/poster', false) . md5($filename) . '.jpg';
  426. if (!is_file($file) || $update == 1) {
  427. $this->img->setType($type);
  428. $result = $this->img->init($config['background'], $config['param'], true, $file);
  429. if ($result) {
  430. $file = array_pop($result);
  431. }
  432. } else {
  433. }
  434. $host = str_replace(DEVER_APP_NAME . '/', '', Dever::config('host')->base);
  435. return $host . str_replace(DEVER_PROJECT_PATH, '', $file);
  436. $file = Dever::pic($file);
  437. return $file;
  438. }
  439. public function creates($config = array(), $filename = '')
  440. {
  441. if(empty($filename)) header("content-type: image/png");
  442. $imageDefault = array(
  443. 'left'=>0,
  444. 'top'=>0,
  445. 'right'=>0,
  446. 'bottom'=>0,
  447. 'width'=>100,
  448. 'height'=>100,
  449. 'opacity'=>100
  450. );
  451. $textDefault = array(
  452. 'text'=>'',
  453. 'left'=>0,
  454. 'top'=>0,
  455. 'fontSize'=>32, //字号
  456. 'fontColor'=>'255,255,255', //字体颜色
  457. 'angle'=>0,
  458. );
  459. $background = $config['background'];//海报最底层得背景
  460. //背景方法
  461. $backgroundInfo = getimagesize($background);
  462. $backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false);
  463. $background = $backgroundFun($background);
  464. $backgroundWidth = imagesx($background); //背景宽度
  465. $backgroundHeight = imagesy($background); //背景高度
  466. $imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
  467. $color = imagecolorallocate($imageRes, 0, 0, 0);
  468. imagefill($imageRes, 0, 0, $color);
  469. // imageColorTransparent($imageRes, $color); //颜色透明
  470. imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));
  471. //处理了图片
  472. if (!empty($config['image'])) {
  473. foreach ($config['image'] as $key => $val) {
  474. $val = array_merge($imageDefault,$val);
  475. $info = getimagesize($val['url']);
  476. $function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
  477. if ($val['stream']) { //如果传的是字符串图像流
  478. $info = getimagesizefromstring($val['url']);
  479. $function = 'imagecreatefromstring';
  480. }
  481. if (isset($val['radius']) && $val['radius']) {
  482. $res = $this->radius($val['url'], $val['radius']);
  483. } else {
  484. $res = $function($val['url']);
  485. }
  486. $resWidth = $info[0];
  487. $resHeight = $info[1];
  488. //建立画板 ,缩放图片至指定尺寸
  489. $canvas = imagecreatetruecolor($val['width'], $val['height']);
  490. $color = imagecolorallocate($background, 202, 201, 201); // 为真彩色画布创建白色背景,再设置为透明
  491. imagefill($canvas, 0, 0, $color);
  492. imageColorTransparent($canvas, $color);
  493. //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
  494. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight);
  495. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
  496. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];
  497. //放置图像
  498. imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左,上,右,下,宽度,高度,透明度
  499. }
  500. }
  501. # 处理文字
  502. if (!empty($config['text'])) {
  503. foreach ($config['text'] as $key => $val) {
  504. $val = array_merge($textDefault,$val);
  505. list($R,$G,$B) = explode(',', $val['fontColor']);
  506. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  507. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
  508. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
  509. imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
  510. }
  511. }
  512. # 生成图片
  513. if (!empty($filename)) {
  514. $res = imagepng ($imageRes,$filename,9);
  515. imagedestroy($imageRes);
  516. if(!$res) return false;
  517. return $filename;
  518. } else {
  519. imagepng ($imageRes, null, 9);
  520. imagedestroy($imageRes);
  521. }
  522. }
  523. }