php 抓取网页生成图片(PHPHTML页面jshtmlcanvas2直接在页面生成的base64的图片)
优采云 发布时间: 2021-12-03 14:31php 抓取网页生成图片(PHPHTML页面jshtmlcanvas2直接在页面生成的base64的图片)
一、页面介绍js,我直接用网上的
HTML 页面 js
$(document).ready(function (){
html2canvas($(".ask_con")).then(function (canvas) {
window.html_canvas = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
var pHtml = "/span+window.html_canvas+span class=";
$('#html2canvas').html(pHtml);
$('.ask_con').remove();
ceshi(window.html_canvas);
});
});
/**
* 把图片文件流保存到本地
*/
function ceshi(path){
//alert(path);
$.ajax({
url:'{:U("Text/getPath")}',
data:{img:path},
type:'post',
dataType:'json',
success:function(data){
//alert(data);
}
});
}
htmlcanvas2直接在页面上生成的base64图片需要处理保存为jpg/png类型
下面是PHP代码
/*把base64类型的图片保存到本地*/
function getPath(){
$base64_string = I('img');
$url = '/Public/WEB_ROOT/images/'.time().rand(1000,9999).".png";
$base64_string= explode(',', $base64_string); //截取data:image/png;base64, 这个逗号后的字符
$data= base64_decode($base64_string[1]);//对截取后的字符使用base64_decode进行解码
file_put_contents('./'.$url, $data); //写入文件并保存
$this->ajaxReturn($url);
}
要截图的页面收录的图片必须是本地的,不能跨域