Image.php 5.3 KB

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