Commit 128cd85a authored by 王源's avatar 王源 🎧

对下载图片进行oss原路径提取,以防重名覆盖bug

parent 7ba4198d
...@@ -25,6 +25,8 @@ class Drawer ...@@ -25,6 +25,8 @@ class Drawer
private $savePath = ''; private $savePath = '';
private $ossDomain = 'http://huaperfect-file-service.oss-cn-hangzhou.aliyuncs.com';
public function __construct() public function __construct()
{ {
$this->config = container(ConfigInterface::class); $this->config = container(ConfigInterface::class);
...@@ -49,16 +51,16 @@ class Drawer ...@@ -49,16 +51,16 @@ class Drawer
{ {
$url = $this->parseUrl($url); $url = $this->parseUrl($url);
// excel画图中下载图片时对图片名做urlencode处理,防止中文名不能正常画图片的bug // excel画图中下载图片时对图片名做urlencode处理,防止中文名不能正常画图片的bug
$filename = trim(pathinfo($url, PATHINFO_FILENAME)); $pathinfo = pathinfo($url);
$ext = strtolower(pathinfo($url, PATHINFO_EXTENSION)); $filename = trim($pathinfo['filename']);
$ext = strtolower($pathinfo['extension']);
$filename = "$filename.$ext"; $filename = "$filename.$ext";
$savePath = $this->parseSavePath($pathinfo['dirname'], $path);
$path = $this->rootPath . '/download/images/' . ($path ?: $this->savePath); if (!is_dir($savePath)) {
if (!is_dir($path)) {
// 判断路径是否存在,不存在,则创建 // 判断路径是否存在,不存在,则创建
mkdir($path, 0777, true); mkdir($savePath, 0777, true);
} }
$filePath = $path . '/' . $filename; $filePath = $savePath . '/' . $filename;
if (!file_exists($filePath)) { if (!file_exists($filePath)) {
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
...@@ -87,6 +89,26 @@ class Drawer ...@@ -87,6 +89,26 @@ class Drawer
return $this->handleOssUrl($url); return $this->handleOssUrl($url);
} }
/**
* 处理保存路径
* @param $dirname
* @param $path
* @return string
* @author Zero
*/
private function parseSavePath($dirname, $path)
{
$path = trim($path, '/');
$path = $this->rootPath . '/download/images/' . ($path ?: $this->savePath) . '/';
if (strstr($dirname, $this->ossDomain) !== false) {
// 以oss的原路径为保存路径
$dirname = str_replace($this->ossDomain, '', $dirname);
$dirname = trim($dirname, '/');
$path = $path . $dirname . '/';
}
return $path;
}
// 替换阿里云图片内部地址 // 替换阿里云图片内部地址
private function handleOssUrl($url) private function handleOssUrl($url)
{ {
...@@ -187,7 +209,7 @@ class Drawer ...@@ -187,7 +209,7 @@ class Drawer
if ($imgInfo && end($imgInfo) == "image/webp") { if ($imgInfo && end($imgInfo) == "image/webp") {
$filename = trim(pathinfo($filePath, PATHINFO_FILENAME)); $filename = trim(pathinfo($filePath, PATHINFO_FILENAME));
$im = imagecreatefromwebp($filePath); // 加载 WebP 文件 $im = imagecreatefromwebp($filePath); // 加载 WebP 文件
switch ($ext){ switch ($ext) {
case 'jpg': case 'jpg':
$toNewFileName = $savePath . $filename . '.jpg'; $toNewFileName = $savePath . $filename . '.jpg';
break; break;
...@@ -362,7 +384,7 @@ class Drawer ...@@ -362,7 +384,7 @@ class Drawer
public function setSavePath($path) public function setSavePath($path)
{ {
$this->savePath = $path; $this->savePath = trim($path, '/');
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment