DownloadImage.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\Image;
  15. /**
  16. * 下载图片到本地
  17. * Class DownloadImage
  18. * @package crmeb\utils
  19. * @method $this thumb(bool $thumb) 是否生成缩略图
  20. * @method $this thumbWidth(int $thumbWidth) 缩略图宽度
  21. * @method $this thumHeight(int $thumHeight) 缩略图宽度
  22. * @method $this path(int $path) 存储位置
  23. */
  24. class DownloadImage
  25. {
  26. //是否生成缩略图
  27. protected $thumb = false;
  28. //缩略图宽度
  29. protected $thumbWidth = 300;
  30. //缩略图高度
  31. protected $thumHeight = 300;
  32. //存储位置
  33. protected $path = 'attach';
  34. /**
  35. * @var string[]
  36. */
  37. protected $rules = ['thumb', 'thumbWidth', 'thumHeight', 'path'];
  38. /**
  39. * 获取即将要下载的图片扩展名
  40. * @param string $url
  41. * @param string $ex
  42. * @return array|string[]
  43. */
  44. public function getImageExtname($url = '', $ex = 'jpg')
  45. {
  46. $_empty = ['file_name' => '', 'ext_name' => $ex];
  47. if (!$url) return $_empty;
  48. if (strpos($url, '?')) {
  49. $_tarr = explode('?', $url);
  50. $url = trim($_tarr[0]);
  51. }
  52. $arr = explode('.', $url);
  53. if (!is_array($arr) || count($arr) <= 1) return $_empty;
  54. $ext_name = trim($arr[count($arr) - 1]);
  55. $ext_name = !$ext_name ? $ex : $ext_name;
  56. return ['file_name' => md5($url) . '.' . $ext_name, 'ext_name' => $ext_name];
  57. }
  58. /**
  59. * 下载图片
  60. * @param string $url
  61. * @param string $name
  62. * @param int $upload_type
  63. * @return mixed
  64. */
  65. public function downloadImage(string $url, $name = '')
  66. {
  67. if (!$name) {
  68. //TODO 获取要下载的文件名称
  69. $downloadImageInfo = $this->getImageExtname($url);
  70. $ext = $downloadImageInfo['ext_name'];
  71. $name = $downloadImageInfo['file_name'];
  72. if (!$name) throw new AdminException(400725);
  73. } else {
  74. $ext = $this->getImageExtname($name)['ext_name'];
  75. }
  76. if (!in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'JPG', 'JPEG', 'PNG', 'GIF'])) {
  77. throw new AdminException(400558);
  78. }
  79. if (strstr($url, 'http://') === false && strstr($url, 'https://') === false) {
  80. $url = 'http:' . $url;
  81. }
  82. $url = str_replace('https://', 'http://', $url);
  83. if ($this->path == 'attach') {
  84. $date_dir = date('Y') . DIRECTORY_SEPARATOR . date('m') . DIRECTORY_SEPARATOR . date('d');
  85. $to_path = $this->path . '/' . $date_dir;
  86. } else {
  87. $to_path = $this->path;
  88. }
  89. $upload = UploadService::init(1);
  90. if (!file_exists($upload->uploadDir($to_path) . '/' . $name)) {
  91. ob_start();
  92. readfile($url);
  93. $content = ob_get_contents();
  94. ob_end_clean();
  95. $size = strlen(trim($content));
  96. if (!$content || $size <= 2) throw new AdminException(400726);
  97. if ($upload->to($to_path)->down($content, $name) === false) {
  98. throw new AdminException(400727);
  99. }
  100. $imageInfo = $upload->getDownloadInfo();
  101. $path = $imageInfo['dir'];
  102. if ($this->thumb) {
  103. Image::open(root_path() . 'public' . $path)->thumb($this->thumbWidth, $this->thumHeight)->save(root_path() . 'public' . $path);
  104. $this->thumb = false;
  105. }
  106. } else {
  107. $path = '/uploads/' . $to_path . '/' . $name;
  108. $imageInfo['name'] = $name;
  109. }
  110. $date['path'] = $path;
  111. $date['name'] = $imageInfo['name'];
  112. $date['size'] = $imageInfo['size'] ?? '';
  113. $date['mime'] = $imageInfo['type'] ?? '';
  114. $date['image_type'] = 1;
  115. $date['is_exists'] = false;
  116. return $date;
  117. }
  118. /**
  119. * @param $name
  120. * @param $arguments
  121. * @return $this
  122. */
  123. public function __call($name, $arguments)
  124. {
  125. if (in_array($name, $this->rules)) {
  126. if ($name === 'path') {
  127. $this->{$name} = $arguments[0] ?? 'attach';
  128. } else {
  129. $this->{$name} = $arguments[0] ?? null;
  130. }
  131. return $this;
  132. } else {
  133. throw new \RuntimeException('Method does not exist' . __CLASS__ . '->' . $name . '()');
  134. }
  135. }
  136. }