有没有办法在PHP中将html div内容转换为jpg / png图像?
<div class="canvas-object" style="background-color: rgb(247, 213, 183);">
<img id="img1" class="img" src="logo.png" style="width:110px; height:54px;">
<div>This is a sample text</div>
</div>
谢谢!
答案 0 :(得分:2)
是的我已经完成了这个使用html2canvas功能,但这非常复杂
从阅读文档
下载Html2canvashttps://github.com/niklasvh/html2canvas/releases
您需要有2页 a)一个带有Javascript的HTML页面 b)处理来自此页面的请求的另一个页面(如果你传递请求,则更好)
html2canvas($("#droppable2") , {
onrendered: function(canvas) {
dataURL = canvas.toDataURL("image/png");
$(image_id).attr('src','images/ajax_loader.gif'); //show a loader while the request is being forwared
$.ajax({
type: "POST",
dataType: "json",
url: "saveimage.php", //second file
data: {data:dataURL},
success: function(data) {
image = data.data;
source = 'template_images/'+username+"/"+magz_name+"/"+image+"?a="+Math.random(); // image name
$(image_id).attr('src',source); // set the source of new image with dynamically generated image
});
});
在第二页
使用base64_decode()函数获取此图像的内容并将其放入新文件
<?php
$data = $_REQUEST['data'];
$rawImage =$data;
$removeheaders =substr($rawImage,strpos($rawImage,",")+1);
$decode = base64_decode($removeheaders);
$fopen =fopen('newfile.png','wb');
fwrite($fopen,$decode);
$arr = array('data' => 'newfile.png','username' => $username );
echo json_encode($arr);
?>