首页 > PHP教程 > php开发知识文章

php 以base64编码读取和保存图片的内容

本介绍了php 以base64编码读取和保存图片的内容的具体代码,供大家参考,欢迎大家的学习。

<?php
header('Content-type:text/html;charset=utf-8');
//读取图片文件,转换成base64编码格式
$pic_file = './2.jpg';
$pic_info = getimagesize($pic_file);
$base64_pic_content = "data:{$pic_info['mime']};base64," .
chunk_split(base64_encode(file_get_contents($pic_file)));

//匹配出图片的格式
if (preg_match('/^(data:/s*image//(/w+);base64,)/', $base64_pic_content, $result)) {
$type = $result[2];
$new_file = "./.{$type}";
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_pic_content)))) {
echo '文件保存成功:', $new_file;
}
}
?>
<img src="<?php echo $base64_pic_content; ?>"/>

以上就是本文php 以base64编码读取和保存图片的内容的全部内容,希望对大家的学习有所帮助。

php 以base64编码读取和保存图片的内容

关闭
感谢您的支持,我会继续努力!
扫码打赏,建议金额1-10元


提醒:打赏金额将直接进入对方账号,无法退款,请您谨慎操作。