🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 图片转base64 ``` <pre class="calibre15">``` <span class="token"><</span><span class="token">?</span>php namespace app\common<span class="token1">;</span> <span class="token2">class</span> <span class="token3">Pichandle</span> <span class="token1">{</span> <span class="token6">/** 公共---- 读取本地图片转成base64格式 * @param $filePath 本地相对路径 */</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">pictobase64</span><span class="token1">(</span>$filePath<span class="token1">)</span><span class="token1">{</span> $img_base64 <span class="token">=</span> <span class="token4">''</span><span class="token1">;</span> $fullpath <span class="token">=</span> <span class="token3">app</span><span class="token1">(</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">getRuntimePath</span><span class="token1">(</span><span class="token1">)</span><span class="token1">.</span>$filePath<span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">file_exists</span><span class="token1">(</span>$fullpath<span class="token1">)</span><span class="token1">)</span><span class="token1">{</span> $imageInfo <span class="token">=</span> <span class="token3">getimagesize</span><span class="token1">(</span>$fullpath<span class="token1">)</span><span class="token1">;</span> <span class="token6">// 获取图片类型和大小等信息</span> $filestream <span class="token">=</span> <span class="token3">fopen</span><span class="token1">(</span>$fullpath<span class="token1">,</span><span class="token4">'r'</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span><span class="token1">(</span>$filestream<span class="token1">)</span><span class="token1">{</span> $filesize <span class="token">=</span> <span class="token3">filesize</span><span class="token1">(</span>$fullpath<span class="token1">)</span><span class="token1">;</span> $content <span class="token">=</span> <span class="token3">fread</span><span class="token1">(</span>$filestream<span class="token1">,</span>$filesize<span class="token1">)</span><span class="token1">;</span> $file_content <span class="token">=</span> <span class="token3">chunk_split</span><span class="token1">(</span><span class="token3">base64_encode</span><span class="token1">(</span>$content<span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">switch</span> <span class="token1">(</span>$imageInfo<span class="token1">[</span><span class="token5">2</span><span class="token1">]</span><span class="token1">)</span> <span class="token1">{</span> <span class="token6">//判读图片类型</span> <span class="token2">case</span> <span class="token5">1</span><span class="token1">:</span> $img_type <span class="token">=</span> <span class="token4">"gif"</span><span class="token1">;</span> <span class="token2">break</span><span class="token1">;</span> <span class="token2">case</span> <span class="token5">2</span><span class="token1">:</span> $img_type <span class="token">=</span> <span class="token4">"jpg"</span><span class="token1">;</span> <span class="token2">break</span><span class="token1">;</span> <span class="token2">case</span> <span class="token5">3</span><span class="token1">:</span> $img_type <span class="token">=</span> <span class="token4">"png"</span><span class="token1">;</span> <span class="token2">break</span><span class="token1">;</span> <span class="token1">}</span> $img_base64 <span class="token">=</span> <span class="token4">'data:image/'</span> <span class="token1">.</span> $img_type <span class="token1">.</span> <span class="token4">';base64,'</span> <span class="token1">.</span> <span class="token3">str_replace</span><span class="token1">(</span><span class="token5">PHP_EOL</span><span class="token1">,</span> <span class="token4">''</span><span class="token1">,</span> $file_content<span class="token1">)</span><span class="token1">;</span><span class="token6">//合成图片的base64编码</span> <span class="token1">}</span> <span class="token3">fclose</span><span class="token1">(</span>$filestream<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">return</span> $img_base64<span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ```