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
00a952d0
Commit
00a952d0
authored
Oct 21, 2020
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化画图工具类,添加合并图片的方法
parent
20fc68ee
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
69 additions
and
8 deletions
+69
-8
Drawer.php
src/Tools/Drawer.php
+69
-8
No files found.
src/Tools/Drawer.php
View file @
00a952d0
...
...
@@ -46,11 +46,12 @@ class Drawer
*/
public
function
downloadWebImage
(
$url
,
$path
=
null
)
{
$url
=
$this
->
parseUrl
(
$url
);
// excel画图中下载图片时对图片名做urlencode处理,防止中文名不能正常画图片的bug
$filename
=
urlencode
(
trim
(
pathinfo
(
$url
,
PATHINFO_FILENAME
)
));
$filename
=
trim
(
pathinfo
(
$url
,
PATHINFO_FILENAME
));
$ext
=
strtolower
(
pathinfo
(
$url
,
PATHINFO_EXTENSION
));
$filename
=
"
$filename
.
$ext
"
;
$url
=
$this
->
parseUrl
(
$url
);
$path
=
$this
->
rootPath
.
'/download/images/'
.
(
$path
?:
$this
->
savePath
);
if
(
!
is_dir
(
$path
))
{
// 判断路径是否存在,不存在,则创建
...
...
@@ -65,8 +66,7 @@ class Drawer
curl_setopt
(
$ch
,
CURLOPT_CONNECTTIMEOUT
,
30
);
$file
=
curl_exec
(
$ch
);
curl_close
(
$ch
);
$resource
=
fopen
(
$path
.
'/'
.
$filename
,
'a'
);
$resource
=
fopen
(
$filePath
,
'a'
);
fwrite
(
$resource
,
$file
);
fclose
(
$resource
);
}
...
...
@@ -75,9 +75,12 @@ class Drawer
private
function
parseUrl
(
$url
)
{
if
(
!
preg_match
(
'/%[0-9A-Z]{2}/'
,
$url
))
{
$url
=
rawurlencode
(
$url
);
$url
=
str_replace
(
"%3A"
,
":"
,
$url
);
return
str_replace
(
"%2F"
,
"/"
,
$url
);
$url
=
str_replace
(
"%2F"
,
"/"
,
$url
);
}
return
$url
;
}
/**
...
...
@@ -151,6 +154,64 @@ class Drawer
return
$res
?
$imgNewPath
:
false
;
}
/**
* 拼图
* @param $imgPathList
* @return bool|string
*/
public
function
mergeImages
(
$imgPathList
)
{
$maxW
=
$maxH
=
0
;
$filenameList
=
[];
$imageList
=
[];
foreach
(
$imgPathList
as
$k
=>
$path
)
{
$path
=
$this
->
parseImagePath
(
$path
);
$imgPathInfo
=
pathinfo
(
$path
);
$filename
=
$imgPathInfo
[
'filename'
];
// 图片名称
$ext
=
$imgPathInfo
[
'extension'
];
// 图片扩展名
[
$w
,
$h
]
=
getimagesize
(
$path
);
// 图片大小
$imageList
[
$k
]
=
[
'path'
=>
$path
,
'filename'
=>
$filename
,
'ext'
=>
$ext
,
'w'
=>
$w
,
'h'
=>
$h
,
];
$filenameList
[]
=
$filename
;
$maxW
+=
$w
;
if
(
$maxH
<
$h
)
{
$maxH
=
$h
;
}
}
$filenameList
=
collect
(
$filenameList
)
->
unique
()
->
sort
()
->
toArray
();
$filename
=
implode
(
'_'
,
$filenameList
);
$savePath
=
$this
->
rootPath
.
'/download/merge/'
.
$this
->
savePath
;
if
(
!
is_dir
(
$savePath
))
{
// 判断路径是否存在,不存在,则创建
mkdir
(
$savePath
,
0777
,
true
);
}
$imgNewPath
=
$savePath
.
'/'
.
$filename
.
'.png'
;
if
(
file_exists
(
$imgNewPath
))
{
return
$imgNewPath
;
}
// 黑色背景图片
$im
=
@
imagecreatetruecolor
(
$maxW
,
$maxH
)
or
die
(
"Cannot Initialize new GD image stream"
);
// 为真彩色画布创建背景,再设置为透明
$color
=
imagecolorallocate
(
$im
,
0
,
0
,
0
);
imagefill
(
$im
,
0
,
0
,
$color
);
imageColorTransparent
(
$im
,
$color
);
// 循环画图片
$dstX
=
0
;
foreach
(
$imageList
as
$item
)
{
$resource
=
imagecreatefromstring
(
file_get_contents
(
$item
[
'path'
]));
imagecopy
(
$im
,
$resource
,
$dstX
,
0
,
0
,
0
,
$item
[
'w'
],
$item
[
'h'
]);
$dstX
+=
$item
[
'w'
];
}
$res
=
imagepng
(
$im
,
$imgNewPath
);
imagedestroy
(
$im
);
return
$res
?
$imgNewPath
:
false
;
}
/**
* 画图片
* @param string $path 图片路径
...
...
@@ -167,7 +228,7 @@ class Drawer
$path
=
$this
->
addBoard
(
$path
,
$boardPx
);
}
$drawing
=
new
Drawing
();
$drawing
->
setPath
(
$path
)
->
setCoordinates
(
$p
)
->
setHeight
(
$h
)
->
setWorksheet
(
$sheet
);
$drawing
->
setPath
(
$path
)
->
setCoordinates
(
$p
)
->
setHeight
(
$h
)
->
set
OffsetX
(
1
)
->
setOffsetY
(
1
)
->
set
Worksheet
(
$sheet
);
}
catch
(
Exception
$e
)
{
put_log
(
$e
->
getMessage
());
}
...
...
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