I have an cropped image, Its src is like  data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAY..... When I hit this URL it is opening in my own system, not in other systems. How to MOve to folder hence save it?
            Asked
            
        
        
            Active
            
        
            Viewed 455 times
        
    0
            
            
        
        Saravanan DS
        
- 278
 - 3
 - 14
 
- 
                    1https://stackoverflow.com/questions/11511511/how-to-save-a-png-image-server-side-from-a-base64-data-string – TarangP Dec 26 '17 at 06:25
 - 
                    Possible duplicate of [How to save a PNG image server-side, from a base64 data string](https://stackoverflow.com/questions/11511511/how-to-save-a-png-image-server-side-from-a-base64-data-string) – Steve Byrne Dec 26 '17 at 06:56
 
1 Answers
1
            
            
        if (preg_match('/^data:image\/(\w+);base64,/', $data, $type)) {
//THIS FOR VALIDATION(CHECK VALID BASE64)
    $data = substr($data, strpos($data, ',') + 1);
    $type = strtolower($type[1]); // jpg, png, gif
    //GET FILE NAME AND EXTENSION
    if (!in_array($type, [ 'jpg', 'jpeg', 'gif', 'png' ])) {
        throw new \Exception('invalid image type');
    }
    //EXTENSION VALIDATION
    $data = base64_decode($data);
    //DECODE DATA
    if ($data === false) {
        throw new \Exception('base64_decode failed');
    }
} else {
    throw new \Exception('did not match data URI with image data');
}
file_put_contents("img.{$type}", $data);
//FINALLY GET  IMAGE
        TarangP
        
- 2,711
 - 5
 - 20
 - 41