4

我生成画布并将其传递给 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);
                            });
                    }
                });
 });

图像.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>';
?>

下载.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/

4

1 回答 1

7

您可能应该是base64_decode()数据 URL。它甚至在 URL 本身中说:data:image/png;base64,...

$decoded = base64_decode(str_replace('data:image/png;base64,', '', $image));
于 2013-03-30T17:22:15.133 回答