View.php 14 KB

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