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
85446694
Commit
85446694
authored
Sep 17, 2020
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加图片工具类
parent
90c64948
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
0 deletions
+75
-0
Imager.php
src/Tools/Imager.php
+75
-0
No files found.
src/Tools/Imager.php
0 → 100644
View file @
85446694
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/9/17
* Time: 8:40
*/
namespace
Meibuyu\Micro\Tools
;
use
Exception
;
/**
* 图片工具类
* Class Imager
* @package Meibuyu\Micro\Tools
*/
class
Imager
{
/**
* 图片转base64,暂不支持外部地址图片
* @param $path
* @param bool $noPrefix 不需要前缀
* @return string
* @throws Exception
*/
public
static
function
img2Base64
(
$path
,
$noPrefix
=
false
)
{
$path
=
self
::
parsePath
(
$path
);
if
(
file_exists
(
$path
))
{
$fp
=
fopen
(
$path
,
"r"
);
// 图片是否可读权限
if
(
$fp
)
{
$imgInfo
=
getimagesize
(
$path
);
// 取得图片的大小,类型等
$content
=
fread
(
$fp
,
filesize
(
$path
));
$content
=
chunk_split
(
base64_encode
(
$content
));
// base64编码
if
(
$noPrefix
)
{
$base64
=
$content
;
}
else
{
$base64
=
'data:'
.
$imgInfo
[
'mime'
]
.
';base64,'
.
$content
;
// 合成图片的base64编码
}
fclose
(
$fp
);
return
$base64
;
}
else
{
throw
new
Exception
(
'图片读取失败'
);
}
}
else
{
throw
new
Exception
(
'图片不存在'
);
}
}
/**
* @param $path
* @return string
* @throws Exception
*/
public
static
function
parsePath
(
$path
)
{
if
(
strstr
(
$path
,
'http'
))
{
$appDomain
=
config
(
'app_domain'
);
if
(
strstr
(
$path
,
$appDomain
))
{
$path
=
str_replace
(
$appDomain
,
''
,
$path
);
}
else
{
throw
new
Exception
(
'暂不支持外部地址图片'
);
}
}
$documentRoot
=
config
(
'server.settings.document_root'
);
if
(
!
strstr
(
$path
,
$documentRoot
))
{
$path
=
trim
(
trim
(
$path
),
'/'
);
$path
=
$documentRoot
.
'/'
.
$path
;
}
return
$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