Created
February 26, 2014 18:11
-
-
Save agustinhaller/9235020 to your computer and use it in GitHub Desktop.
Given any image url, it returns the base64 image
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 | |
// Given any image url, it returns the base64 image | |
function ParseImageURL() | |
{ | |
// $image_url = (isset($_REQUEST["image_url"]) && $_REQUEST["image_url"]!=null) ? $_REQUEST["image_url"] : null; | |
$image_url = "https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-prn1/t1/p526x296/1965014_10153859707455111_1422873058_n.jpg"; | |
// $image_url = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIWFRQBAwQEBgUGCgYGChAODA0QFA8OEAwNDQ8QDw0ODhANEA8MDA8PDxAQFA4VDw8PDhANDQ4ODRAQDw8UDhAMDA0MDP/AABEIACwALAMBEQACEQEDEQH/xAAaAAADAAMBAAAAAAAAAAAAAAAHCAkDBQYE/8QANBAAAQMDAwMCAwUJAQAAAAAAAQIDBAUGEQAHIQgSMRNBFCJxFzJCYYEVFiM0Q1FTcpEJ/8QAGgEAAgMBAQAAAAAAAAAAAAAAAwQABQYCAf/EACsRAAEDAwIEBQUBAAAAAAAAAAEAAgMREjEEIRNBUfBxgZGhwQUUImHRsf/aAAwDAQACEQMRAD8AfZtXA0wuFnR51FKrX3jd1N29s2uXRWFqbpVFgvVCUpsZV6baCtQSPdRAwB7kjUUUO7yvW/uszeebcc1Dji1r7I8NtwmPTI/4Gkk8ADyTgFSsnydIT6hkQq4+ATmn0z5zaweJ5BF66+gLcyltQbkiINXSyhC3YkBKlyEYGThJyFeM8YGST76Wh1okdu0jompdAWD8XA9eXosL/UxfuyJRbNLrNToUVCEv/COLbBClDCj83d7pxwpQ48k51b39FVFtNiq4IV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A8n11Rzw+0tA57HEJUUg+VISR9w641UV8RAyNx5f3Ca0Utkorg7HzS80y32r4tYUByS7T1xX2pDqWn1hTriCCkecdiO9RCMcFefODrOtNzbgMrWPNjgOnfxRPXYdTYhWtBaDhcKW0gqznJCQPJyfbV5A+xtO8LM6iPiPqe91BeqPLEdgA47ngDj/AFOrOXAVZDlPP009INtVWy7R3BRc9zwK1UqQFSWYj8UR3EvtYdbKVR1EoIPgqPgHyAddCMEVRnPOESbT/wDOW0KFV4lUpe5G5NOqEUgsyI1WipUkjGD/ACvPgHn3AOveGEIklFDY7oMtLaC/KjeFMve+qrW6myW5b1YqUeQXlZCg4pQjhZWCOCVeCQcg68LaLnCKdd6U7SrtiN2pEqFYt+GiX8eiXSnWRKS4XCtWFuNLwDns8Z7AEggDS3DZbZaKZ+e/0mPuZOJxCan/AEDr3+8rorW2LgWpRWKazclfmIa/rzHY63VfUhkD/gGg/bs7KK7WPca0Hv8A1f/Z"; | |
// First detect if it's a base64 image | |
if(strpos($image_url,'data:image/') !== false) | |
{ | |
// It's a base64 image | |
$image_parts = explode(";base64,", $image_url); | |
$image_type_aux = explode("image/", $image_parts[0]); | |
$image_type = $image_type_aux[1]; | |
$image_base64 = $image_url; | |
} | |
else | |
{ | |
// It's not a base64 image, it's a regular url | |
$file_aux = file_get_contents($image_url); | |
$headers_aux = get_headers($image_url); | |
$content_type = parseHeaders($headers_aux, "Content-Type"); | |
$image_type_aux = explode("image/", $content_type); | |
$image_type = $image_type_aux[1]; | |
$image_base64 = 'data:image/'.$image_type.';base64,'.base64_encode($file_aux); | |
} | |
$is_image = is_valid_image_type($image_type); | |
$result = "ERROR"; | |
if($is_image) | |
{ | |
$result = "OK"; | |
} | |
// Send response to the user | |
$response = array("result"=>$result, | |
"image_type"=>$image_type, | |
"image_base64"=>$image_base64); | |
$json_response = json_encode($response); | |
// var_dump($response); | |
echo($json_response); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment