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->copy();
  21. unset($this->output['file']);
  22. }
  23. }
  24. public function files()
  25. {
  26. $data = array();
  27. $key = Dever::input('key');
  28. $cate = Dever::input('cate');
  29. $name = Dever::input('name');
  30. $tag = Dever::input('tag');
  31. $data['cur'] = Dever::input('cur');
  32. $data['search_pg'] = Dever::input('pg', 1);
  33. $param = array();
  34. $param['upload'] = $key;
  35. if ($name) {
  36. $param['source_name'] = $name;
  37. $param['name'] = $name;
  38. }
  39. if ($cate && $cate > 0) {
  40. $param['cate'] = $cate;
  41. }
  42. $data['search_cate'] = $cate;
  43. $data['search_tag'] = $tag;
  44. $data['search_name'] = $name;
  45. $data['key'] = $key;
  46. if (strstr($key, '_')) {
  47. $temp = explode('_', $key);
  48. if (is_numeric($temp[0]) && $temp[0] > 0) {
  49. $key = $temp[0];
  50. }
  51. }
  52. if (!is_numeric($key)) {
  53. $data += Dever::load($key);
  54. } else {
  55. $data['name'] = '文件名';
  56. $data['file'] = Dever::db('upload/file')->getData($param);
  57. $data['cate'] = Dever::db('upload/cate')->state();
  58. $data['config'] = Dever::db('upload/upload')->one($key);
  59. if ($data['config'] && isset($data['config']['yun']) && $data['config']['yun']) {
  60. $data['yun'] = Dever::db('upload/yun')->one($data['config']['yun']);
  61. }
  62. }
  63. $data['total'] = Dever::total();
  64. if ($data['file']) {
  65. $pic = Dever::config('host')->script . 'img/video.jpg';
  66. foreach ($data['file'] as $k => $v) {
  67. if (!strstr($v['file'], 'http')) {
  68. if (isset($data['config']) && $data['config']['save_type'] >= 3 && isset($data['yun'])) {
  69. $data['file'][$k]['url'] = $data['yun']['host'] . $v['file'];
  70. } else {
  71. $data['file'][$k]['url'] = Dever::upload('{uploadRes}' . $v['file']);
  72. }
  73. } else {
  74. $data['file'][$k]['url'] = $v['file'];
  75. }
  76. if (isset($v['pic']) && $v['pic']) {
  77. $data['file'][$k]['pic'] = $v['pic'];
  78. } elseif (!isset($v['ext'])) {
  79. $data['file'][$k]['pic'] = $pic;
  80. } else {
  81. if ($v['ext'] != '.jpg' && $v['ext'] != '.png' && $v['ext'] != '.bmp' && $v['ext'] != '.gif') {
  82. $data['file'][$k]['pic'] = $pic;
  83. } else {
  84. $data['file'][$k]['pic'] = $data['file'][$k]['url'];
  85. }
  86. }
  87. if (!isset($data['file'][$k]['pic'])) {
  88. $data['file'][$k]['pic'] = $pic;
  89. }
  90. }
  91. }
  92. $data = Dever::render('show', $data);
  93. Dever::out($data);
  94. }
  95. public function kindeditorFile()
  96. {
  97. $key = Dever::input('key');
  98. $param['upload'] = $key;
  99. $file = Dever::db('upload/file')->state();
  100. $config = Dever::db('upload/upload')->one($key);
  101. $list = array();
  102. if ($file) {
  103. $i = 0;
  104. foreach ($file as $k => $v) {
  105. if ($v['name'] && $v['file']) {
  106. $list[$i] = array();
  107. $list[$i]['is_dir'] = false;
  108. $list[$i]['has_file'] = false;
  109. $list[$i]['filesize'] = $v['size'];
  110. $list[$i]['dir_path'] = '';
  111. $list[$i]['is_photo'] = true;
  112. $list[$i]['filetype'] = str_replace('.', '', $v['ext']);
  113. $list[$i]['filename'] = $v['source_name'];
  114. $list[$i]['path'] = '';
  115. $list[$i]['file'] = Dever::upload('{uploadRes}' . $v['file']);
  116. $list[$i]['datetime'] = date('Y-m-d H:i:s', filemtime(Dever::local($list[$i]['file'])));
  117. $i++;
  118. }
  119. }
  120. }
  121. $result = array();
  122. //相对于根目录的上一级目录
  123. $result['moveup_dir_path'] = $config['id'];
  124. //相对于根目录的当前目录
  125. $result['current_dir_path'] = '';
  126. //当前目录的URL
  127. $result['current_url'] = '';
  128. //文件数
  129. $result['total_count'] = count($list);
  130. //文件列表数组
  131. $result['file_list'] = $list;
  132. Dever::outDiy($result);
  133. }
  134. # webp
  135. public function webp($file)
  136. {
  137. $host = Dever::config('host')->uploadRes;
  138. $root = Dever::data() . 'upload/';
  139. $source = str_replace(array('{uploadRes}', $host), $root, $file);
  140. $dest = $source . '.webp';
  141. if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')) {
  142. if (!is_file($dest)) {
  143. Dever::run('cwebp '.$source.' -o ' . $dest . '');
  144. header('Content-type: image/webp');
  145. readfile($source);
  146. } else {
  147. header('Content-type: image/webp');
  148. readfile($dest);
  149. }
  150. } else {
  151. header('Content-type: image/webp');
  152. readfile($source);
  153. }
  154. }
  155. # 根据配置生成缩略图 暂时支持缩略图
  156. public function get($file)
  157. {
  158. $array = $this->getFile($file);
  159. $source = $array[0];
  160. $type = $array[1];
  161. $id = $array[2];
  162. if ($id > 0) {
  163. $host = Dever::config('host')->uploadRes;
  164. $root = Dever::data() . 'upload/';
  165. $dest = str_replace(array('{uploadRes}', $host), $root, $file);
  166. if (strstr($source, $host)) {
  167. $source = $root . str_replace($host, '', $source);
  168. if (!is_file($source)) {
  169. $source = $array[0];
  170. }
  171. }
  172. if ($type == 'wp') {
  173. $temp = explode('.', $dest);
  174. $dest = $temp[0] . '.webp';
  175. $file = str_replace($root, $host, $dest);
  176. }
  177. if (!strstr($dest, 'http://') && !is_file($dest)) {
  178. $handle = new Handle();
  179. $method = 'handle_' . $type;
  180. $handle->$method($id, $source, $dest);
  181. }
  182. }
  183. return $file;
  184. }
  185. public function source($file)
  186. {
  187. # 搜索文件信息
  188. $array = $this->getFile($file);
  189. return $array[0];
  190. }
  191. private function getFile($file)
  192. {
  193. $type = $id = false;
  194. if (strstr($file, '_wp')) {
  195. $array = explode('_wp', $file);
  196. $type = 'wp';
  197. } elseif (strstr($file, '_p')) {
  198. $array = explode('_p', $file);
  199. $type = 'p';
  200. } elseif (strstr($file, '_c')) {
  201. $array = explode('_c', $file);
  202. $type = 'c';
  203. } elseif (strstr($file, '_t')) {
  204. $array = explode('_t', $file);
  205. $type = 't';
  206. }
  207. if (isset($array)) {
  208. $temp = explode('.', $array[1]);
  209. $id = $temp[0];
  210. $file = $array[0] . '.' . $temp[1];
  211. }
  212. return array($file, $type, $id);
  213. }
  214. /**
  215. * create 生成图片
  216. * @param array $config
  217. * @param string $filename
  218. *
  219. * @return mixed
  220. */
  221. public function create($config = array(), $filename = '', $type = 'gd', $update = 2)
  222. {
  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. }