动态地将图像src分配给画布

时间:2017-04-02 18:33:24

标签: javascript php canvas

我正在尝试将php变量初始化为从数据库循环获取的图像源。这是我用来做的代码片段

<?php foreach($load_preview as $item): ?>
    <div class="store-item">
        <?php var imgSrc = base_url()."thumbs/".$item["filename"]; ?>//having syntaxt error at this line
            <a href="<?php echo site_url()."/"."accessories/preview/".$item["id"];?>">
                <canvas width="450" height="450" id="canvas">Sorry, no canvas available</canvas>    
            </a>    
        </div>   
    <?php endforeach; ?>

我将以上内容重新分配到javascript代码中以绘制用于缩放的画布,如图所示

/**
 * START
 */
var img = document.createElement('img');
img.onload = function() {
    iw = 500;
    ih = 500;
    canvas.width = (iw / 2 - 1.5)|0; //compensate for rounding errors
    canvas.height = (ih / 2 - 1.5)|0;
    doCanvas();
    canvas.addEventListener('mouseout', doCanvas, false);
    canvas.addEventListener('mousemove', move, false);
}
img.src = "<?php echo imgSrc ?>";//retrieve the assigned variable here in the javascript assignment


</script>
这对我来说有点让人困惑。有人可以帮忙吗

1 个答案:

答案 0 :(得分:0)

这一行错了:

<?php var imgSrc = base_url()."thumbs/".$item["filename"]; ?>

可能应该是这样的:

var imgSrc = '<?php echo base_url()."thumbs/".$item["filename"]; ?>';