DownloadImageService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use think\exception\ValidateException;
  13. use think\Image;
  14. class DownloadImageService
  15. {
  16. //是否生成缩略图
  17. protected $thumb = false;
  18. //缩略图宽度
  19. protected $thumbWidth = 300;
  20. //缩略图高度
  21. protected $thumHeight = 300;
  22. //存储位置
  23. protected $path = 'attach';
  24. protected $rules = ['thumb', 'thumbWidth', 'thumHeight', 'path'];
  25. /**
  26. * 获取即将要下载的图片扩展名
  27. * @param string $url
  28. * @param string $ex
  29. * @return array|string[]
  30. */
  31. public function getImageExtname($url = '', $ex = 'jpg')
  32. {
  33. $_empty = ['file_name' => '', 'ext_name' => $ex];
  34. if (!$url) return $_empty;
  35. if (strpos($url, '?')) {
  36. $_tarr = explode('?', $url);
  37. $url = trim($_tarr[0]);
  38. }
  39. $arr = explode('.', $url);
  40. if (!is_array($arr) || count($arr) <= 1) return $_empty;
  41. $ext_name = trim($arr[count($arr) - 1]);
  42. $ext_name = !$ext_name ? $ex : $ext_name;
  43. return ['file_name' => md5($url) . '.' . $ext_name, 'ext_name' => $ext_name];
  44. }
  45. /**
  46. * 下载图片
  47. * @param string $url
  48. * @param string $name
  49. * @param int $upload_type
  50. * @return mixed
  51. */
  52. public function downloadImage(string $url, $name = '')
  53. {
  54. if (!$name) {
  55. // 获取要下载的文件名称
  56. $downloadImageInfo = $this->getImageExtname($url);
  57. $ext = $downloadImageInfo['ext_name'];
  58. $name = $downloadImageInfo['file_name'];
  59. if (!$name) throw new ValidateException('上传图片不存在');
  60. } else {
  61. $ext = $this->getImageExtname($name)['ext_name'];
  62. }
  63. if (!in_array($ext, ['png', 'jpg', 'jpeg', 'gif', 'webp', 'bpm', 'tif', 'pcx', 'tga', 'exif', 'fpx', 'svg', 'apng'])) {
  64. $headerArray = get_headers(str_replace('\\', '/', $url), true);
  65. if (is_array($headerArray['Content-Type']) && count($headerArray['Content-Type']) == 2) {
  66. $type = $headerArray['Content-Type'][1];
  67. } else {
  68. $type = $headerArray['Content-Type'];
  69. }
  70. if(!$type) throw new ValidateException('格式错误,文件后缀不允许');
  71. $img = explode('/',$type);
  72. if($img[0]!= 'image') throw new ValidateException('格式错误,文件后缀不允许');
  73. }
  74. if (strstr($url, 'http://') === false && strstr($url, 'https://') === false) {
  75. $url = 'http:' . $url;
  76. }
  77. $url = str_replace('https://', 'http://', $url);
  78. if ($this->path == 'attach') {
  79. $date_dir = date('Y') . DIRECTORY_SEPARATOR . date('m') . DIRECTORY_SEPARATOR . date('d');
  80. $to_path = $this->path . '/' . $date_dir;
  81. } else {
  82. $to_path = $this->path;
  83. }
  84. $upload = UploadService::init(1);
  85. if (!file_exists($upload->uploadDir($to_path) . '/' . $name)) {
  86. ob_start();
  87. readfile($url);
  88. $content = ob_get_contents();
  89. ob_end_clean();
  90. $size = strlen(trim($content));
  91. if (!$content || $size <= 2) throw new ValidateException('图片流获取失败');
  92. if ($upload->to($to_path)->down($content, $name) === false) {
  93. throw new ValidateException('图片下载失败');
  94. }
  95. $imageInfo = $upload->getDownloadInfo();
  96. $path = $imageInfo['dir'];
  97. if ($this->thumb) {
  98. Image::open(root_path() . 'public' . $path)->thumb($this->thumbWidth, $this->thumHeight)->save(root_path() . 'public' . $path);
  99. $this->thumb = false;
  100. }
  101. } else {
  102. $path = '/uploads/' . $to_path . '/' . $name;
  103. $imageInfo['name'] = $name;
  104. }
  105. $date['path'] = $path;
  106. $date['name'] = $imageInfo['name'];
  107. $date['size'] = $imageInfo['size'] ?? '';
  108. $date['mime'] = $imageInfo['type'] ?? '';
  109. $date['image_type'] = 1;
  110. $date['is_exists'] = false;
  111. return $date;
  112. }
  113. /**
  114. * @param $name
  115. * @param $arguments
  116. * @return $this
  117. */
  118. public function __call($name, $arguments)
  119. {
  120. if (in_array($name, $this->rules)) {
  121. if ($name === 'path') {
  122. $this->{$name} = $arguments[0] ?? 'attach';
  123. } else {
  124. $this->{$name} = $arguments[0] ?? null;
  125. }
  126. return $this;
  127. } else {
  128. throw new \RuntimeException('Method does not exist' . __CLASS__ . '->' . $name . '()');
  129. }
  130. }
  131. }