php image to base64

$image_file is the image path

function base64EncodeImage ($image_file) {

    $type=getimagesize($image_file);//Get the size, type, etc. of the image
    $content=file_get_contents($image_file);
    $file_content=chunk_split(base64_encode($content));//base64 encoding
    switch($type[2] ){//Interpretation of image type
        case 1:$img_type="gif";break;
        case 2:$img_type="jpg";break;
        case 3:$img_type="png";break;
    }
    $img='data: image/'.$img_type.';base64,'.$file_content;//Base64 encoding of composite image
    return $img;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325139379&siteId=291194637