DownloadImage.php 4.9 KB

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