PHP: Convert Image to Base64 Encoding ?>

PHP: Convert Image to Base64 Encoding

Simple Image class, convert image to Base64 encoding.

class Image {
	public static function toBase64($image_location) {
		$image_path = $image_location;
		$image_type = pathinfo($image_path, PATHINFO_EXTENSION);
		$image_data = file_get_contents($image_path);
		$image_base64 = 'data:image/' . $image_type . ';base64,' . base64_encode($image_data);

		return $image_base64;
	}
}

Usage:

$b64 = Image::toBase64('https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png');
Base64
Base64

Leave a Reply

Your email address will not be published. Required fields are marked *

10 + 17 =