View.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | 查看图片的几个方法
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace Upload\Src;
  8. use Dever;
  9. use Upload\Src\Store\Local as Handle;
  10. use Upload\Src\Lib\Img;
  11. class View
  12. {
  13. public function getFileName()
  14. {
  15. }
  16. public function files()
  17. {
  18. $data = array();
  19. $key = Dever::input('key');
  20. $cate = Dever::input('cate');
  21. $name = Dever::input('name');
  22. $tag = Dever::input('tag');
  23. $data['cur'] = Dever::input('cur');
  24. $data['search_pg'] = Dever::input('pg', 1);
  25. $param = array();
  26. $param['upload'] = $key;
  27. if ($name) {
  28. $param['source_name'] = $name;
  29. $param['name'] = $name;
  30. }
  31. if ($cate && $cate > 0) {
  32. $param['cate'] = $cate;
  33. }
  34. $data['search_cate'] = $cate;
  35. $data['search_tag'] = $tag;
  36. $data['search_name'] = $name;
  37. $data['key'] = $key;
  38. if (strstr($key, '_')) {
  39. $temp = explode('_', $key);
  40. if (is_numeric($temp[0]) && $temp[0] > 0) {
  41. $key = $temp[0];
  42. }
  43. }
  44. if (!is_numeric($key)) {
  45. $data += Dever::load($key);
  46. } else {
  47. $data['name'] = '文件名';
  48. $data['file'] = Dever::db('upload/file')->getData($param);
  49. $data['cate'] = Dever::db('upload/cate')->state();
  50. $data['config'] = Dever::db('upload/upload')->one($key);
  51. if ($data['config'] && isset($data['config']['yun']) && $data['config']['yun']) {
  52. $data['yun'] = Dever::db('upload/yun')->one($data['config']['yun']);
  53. }
  54. }
  55. $data['total'] = Dever::total();
  56. if ($data['file']) {
  57. $pic = Dever::config('host')->script . 'img/video.jpg';
  58. foreach ($data['file'] as $k => $v) {
  59. if (!strstr($v['file'], 'http')) {
  60. if (isset($data['config']) && $data['config']['save_type'] >= 3 && isset($data['yun'])) {
  61. $data['file'][$k]['url'] = $data['yun']['host'] . $v['file'];
  62. } else {
  63. $data['file'][$k]['url'] = Dever::upload('{uploadRes}' . $v['file']);
  64. }
  65. } else {
  66. $data['file'][$k]['url'] = $v['file'];
  67. }
  68. if (isset($v['pic']) && $v['pic']) {
  69. $data['file'][$k]['pic'] = $v['pic'];
  70. } elseif (!isset($v['ext'])) {
  71. $data['file'][$k]['pic'] = $pic;
  72. } else {
  73. if ($v['ext'] != '.jpg' && $v['ext'] != '.png' && $v['ext'] != '.bmp' && $v['ext'] != '.gif') {
  74. $data['file'][$k]['pic'] = $pic;
  75. } else {
  76. $data['file'][$k]['pic'] = $data['file'][$k]['url'];
  77. }
  78. }
  79. if (!isset($data['file'][$k]['pic'])) {
  80. $data['file'][$k]['pic'] = $pic;
  81. }
  82. }
  83. }
  84. $data = Dever::render('show', $data);
  85. Dever::out($data);
  86. }
  87. public function kindeditorFile()
  88. {
  89. $key = Dever::input('key');
  90. $param['upload'] = $key;
  91. $file = Dever::db('upload/file')->state();
  92. $config = Dever::db('upload/upload')->one($key);
  93. $list = array();
  94. if ($file) {
  95. $i = 0;
  96. foreach ($file as $k => $v) {
  97. if ($v['name'] && $v['file']) {
  98. $list[$i] = array();
  99. $list[$i]['is_dir'] = false;
  100. $list[$i]['has_file'] = false;
  101. $list[$i]['filesize'] = $v['size'];
  102. $list[$i]['dir_path'] = '';
  103. $list[$i]['is_photo'] = true;
  104. $list[$i]['filetype'] = str_replace('.', '', $v['ext']);
  105. $list[$i]['filename'] = $v['source_name'];
  106. $list[$i]['path'] = '';
  107. $list[$i]['file'] = Dever::upload('{uploadRes}' . $v['file']);
  108. $list[$i]['datetime'] = date('Y-m-d H:i:s', filemtime(Dever::local($list[$i]['file'])));
  109. $i++;
  110. }
  111. }
  112. }
  113. $result = array();
  114. //相对于根目录的上一级目录
  115. $result['moveup_dir_path'] = $config['id'];
  116. //相对于根目录的当前目录
  117. $result['current_dir_path'] = '';
  118. //当前目录的URL
  119. $result['current_url'] = '';
  120. //文件数
  121. $result['total_count'] = count($list);
  122. //文件列表数组
  123. $result['file_list'] = $list;
  124. Dever::outDiy($result);
  125. }
  126. # webp
  127. public function webp($file)
  128. {
  129. $host = Dever::config('host')->uploadRes;
  130. $root = Dever::data() . 'upload/';
  131. $source = str_replace(array('{uploadRes}', $host), $root, $file);
  132. $dest = $source . '.webp';
  133. if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')) {
  134. if (!is_file($dest)) {
  135. Dever::run('cwebp '.$source.' -o ' . $dest . '');
  136. header('Content-type: image/webp');
  137. readfile($source);
  138. } else {
  139. header('Content-type: image/webp');
  140. readfile($dest);
  141. }
  142. } else {
  143. header('Content-type: image/webp');
  144. readfile($source);
  145. }
  146. }
  147. # 根据配置生成缩略图 暂时支持缩略图
  148. public function get($file)
  149. {
  150. $array = $this->getFile($file);
  151. $source = $array[0];
  152. $type = $array[1];
  153. $id = $array[2];
  154. if ($id > 0) {
  155. $host = Dever::config('host')->uploadRes;
  156. $root = Dever::data() . 'upload/';
  157. $dest = str_replace(array('{uploadRes}', $host), $root, $file);
  158. if (strstr($source, $host)) {
  159. $source = $root . str_replace($host, '', $source);
  160. if (!is_file($source)) {
  161. $source = $array[0];
  162. }
  163. }
  164. if ($type == 'wp') {
  165. $temp = explode('.', $dest);
  166. $dest = $temp[0] . '.webp';
  167. $file = str_replace($root, $host, $dest);
  168. }
  169. if (!strstr($dest, 'http://') && !is_file($dest)) {
  170. $handle = new Handle();
  171. $method = 'handle_' . $type;
  172. $handle->$method($id, $source, $dest);
  173. }
  174. }
  175. return $file;
  176. }
  177. public function source($file)
  178. {
  179. # 搜索文件信息
  180. $array = $this->getFile($file);
  181. return $array[0];
  182. }
  183. private function getFile($file)
  184. {
  185. $type = $id = false;
  186. if (strstr($file, '_wp')) {
  187. $array = explode('_wp', $file);
  188. $type = 'wp';
  189. } elseif (strstr($file, '_p')) {
  190. $array = explode('_p', $file);
  191. $type = 'p';
  192. } elseif (strstr($file, '_c')) {
  193. $array = explode('_c', $file);
  194. $type = 'c';
  195. } elseif (strstr($file, '_t')) {
  196. $array = explode('_t', $file);
  197. $type = 't';
  198. }
  199. if (isset($array)) {
  200. $temp = explode('.', $array[1]);
  201. $id = $temp[0];
  202. $file = $array[0] . '.' . $temp[1];
  203. }
  204. return array($file, $type, $id);
  205. }
  206. /**
  207. * create 生成图片
  208. * @param array $config
  209. * @param string $filename
  210. *
  211. * @return mixed
  212. */
  213. public function create($config = array(), $filename = '', $type = 'gd', $update = 2)
  214. {
  215. $this->img = isset($this->img) ? $this->img : new Img();
  216. /*
  217. $config = array
  218. (
  219. 'background' => '/www/grow/data/upload/1/2018/07/19/a49fe8914df0eada4d4b7d530d7fa5ba.jpg',
  220. 'param' => array
  221. (
  222. 0 => array
  223. (
  224. 'method' => 'mark',
  225. 'water' => '/www/grow/data/upload/1/2018/07/19/46d5b0c5c25c800cf197ea4f64f503f2.jpg',
  226. //left,top
  227. 'position' => array(100,200),
  228. 'width' => 100,
  229. 'height' => 100,
  230. ),
  231. 1 => array
  232. (
  233. 'method' => 'txt',
  234. 'name' => 'test文字',
  235. 'color' => '#FF0000',
  236. 'position' => array(100,200),
  237. 'size' => 50,
  238. 'angle' => 0,
  239. 'font' => '/www/grow/config/fonts/simsun.ttc',
  240. ),
  241. ),
  242. );
  243. */
  244. if (!$filename) {
  245. $filename = Dever::id();
  246. }
  247. $file = Dever::pathDay('upload/poster', false) . md5($filename) . '.jpg';
  248. if (!is_file($file) || $update == 1) {
  249. $this->img->setType($type);
  250. $result = $this->img->init($config['background'], $config['param'], true, $file);
  251. if ($result) {
  252. $file = array_pop($result);
  253. }
  254. } else {
  255. }
  256. $host = str_replace(DEVER_APP_NAME . '/', '', Dever::config('host')->base);
  257. return $host . str_replace(DEVER_PROJECT_PATH, '', $file);
  258. $file = Dever::pic($file);
  259. return $file;
  260. }
  261. public function creates($config = array(), $filename = '')
  262. {
  263. if(empty($filename)) header("content-type: image/png");
  264. $imageDefault = array(
  265. 'left'=>0,
  266. 'top'=>0,
  267. 'right'=>0,
  268. 'bottom'=>0,
  269. 'width'=>100,
  270. 'height'=>100,
  271. 'opacity'=>100
  272. );
  273. $textDefault = array(
  274. 'text'=>'',
  275. 'left'=>0,
  276. 'top'=>0,
  277. 'fontSize'=>32, //字号
  278. 'fontColor'=>'255,255,255', //字体颜色
  279. 'angle'=>0,
  280. );
  281. $background = $config['background'];//海报最底层得背景
  282. //背景方法
  283. $backgroundInfo = getimagesize($background);
  284. $backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false);
  285. $background = $backgroundFun($background);
  286. $backgroundWidth = imagesx($background); //背景宽度
  287. $backgroundHeight = imagesy($background); //背景高度
  288. $imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
  289. $color = imagecolorallocate($imageRes, 0, 0, 0);
  290. imagefill($imageRes, 0, 0, $color);
  291. // imageColorTransparent($imageRes, $color); //颜色透明
  292. imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));
  293. //处理了图片
  294. if (!empty($config['image'])) {
  295. foreach ($config['image'] as $key => $val) {
  296. $val = array_merge($imageDefault,$val);
  297. $info = getimagesize($val['url']);
  298. $function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
  299. if ($val['stream']) { //如果传的是字符串图像流
  300. $info = getimagesizefromstring($val['url']);
  301. $function = 'imagecreatefromstring';
  302. }
  303. if (isset($val['radius']) && $val['radius']) {
  304. $res = $this->radius($val['url'], $val['radius']);
  305. } else {
  306. $res = $function($val['url']);
  307. }
  308. $resWidth = $info[0];
  309. $resHeight = $info[1];
  310. //建立画板 ,缩放图片至指定尺寸
  311. $canvas = imagecreatetruecolor($val['width'], $val['height']);
  312. $color = imagecolorallocate($background, 202, 201, 201); // 为真彩色画布创建白色背景,再设置为透明
  313. imagefill($canvas, 0, 0, $color);
  314. imageColorTransparent($canvas, $color);
  315. //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
  316. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight);
  317. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
  318. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];
  319. //放置图像
  320. imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左,上,右,下,宽度,高度,透明度
  321. }
  322. }
  323. # 处理文字
  324. if (!empty($config['text'])) {
  325. foreach ($config['text'] as $key => $val) {
  326. $val = array_merge($textDefault,$val);
  327. list($R,$G,$B) = explode(',', $val['fontColor']);
  328. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  329. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
  330. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
  331. imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
  332. }
  333. }
  334. # 生成图片
  335. if (!empty($filename)) {
  336. $res = imagepng ($imageRes,$filename,9);
  337. imagedestroy($imageRes);
  338. if(!$res) return false;
  339. return $filename;
  340. } else {
  341. imagepng ($imageRes, null, 9);
  342. imagedestroy($imageRes);
  343. }
  344. }
  345. }