DownloadImageService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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 crmeb\exceptions\AdminException;
  13. use think\exception\ValidateException;
  14. use think\facade\Config;
  15. class DownloadImageService
  16. {
  17. //存储位置
  18. protected $path = 'attach';
  19. protected $rules = ['thumb', 'thumbWidth', 'thumHeight', 'path'];
  20. /**
  21. * 获取即将要下载的图片扩展名
  22. * @param string $url
  23. * @param string $ex
  24. * @return array|string[]
  25. */
  26. public function getImageExtname($url = '', $ex = 'jpg')
  27. {
  28. $_empty = ['file_name' => '', 'ext_name' => $ex];
  29. if (!$url) return $_empty;
  30. if (strpos($url, '?')) {
  31. $_tarr = explode('?', $url);
  32. $url = trim($_tarr[0]);
  33. }
  34. $arr = explode('.', $url);
  35. if (!is_array($arr) || count($arr) <= 1) return $_empty;
  36. $ext_name = trim($arr[count($arr) - 1]);
  37. $ext_name = !$ext_name ? $ex : $ext_name;
  38. return ['file_name' => md5($url) . '.' . $ext_name, 'ext_name' => $ext_name];
  39. }
  40. /**
  41. * @param $url
  42. * @param string $name
  43. * @param int $upload_type
  44. * @return mixed
  45. * @author xaboy
  46. * @day 2020/8/1
  47. */
  48. public function downloadImage($url, $path = 'def', $name = '', $upload_type = null)
  49. {
  50. if (!$name) {
  51. //TODO 获取要下载的文件名称
  52. $downloadImageInfo = $this->getImageExtname($url);
  53. $ext = $downloadImageInfo['ext_name'];
  54. $name = $downloadImageInfo['file_name'];
  55. if (!$name) throw new ValidateException('上传图片不存在');
  56. }else {
  57. $ext = $this->getImageExtname($name)['ext_name'];
  58. }
  59. if (!in_array($ext, Config::get('upload.fileExt'))) {
  60. throw new AdminException('图片格式错误,无法下载');
  61. }
  62. if (strstr($url, 'http://') === false && strstr($url, 'https://') === false) {
  63. $url = 'http:' . $url;
  64. }
  65. $url = str_replace('https://', 'http://', $url);
  66. if ($this->path == 'attach') {
  67. $date_dir = date('Y') . DIRECTORY_SEPARATOR . date('m') . DIRECTORY_SEPARATOR . date('d');
  68. $to_path = $this->path . '/' . $date_dir;
  69. } else {
  70. $to_path = $this->path;
  71. }
  72. checkSuffix($url);
  73. ob_start();
  74. readfile($url);
  75. $content = ob_get_contents();
  76. ob_end_clean();
  77. $size = strlen(trim($content));
  78. if (!$content || $size <= 2) throw new ValidateException('图片流获取失败');
  79. $upload = UploadService::create($upload_type);
  80. if ($upload->to($to_path)->stream($content, $name) === false) {
  81. throw new ValidateException('图片下载失败');
  82. }
  83. $imageInfo = $upload->getUploadInfo();
  84. $date['path'] = $imageInfo['dir'];
  85. $date['name'] = $imageInfo['name'];
  86. $date['size'] = $imageInfo['size'];
  87. $date['mime'] = $imageInfo['type'];
  88. return $date;
  89. }
  90. public function downloadImages($urls, $path = 'def',$upload_type = null)
  91. {
  92. if ($urls) {
  93. try{
  94. foreach ($urls as $url) {
  95. $res[] = $this->downloadImage($url,$path,'',$upload_type);
  96. }
  97. return $res;
  98. }catch (\Exception $exception) {
  99. throw new ValidateException($exception->getMessage());
  100. }
  101. }
  102. return [];
  103. }
  104. /**
  105. * @param $name
  106. * @param $arguments
  107. * @return $this
  108. */
  109. public function __call($name, $arguments)
  110. {
  111. if (in_array($name, $this->rules)) {
  112. if ($name === 'path') {
  113. $this->{$name} = $arguments[0] ?? 'attach';
  114. } else {
  115. $this->{$name} = $arguments[0] ?? null;
  116. }
  117. return $this;
  118. } else {
  119. throw new \RuntimeException('Method does not exist' . __CLASS__ . '->' . $name . '()');
  120. }
  121. }
  122. }