Created
June 13, 2015 16:51
-
-
Save NrI3/6d877b10afa6fb41f53c to your computer and use it in GitHub Desktop.
Codifica una imagen a base 64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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