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

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

parent 7ba4198d
......@@ -25,6 +25,8 @@ class Drawer
private $savePath = '';
private $ossDomain = 'http://huaperfect-file-service.oss-cn-hangzhou.aliyuncs.com';
public function __construct()
{
$this->config = container(ConfigInterface::class);
......@@ -49,16 +51,16 @@ class Drawer
{
$url = $this->parseUrl($url);
// excel画图中下载图片时对图片名做urlencode处理,防止中文名不能正常画图片的bug
$filename = trim(pathinfo($url, PATHINFO_FILENAME));
$ext = strtolower(pathinfo($url, PATHINFO_EXTENSION));
$pathinfo = pathinfo($url);
$filename = trim($pathinfo['filename']);
$ext = strtolower($pathinfo['extension']);
$filename = "$filename.$ext";
$path = $this->rootPath . '/download/images/' . ($path ?: $this->savePath);
if (!is_dir($path)) {
$savePath = $this->parseSavePath($pathinfo['dirname'], $path);
if (!is_dir($savePath)) {
// 判断路径是否存在,不存在,则创建
mkdir($path, 0777, true);
mkdir($savePath, 0777, true);
}
$filePath = $path . '/' . $filename;
$filePath = $savePath . '/' . $filename;
if (!file_exists($filePath)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
......@@ -87,6 +89,26 @@ class Drawer
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)
{
......@@ -187,7 +209,7 @@ class Drawer
if ($imgInfo && end($imgInfo) == "image/webp") {
$filename = trim(pathinfo($filePath, PATHINFO_FILENAME));
$im = imagecreatefromwebp($filePath); // 加载 WebP 文件
switch ($ext){
switch ($ext) {
case 'jpg':
$toNewFileName = $savePath . $filename . '.jpg';
break;
......@@ -362,7 +384,7 @@ class Drawer
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