html2canvas的div截图.发送到php,保存:损坏的图像



生成画布并将其传递给php,因此:

$('body').on('click','#save_image',function(){
                html2canvas($('.myImage'), {
                    onrendered: function(canvas) {
                        //$('.imageHolder').html(canvas);
                            var dataURL = canvas.toDataURL("image/png");
                           // $('.imageHolder').append('<img src="'+dataURL+'" />');
                            $('.imageHolder').html('Generating..');
                            $.post('image.php',{image: dataURL},function(data){
                                $('.imageHolder').html(data);
                            });
                    }
                });
 });

image.php:

<?
    $image = $_POST['image'];
    echo "<img src='$image' alt='image' />";
    $decoded = str_replace('data:image/png;base64,','',$image);
    $name = time();
    file_put_contents("/home/toni005/public_html/toniweb.us/div2img/" . $name . ".png", $decoded);
    echo '<p><a href="download.php?img='.$name.'.png">Download</a></p>';
?>

download.php:

    <? $file = $_GET['img'];
header('Content-Description: File Transfer');
header("Content-type: image/jpg");
header("Content-disposition: attachment; filename= ".$file."");
readfile($file);
?>

事情是,图像生成,当我点击下载链接下载是格式化的,但图像不能打开(似乎已损坏)

我错过了什么?

可以在这里测试:http://toniweb.us/div2img/

您可能应该base64_decode()数据URL。甚至连URL本身都写着:data:image/png;base64,...

$decoded = base64_decode(str_replace('data:image/png;base64,', '', $image));

最新更新