<
meta charset
=
"UTF-8"
>
<
title
>
<
/
title
>
<
script type
=
"text/javascript"
src
=
"../js/jquery.min.js"
>
<
/
script
>
<
/
head
>
<
form action
=
"test.php"
method
=
"post"
enctype
=
"multipart/form-data"
>
<
input type
=
"file"
name
=
"file"
/
>
<
input type
=
"submit"
name
=
"submit"
/
>
<
input type
=
"button"
value
=
"清空"
onclick
=
"window.location.href='test.php'"
/
>
$url
=
$_GET
[
'url'
]
;
if
(
!
empty
(
$url
)
&&
file_exists
(
'test/'
.
$url
)
)
{
echo
"<a href='test/
$url
' download='
$url
' title='仅支持谷歌下载'>
$url
</a>"
;
<
/
form
>
<
/
body
>
<
/
html
>
if
(
!
empty
(
$_FILES
[
'file'
]
)
)
{
require_once
(
'function/uploadUtil.php'
)
;
$upload
=
new
upload
(
)
;
$upload
-
>
uploadPath
=
'test/tmp'
;
$arr
=
$upload
-
>
uploadFile
(
)
;
$image_path
=
'test/tmp/'
.
$arr
[
'msg'
]
;
$save_path
=
substr
(
$arr
[
'msg1'
]
,
0
,
strpos
(
$arr
[
'msg1'
]
,
"."
)
)
.
'.png'
;
if
(
preg_match
(
"/[\x7f-\xff]/"
,
$save_path
)
)
{
$save_path
=
substr
(
$arr
[
'msg'
]
,
0
,
strpos
(
$arr
[
'msg'
]
,
"."
)
)
.
'.png'
;
transform_image
(
$image_path
,
'png'
,
'test/'
.
$save_path
)
;
@
unlink
(
'test/tmp/'
.
$arr
[
'msg'
]
)
;
$save_path
=
htmlspecialchars
(
$save_path
)
;
echo
"<script>window.location.href='test.php?url=
$save_path
'</script>"
;
* 图片格式转换 https://www.php.cn/php-ask-458661.html
* @param string $image_path 文件路径或url
* @param string $to_ext 待转格式,支持png,gif,jpeg,wbmp,webp,xbm
* @param null|string $save_path 存储路径,null则返回二进制内容,string则返回true|false
* @return boolean|string $save_path是null则返回二进制内容,是string则返回true|false
* @throws Exception
* @author klinson
function
transform_image
(
$image_path
,
$to_ext
=
'png'
,
$save_path
=
null
)
if
(
!
in_array
(
$to_ext
,
[
'png'
,
'gif'
,
'jpeg'
,
'wbmp'
,
'webp'
,
'xbm'
]
)
)
{
throw
new
\
Exception
(
'unsupport transform image to '
.
$to_ext
)
;
switch
(
exif_imagetype
(
$image_path
)
)
{
case
IMAGETYPE_GIF
:
$img
=
imagecreatefromgif
(
$image_path
)
;
break
;
case
IMAGETYPE_JPEG
:
case
IMAGETYPE_JPEG2000
:
$img
=
imagecreatefromjpeg
(
$image_path
)
;
break
;
case
IMAGETYPE_PNG
:
$img
=
imagecreatefrompng
(
$image_path
)
;
break
;
case
IMAGETYPE_BMP
:
case
IMAGETYPE_WBMP
:
$img
=
imagecreatefromwbmp
(
$image_path
)
;
break
;
case
IMAGETYPE_XBM
:
$img
=
imagecreatefromxbm
(
$image_path
)
;
break
;
case
IMAGETYPE_WEBP
:
$img
=
imagecreatefromwebp
(
$image_path
)
;
break
;
default
:
throw
new
\
Exception
(
'Invalid image type'
)
;
$function
=
'image'
.
$to_ext
;
if
(
$save_path
)
{
return
$function
(
$img
,
$save_path
)
;
}
else
{
$tmp
=
__DIR__
.
'/'
.
uniqid
(
)
.
'.'
.
$to_ext
;
if
(
$function
(
$img
,
$tmp
)
)
{
$content
=
file_get_contents
(
$tmp
)
;
unlink
(
$tmp
)
;
return
$content
;
}
else
{
unlink
(
$tmp
)
;
throw
new
\
Exception
(
'the file '
.
$tmp
.
' can not write'
)
;
PHP将图片转换为png格式<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="../js/jquery.min.js"></script></head><body> <form acti
$begin_b = 250;
list($src_w, $src_h) = getimagesize($o_pic);// 获取原图像信息 宽高
$src_im = imagecreatefromjpeg($o_pic); //读取
png
图片
print_r($src_im);
//imagesave
引入自动加载类
require_once '../../
Php
Office/
PHP
Presentation/src/
Php
Presentation/Autoloader.
php
' ;
\
Php
O
运行creat.
php
生成SERVER.wsdl
更改生成的.wsdl中<port>部分soap:address的location
<port name="..." binding="..." ><soap:address location="http://127.0.0.1/wsdl/creat.
php
"></port>
<port name="..." binding="..." ><soap:address lo
// 从base64字符串中获取数据
$data = 'data:image/
png
;base64,iVBORw0KGg...';
$data = substr($data, strpos($data, ',') + 1);
// 解码数据并保存为
图片
文件
$file = 'image.
png
';
$success = file_put_contents($file, base64_decode($data));
if ($success) {
echo "
图片
已保存到 $file";
} else {
echo "保存
图片
失败";
解释一下上面的代码:
- 首先,我们从base64字符串中提取出数据部分,即去掉"data:image/
png
;base64,"这段前缀。
- 然后,我们使用base64_decode()函数将数据解码为二进制数据。
- 最后,我们使用file_put_contents()函数将二进制数据保存到文件中。
请注意,这个例子假设base64字符串表示的是
PNG
格式
的
图片
。如果你的base64字符串表示的是其他
格式
的
图片
,比如JPEG,你需要相应地修改代码。