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
0c58ab11
Commit
0c58ab11
authored
Nov 23, 2020
by
fuyunnan
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'feature-webp-download' into test
parents
f1178ba8
ef2323f6
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
99 additions
and
1 deletion
+99
-1
Drawer.php
src/Tools/Drawer.php
+99
-1
No files found.
src/Tools/Drawer.php
View file @
0c58ab11
...
...
@@ -83,6 +83,104 @@ class Drawer
return
$url
;
}
/**
* description:下载文件到服务端 增加判断是否是webp 格式的图片处理
* author: fuyunnan
* @param string $url
* @param string $path 文件路径
* @return string
* @throws
* Date: 2020/11/20
*/
public
function
downLoadImgWebpChannel
(
$url
,
$path
=
''
)
{
//如果shopify 去掉版本号
if
(
strpos
(
$url
,
'cdn.shopify.com'
)
!==
false
)
{
$url
=
substr
(
$url
,
0
,
strpos
(
$url
,
'?v='
));
}
$savePath
=
$this
->
rootPath
.
'/download/images/'
.
(
$path
?
$path
.
'/'
:
$this
->
savePath
);
create_file_dir
(
$savePath
);
$filePath
=
$this
->
toWebpImg
(
$url
,
$savePath
);
if
(
!
file_exists
(
$filePath
))
{
$filename
=
trim
(
pathinfo
(
$url
,
PATHINFO_FILENAME
));
$ext
=
strtolower
(
pathinfo
(
$url
,
PATHINFO_EXTENSION
));
$filePath
=
$savePath
.
"
$filename
.
$ext
"
;
$ch
=
curl_init
();
curl_setopt
(
$ch
,
CURLOPT_SSL_VERIFYPEER
,
false
);
// 信任任何证书
curl_setopt
(
$ch
,
CURLOPT_URL
,
$url
);
curl_setopt
(
$ch
,
CURLOPT_RETURNTRANSFER
,
1
);
curl_setopt
(
$ch
,
CURLOPT_CONNECTTIMEOUT
,
30
);
$file
=
curl_exec
(
$ch
);
curl_close
(
$ch
);
$resource
=
fopen
(
$filePath
,
'a'
);
fwrite
(
$resource
,
$file
);
fclose
(
$resource
);
$filePath
=
$this
->
toWebpImg
(
$filePath
,
$savePath
);
}
return
$filePath
;
}
/**
* description:将图像绘到excel表格上
* author: fuyunnan
* @param string $url 图片地址
* @param string $childPath 图片地址 用sku名作为文件夹名称
* @param int $h 图片高度
* @param string $p 单元格索引
* @param Worksheet $sheet
* @return void
* @throws
* Date: 2020/11/23
*/
public
function
drawImgExcel
(
$url
,
$childPath
,
$h
,
$p
,
$sheet
)
{
try
{
//下载图片到本地
$absPath
=
$this
->
downLoadImgWebpChannel
(
$url
,
$childPath
);
$drawing
=
new
Drawing
();
$drawing
->
setPath
(
$absPath
)
->
setCoordinates
(
$p
)
->
setHeight
(
$h
)
->
setOffsetX
(
1
)
->
setOffsetY
(
1
)
->
setWorksheet
(
$sheet
);
}
catch
(
Exception
$e
)
{
put_log
(
$e
->
getMessage
(),
'draw2Excel.log'
);
}
}
/**
* description:检查文件是否是webp格式的文件
* 如果是,转化为jpeg格式的文件,并且返回文件的绝对地址.
* author: fuyunnan
* @param string $filePath 文件的绝对地址
* @param string $savePath 文件重新保存的地址
* @return string
* @throws
* Date: 2020/11/23
*/
private
function
toWebpImg
(
$filePath
,
$savePath
)
{
$filename
=
trim
(
pathinfo
(
$filePath
,
PATHINFO_FILENAME
));
$ext
=
strtolower
(
pathinfo
(
$filePath
,
PATHINFO_EXTENSION
));
$imgInfo
=
''
;
//如果存在直接返回 防止每次获取文件资源
if
(
file_exists
(
$savePath
.
"
$filename
.
$ext
"
))
{
return
$savePath
.
"
$filename
.
$ext
"
;
}
$filePath
&&
$imgInfo
=
getimagesize
(
$filePath
);
if
(
$imgInfo
&&
end
(
$imgInfo
)
==
"image/webp"
)
{
$filename
=
trim
(
pathinfo
(
$filePath
,
PATHINFO_FILENAME
));
$im
=
imagecreatefromwebp
(
$filePath
);
// 加载 WebP 文件
imagejpeg
(
$im
,
$savePath
.
$filename
.
'.jpeg'
,
100
);
// 以 100% 的质量转换成 jpeg 格式
imagedestroy
(
$im
);
return
$savePath
.
$filename
.
'.jpeg'
;
}
return
$savePath
.
"
$filename
.
$ext
"
;
}
/**
* @param $imgPath
* @param int $px
...
...
@@ -230,7 +328,7 @@ class Drawer
$drawing
=
new
Drawing
();
$drawing
->
setPath
(
$path
)
->
setCoordinates
(
$p
)
->
setHeight
(
$h
)
->
setOffsetX
(
1
)
->
setOffsetY
(
1
)
->
setWorksheet
(
$sheet
);
}
catch
(
Exception
$e
)
{
put_log
(
$e
->
getMessage
());
put_log
(
$e
->
getMessage
()
,
'draw2Excel.log'
);
}
}
...
...
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