Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-rpc
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
without authentication
meibuyu-rpc
Commits
128cd85a
Commit
128cd85a
authored
Dec 26, 2020
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
对下载图片进行oss原路径提取,以防重名覆盖bug
parent
7ba4198d
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
9 deletions
+31
-9
Drawer.php
src/Tools/Drawer.php
+31
-9
No files found.
src/Tools/Drawer.php
View file @
128cd85a
...
@@ -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
(
$
p
ath
,
0777
,
true
);
mkdir
(
$
saveP
ath
,
0777
,
true
);
}
}
$filePath
=
$
p
ath
.
'/'
.
$filename
;
$filePath
=
$
saveP
ath
.
'/'
.
$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
,
'/'
)
;
}
}
/**
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment