Skip to content

Instantly share code, notes, and snippets.

@NrI3
Created June 13, 2015 16:51
Show Gist options
  • Save NrI3/6d877b10afa6fb41f53c to your computer and use it in GitHub Desktop.
Save NrI3/6d877b10afa6fb41f53c to your computer and use it in GitHub Desktop.
Codifica una imagen a base 64
<?php
//
// By Jsn
//
// Get file
function getFileData($file){
$o = fopen($file,'r');
$r = fread($o,filesize($file));
fclose($o);
return $r;
}
// Encode image file as base64
function encodeBase64($data){
$header = $r = "";
$header = substr($data,0,10);
if (substr($header,6,4)=="JFIF"){
$header = 'data:image/jpg;base64,';
goto jump;
}
if (substr($header,0,3)=="GIF"){
$header = "data:image/gif;base64,";
goto jump;
}
if (substr($header,1,3)=="PNG"){
$header = "data:image/png;base64,";
}
jump:
return $header.base64_encode($data);
}
// Return a Image as Base64
function imgGetData($file){
return encodeBase64(getFileData($file));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment