更改img src后html2canvas保存图像

时间:2015-11-12 08:24:00

标签: javascript jquery html2canvas

我使用html2canvas将html保存为图像。 我的HTML代码如下

<div id="target" class="center-block imgStruc" style="width:92%;">
    <div class="center-block eventName makeMeDraggable box">
        <div class="text">                      
            <div class="text1">Test Data</div>
        </div>
    </div>
    <div class="imgCont clearfix"><img src="http://mydomainname/image.jpg" alt="" class="img-responsive" id="changeImg"></div>
</div>

<div class="col-sm-6">
    <input type="submit" value="Save &amp; Continue »" onClick="snap_onclick()"/>
    <form method="POST" enctype="multipart/form-data" action="savecard.php" id="myForm">
        <input type="hidden" name="img_val" id="img_val" value="" />
    </form>
</div>

我的javascript如下

function snap_onclick() {
    $('#target').html2canvas({
        onrendered: function (canvas) {
            $('#img_val').val(canvas.toDataURL("image/png"));
            document.getElementById("myForm").submit();
        }
    });
}

已创建目标div的图像。但是当我使用以下jquery代码

更改changeImg的图像src时
function changeImage()
{
    $('#changeImg').attr('src','http://mydomainname/path-to-image/image.jpg');
}

然后单击“保存”按钮,它仅创建带有文本部分的图像,更改的图像未来。

save.php如下

<?php
//Get the base-64 string from data
//echo $_POST['img_val'];

$filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);

//Decode the string
$unencodedData=base64_decode($filteredData);

//Save the image
file_put_contents('img.png', $unencodedData);
?>
<h2>Save the image and show to user</h2>
<table>
    <tr>
        <td>
            <a href="img.png" target="blank">
                Click Here to See The Image Saved to Server</a>
        </td>
        <td align="right">
            <a href="index.php">
                Click Here to Go Back</a>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <br />
            <br />
            <span>
                Here is Client-sided image:
            </span>
            <br />
<?php
//Show the image
echo '<img src="'.$_POST['img_val'].'" />';
?>
        </td>
    </tr>
</table>
<style type="text/css">
body, a, span {
    font-family: Tahoma; font-size: 10pt; font-weight: bold;
}
</style>

新图片位于同一个域中。

1 个答案:

答案 0 :(得分:0)

好的,不要像以前一样添加按钮.... 你有这样的HTML吗?(我在网上与其他人合影......)

<div class="col-md-3 col-sm-4 col-xs-4 single-right-grid-left"> 
    <a onclick="changeImage()"> <img style="width:50px;" id="drag1" class="drag" alt="" src="http://beblex.it/wp/wp-content/uploads/2014/05/Online1.jpg">
    </a> 
</div>

<div id="target" class="center-block imgStruc" style="width:92%;">
    <div class="center-block eventName makeMeDraggable box">
        <div class="text">                      
            <div class="text1">Test Data</div>
        </div>
     </div>
    <div class="imgCont clearfix">
        <img src="http://www.bottegamonastica.org/wp-content/uploads/Finalmente-Online.jpg" alt="" class="img-responsive" id="changeImg" style="width:50px;">
    </div>
</div>

<div class="col-sm-6">

    <input type="submit" value="Save &amp; Continue »" onClick="snap_onclick()"/>
    <form method="POST" enctype="multipart/form-data" action="savecard.php" id="myForm">
        <input type="hidden" name="img_val" id="img_val" value="" />
    </form>

</div>

js与粘贴....相同。