简体   繁体   中英

How to save image to folder in php

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM