简体   繁体   中英

How to convert html to base64 and save an image on a server with php?

I have a json file like this:

{"user":{"email":"user@test.com"},
 "screenshot":{"blobFile":"<!DOCTYPE html><html lang=\"en\">...</html>"}}

and I want to take a screenshot, using XMLHttpRequest sending data a PHP file.

In PHP file getting request like:

$data = json_decode(file_get_contents("php://input"), true);
$htmlStr = json_encode($data["screenshot"]["blobFile"]); // <!DOCTYPE html><html lang=\"en\">...

so far everything is ok but how to convert this string to the image file and save a server?

I've tried html2canvas in PHP file but not fire.

any ideas?

I'd try to use PhantomJS . It's headless browser, which allows to interact with web pages many ways including making screenshots. It will require some time to understand how to work with it, but it definitely will get a result. Although, PhantomJS sometimes is a headache if your page is quite complicated structured, written using some frameworks like ReactJS, AngularJS, etc.

What it does it renders HTML page with styles including scripts serverside. If you save not HTML string but exact URL with COOKIE and SESSION data and then reconstruct conditions which user had in an opened page when you did a screenshot, it'll do a job.

See example here Screen Capture on PhantomJS

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