ๆฅๅฐไธๅ case ้่ฆๆๅ็่ฝ็บ base64 ๅณๅบ,้ ๆๅฏซไบๅ class ๅๆ้ไปถไบ。
<?php
class ImageToBase64
{
public function encode64($source = null, $fileName = null)
{
$source = file_get_contents($source);
$type = pathinfo($fileName, PATHINFO_EXTENSION);
$content = 'data:image/' . $type . ';base64,' . base64_encode($source);
return $content;
}
public function decode64($source = null)
{
$content = null;
if (preg_match('/data:([^;]*);base64,(.*)/', $source, $matches)) {
header('Content-Type: '.$matches[1]);
$content = base64_decode($matches[2]);
}
return $content;
}
}