Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DownloadImageService.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace crmeb\services;
  3. use think\exception\ValidateException;
  4. use think\Image;
  5. class DownloadImageService
  6. {
  7. //是否生成缩略图
  8. protected $thumb = false;
  9. //缩略图宽度
  10. protected $thumbWidth = 300;
  11. //缩略图高度
  12. protected $thumHeight = 300;
  13. //存储位置
  14. protected $path = 'attach';
  15. protected $rules = ['thumb', 'thumbWidth', 'thumHeight', 'path'];
  16. /**
  17. * 获取即将要下载的图片扩展名
  18. * @param string $url
  19. * @param string $ex
  20. * @return array|string[]
  21. */
  22. public function getImageExtname($url = '', $ex = 'jpg')
  23. {
  24. $_empty = ['file_name' => '', 'ext_name' => $ex];
  25. if (!$url) return $_empty;
  26. if (strpos($url, '?')) {
  27. $_tarr = explode('?', $url);
  28. $url = trim($_tarr[0]);
  29. }
  30. $arr = explode('.', $url);
  31. if (!is_array($arr) || count($arr) <= 1) return $_empty;
  32. $ext_name = trim($arr[count($arr) - 1]);
  33. $ext_name = !$ext_name ? $ex : $ext_name;
  34. return ['file_name' => md5($url) . '.' . $ext_name, 'ext_name' => $ext_name];
  35. }
  36. /**
  37. * 下载图片
  38. * @param string $url
  39. * @param string $name
  40. * @param int $upload_type
  41. * @return mixed
  42. */
  43. public function downloadImage(string $url, $name = '')
  44. {
  45. if (!$name) {
  46. // 获取要下载的文件名称
  47. $downloadImageInfo = $this->getImageExtname($url);
  48. $name = $downloadImageInfo['file_name'];
  49. if (!$name) throw new ValidateException('上传图片不存在');
  50. }
  51. if (strstr($url, 'http://') === false && strstr($url, 'https://') === false) {
  52. $url = 'http:' . $url;
  53. }
  54. $url = str_replace('https://', 'http://', $url);
  55. if ($this->path == 'attach') {
  56. $date_dir = date('Y') . DIRECTORY_SEPARATOR . date('m') . DIRECTORY_SEPARATOR . date('d');
  57. $to_path = $this->path . '/' . $date_dir;
  58. } else {
  59. $to_path = $this->path;
  60. }
  61. $upload = UploadService::init(1);
  62. if (!file_exists($upload->uploadDir($to_path) . '/' . $name)) {
  63. ob_start();
  64. readfile($url);
  65. $content = ob_get_contents();
  66. ob_end_clean();
  67. $size = strlen(trim($content));
  68. if (!$content || $size <= 2) throw new ValidateException('图片流获取失败');
  69. if ($upload->to($to_path)->down($content, $name) === false) {
  70. throw new ValidateException('图片下载失败');
  71. }
  72. $imageInfo = $upload->getDownloadInfo();
  73. $path = $imageInfo['dir'];
  74. if ($this->thumb) {
  75. Image::open(root_path() . 'public' . $path)->thumb($this->thumbWidth, $this->thumHeight)->save(root_path() . 'public' . $path);
  76. $this->thumb = false;
  77. }
  78. } else {
  79. $path = '/uploads/' . $to_path . '/' . $name;
  80. $imageInfo['name'] = $name;
  81. }
  82. $date['path'] = $path;
  83. $date['name'] = $imageInfo['name'];
  84. $date['size'] = $imageInfo['size'] ?? '';
  85. $date['mime'] = $imageInfo['type'] ?? '';
  86. $date['image_type'] = 1;
  87. $date['is_exists'] = false;
  88. return $date;
  89. }
  90. /**
  91. * @param $name
  92. * @param $arguments
  93. * @return $this
  94. */
  95. public function __call($name, $arguments)
  96. {
  97. if (in_array($name, $this->rules)) {
  98. if ($name === 'path') {
  99. $this->{$name} = $arguments[0] ?? 'attach';
  100. } else {
  101. $this->{$name} = $arguments[0] ?? null;
  102. }
  103. return $this;
  104. } else {
  105. throw new \RuntimeException('Method does not exist' . __CLASS__ . '->' . $name . '()');
  106. }
  107. }
  108. }