Showing posts with label base64_encode. Show all posts
Showing posts with label base64_encode. Show all posts

Wednesday, 27 July 2016

Encode Image in PHP as a Data Source

Use this simple php function to encode image into a data source for easy inline of the image data. This method will make the image load faster in webpage.

function encodeImage($filepath) {
    $filecontents = file_get_contents($filepath);
    $base64 = base64_encode($filecontents);
    $imagetype = exif_imagetype($filepath);
    $mime = image_type_to_mime_type($imagetype);
    return "data:$mime;base64,$base64";
}

Sunday, 17 July 2016

PHP Function to encode and decode string in PHP

Simple php function to encode and decode a string in php. This function uses simple base64_encode to encode the string.

function base64url_encode($mytext) {
    $base64 = base64_encode($mytext);
    $base64url = strtr($base64, '+/=', '-_,');
    return $base64url;
}

function base64url_decode($mytext) {
    $base64url = strtr($mytext, '-_,', '+/=');
    $base64 = base64_decode($base64url);
    return $base64;
}