Image.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace app\common\model;
  3. use think\image\Exception;
  4. class Image extends Base {
  5. public function down_load($url,$config,$flag='vod')
  6. {
  7. if(substr($url,0,4)=='http'){
  8. return $this->down_exec($url,$config,$flag);
  9. }
  10. else{
  11. return $url;
  12. }
  13. }
  14. public function down_exec($url,$config,$flag='vod')
  15. {
  16. $upload_image_ext = 'jpg,jpeg,png,gif,webp';
  17. $ext = strtolower(pathinfo($url, PATHINFO_EXTENSION));
  18. if(!in_array($ext, explode(',', $upload_image_ext))){
  19. $ext = 'jpg';
  20. }
  21. $img = mac_curl_get($url);
  22. if($img){
  23. $file_name = md5(uniqid()) .'.' . $ext;
  24. // 上传附件路径
  25. $_upload_path = ROOT_PATH . 'upload' . '/' . $flag . '/';
  26. // 附件访问路径
  27. $_save_path = 'upload'. '/' . $flag . '/' ;
  28. $ymd = date('Ymd');
  29. $n_dir = $ymd;
  30. for($i=1;$i<=100;$i++){
  31. $n_dir = $ymd .'-'.$i;
  32. $path1 = $_upload_path . $n_dir. '/';
  33. if(file_exists($path1)){
  34. $farr = glob($path1.'*.*');
  35. if($farr){
  36. $fcount = count($farr);
  37. if($fcount>999){
  38. continue;
  39. }
  40. else{
  41. break;
  42. }
  43. }
  44. else{
  45. break;
  46. }
  47. }
  48. else{
  49. break;
  50. }
  51. }
  52. $_upload_path .= $n_dir . '/';
  53. $_save_path .= $n_dir . '/';
  54. //附件访问地址
  55. $_file_path = $_save_path.$file_name;
  56. //写入文件
  57. $r = mac_write_file($_upload_path.$file_name,$img);
  58. if(!$r){
  59. return $url;
  60. }
  61. $file_size = filesize($_upload_path.$file_name);
  62. // 水印
  63. if ($config['watermark'] == 1) {
  64. $this->watermark($_file_path,$config,$flag);
  65. }
  66. // 缩略图
  67. if ($config['thumb'] == 1) {
  68. $this->makethumb($_file_path,$config,$flag);
  69. }
  70. //上传到远程
  71. $_file_path = model('Upload')->api($_file_path,$config);
  72. $tmp = $_file_path;
  73. if((substr($tmp,0,7) == "/upload")){
  74. $tmp = substr($tmp,1);
  75. }
  76. if((substr($tmp,0,6) == "upload")){
  77. $annex = [];
  78. $annex['annex_file'] = $tmp;
  79. $annex['annex_type'] = 'image';
  80. $annex['annex_size'] = $file_size;
  81. model('Annex')->saveData($annex);
  82. }
  83. return $_file_path;
  84. }
  85. else{
  86. return $url;
  87. }
  88. }
  89. public function watermark($file_path,$config,$flag='vod')
  90. {
  91. if(empty($config['watermark_font'])){
  92. $config['watermark_font'] = './static/font/test.ttf';
  93. }
  94. try {
  95. $image = \think\Image::open('./' . $file_path);
  96. $image->text($config['watermark_content']."", $config['watermark_font'], $config['watermark_size'], $config['watermark_color'],$config['watermark_location'])->save('./' . $file_path);
  97. }
  98. catch(\Exception $e){
  99. }
  100. }
  101. public function makethumb($file_path,$config,$flag='vod',$new=1)
  102. {
  103. $thumb_type = $config['thumb_type'];
  104. $data['thumb'] = [];
  105. if (!empty($config['thumb_size'])) {
  106. try {
  107. $image = \think\Image::open('./' . $file_path);
  108. // 支持多种尺寸的缩略图
  109. $thumbs = explode(',', $config['thumb_size']);
  110. foreach ($thumbs as $k => $v) {
  111. $t_size = explode('x', strtolower($v));
  112. if (!isset($t_size[1])) {
  113. $t_size[1] = $t_size[0];
  114. }
  115. $new_thumb = $file_path . '_' . $t_size[0] . 'x' . $t_size[1] . '.' . strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
  116. if($new==0){
  117. $new_thumb = $file_path;
  118. }
  119. $image->thumb($t_size[0], $t_size[1], $thumb_type)->save('./' . $new_thumb);
  120. $thumb_size = round(filesize('./' . $new_thumb) / 1024, 2);
  121. $data['thumb'][$k]['type'] = 'image';
  122. $data['thumb'][$k]['flag'] = $flag;
  123. $data['thumb'][$k]['file'] = $new_thumb;
  124. $data['thumb'][$k]['size'] = $thumb_size;
  125. $data['thumb'][$k]['ctime'] = request()->time();
  126. if ($config['watermark'] == 1) {// 开启文字水印
  127. $image = \think\Image::open('./' . $new_thumb);
  128. $image->text($config['watermark_content'], $config['watermark_font'], $config['watermark_size'], $config['watermark_color'])->save('./' . $new_thumb);
  129. }
  130. }
  131. }
  132. catch(\Exception $e){
  133. }
  134. }
  135. return $data;
  136. }
  137. }