nykek5i 发表于 2024-10-4 19:42:41

PHP怎么样将图像转换为base64编码格式


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">点击</span><span style="color: black;">蓝字</span><img src="https://mmbiz.qpic.cn/mmbiz_png/Lhm5kkN9JXPHtJf8zPadjF40glHh4GIt7e9OZpWcMWdEpaope3y3fm4qOL6tqvdRqIcJwMT1qd4ibdRRhX3Hr9A/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"><span style="color: black;">关注<span style="color: black;">咱们</span>!<span style="color: black;">每日</span>获取最新的编程小知识!</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/Lhm5kkN9JXPHtJf8zPadjF40glHh4GItgW4fo5Y3vkHYhDFLyadbnYDTcLT4V9QyWHSn2alnfhUicwp2LYDRshw/640?wx_fmt=gif&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">源 /&nbsp;</span><span style="color: black;">php中文网</span>&nbsp; &nbsp;&nbsp;<span style="color: black;">源 /&nbsp;</span><span style="color: black;">www.php.cn</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">base64_encode()函数是PHP中的一个内置函数,用于将任何数据转换为base64编码。为了将图像转换成base64编码,<span style="color: black;">首要</span>需要获取文件的内容,这<span style="color: black;">能够</span><span style="color: black;">经过</span>PHP的file_get_contents()函数来完成。<span style="color: black;">而后</span>将这个原始数据传递给base64_encode()函数进行编码。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/Lhm5kkN9JXMZt7AOTNDxOFRJh9H70CDJM68icAjn5X3CnHSHozhuP4SngO72Rk4woWAHdXP4YBic9dHFQ9VojToA/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(查看</span><span style="color: black;">原文</span><span style="color: black;">请点击本文末尾左下角:</span><span style="color: black;">阅读原文</span><span style="color: black;">)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">所需的函数:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">base64_encode()函数</strong>是PHP中的一个内置函数,用于<span style="color: black;">运用</span>MIME base64对数据进行编码。MIME(多用途Internet邮件扩展)base64用于在base64中对字符串进行编码。base64_encoded数据比原始数据多占用33%的空间。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">file_get_contents()函数</strong><span style="color: black;">亦</span>是PHP中的一个内置函数,用于将文件读入字符串。该函数<span style="color: black;">运用</span>服务器支持的内存映射技术,从而<span style="color: black;">加强</span>了性能,使其<span style="color: black;">作为</span>读取文件内容的首选<span style="color: black;">办法</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">输入<span style="color: black;">照片</span>如下:</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/Lhm5kkN9JXMZt7AOTNDxOFRJh9H70CDJM68icAjn5X3CnHSHozhuP4SngO72Rk4woWAHdXP4YBic9dHFQ9VojToA/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><strong style="color: blue;">PHP代码程序如下:</strong>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;?php</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">// 获取图像并转换为字符串</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$img = file_get_contents(img/001.jpg);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">// 将图像字符串数据编码为base64</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$data = base64_encode($img);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">// <span style="color: black;">表示</span>输出</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">echo $data;</p>输出:<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/9j/4RUURXhpZgAASUkqAAgAAAAMAAABAwABAAAAgAIAAAEBAwABAAAAkAEAAAIBAwADAAAAng</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">AAAAYBAwABAAAAAgAAABIBAwABAAAAAQAAABUBAwABAAAAAwAAABoBBQABAAAApAAAABsBBQAB</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">AAAArAAAACgBAwABAAAAAgAAADEBAgAeAAAAtAAAADIBAgAUAAAA0gAAAGmHBAABAAAA6AAAACA</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">...</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">p2wFEq1LzJPMknzajI0ArU1yGkc+ITj2dJPPrPo+H/0DQ4y9NQ/Zgioyy+XLyk5cvqxxy5+b/9k=</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">举荐</span>学习:《PHP教程》</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">http://www.php.cn/course/list/29.html</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本篇<span style="color: black;">文案</span><span style="color: black;">便是</span>关于PHP<span style="color: black;">怎样</span>将图像转换为base64编码格式的<span style="color: black;">办法</span>介绍,简单易懂,<span style="color: black;">期盼</span>对需要的<span style="color: black;">伴侣</span>有所<span style="color: black;">帮忙</span>!</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/Lhm5kkN9JXPzKlOp6OyCsl0l2VVlZB6vVCSGQTaSmmTHZ1Lwiburribibf4p6yHSA9ia8DtuC96GcUGZiaUBfibtBicQA/640?wx_fmt=gif&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></span></a></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">-END-</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">声明:本文选自「&nbsp;</span><span style="color: black;">php中文网&nbsp;</span><span style="color: black;">」,搜索「 phpcnnew 」<span style="color: black;">就可</span>关注!</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/Lhm5kkN9JXPHtJf8zPadjF40glHh4GItJG3HkfOMv1zW65rXkl4XgaXlb5KK0VmoibOibFytgbkT9DwtBckWZ4mw/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/Lhm5kkN9JXPHtJf8zPadjF40glHh4GItDAOENNbqckeNx8dRrtE6yEDXq0XOeZ7WAatxWn7BjJicWzYdwVaeHTg/640?wx_fmt=gif&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p>




页: [1]
查看完整版本: PHP怎么样将图像转换为base64编码格式