javascript - 将 Canvas 图像表示为其他页面中的实际图像

标签 javascript php html

这是我的 Canvas 图像,位于名为 canvas.php 的页面中

  <html>
    <body>
    <style type="text/css">
    table
    {
    border=5;
    }
    </style>
    <p><canvas id="canvas" style="border:2px solid black;" width="500" height="500"></canvas>
    <script>
    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");
    var data = "<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'>" +
                 "<foreignObject width='100%' height='100%'>" +
                   "<div xmlns='http://www.w3.org/1999/xhtml' style='font-size:40px'>" +
                     "<table><tr><td>HI</td><td>Welcome</td></tr><tr><td>Hello</td><td>World</td></tr> </table>" +
                   "</div>" +
                 "</foreignObject>" +
               "</svg>";
    var DOMURL = self.URL || self.webkitURL || self;
    var img = new Image();
    var svg = new Blob([data], {type: "image/svg+xml;charset=utf-8"});
    var url = DOMURL.createObjectURL(svg);
    img.onload = function() {
        ctx.drawImage(img, 0, 0);
        DOMURL.revokeObjectURL(url);
    };
    img.src = url;
    </script>
    </body>
    </html>

我想将此图像作为其他页面中的实际图像......基本上我希望能够在 otherpage.php

中执行此操作
<img src="www.mysite.com/canvas.php" />

此图像会不时动态变化..所以我不想保存它...只想将其显示在没有 Canvas 的其他页面中

最佳答案

您不能在 PHP 文件中使用 canvas 并使用 img 调用该 PHP,

  • Canvas 与 Javascript 兼容 PHP 文件头必须设置为
  • image/png 或 image/jpg header( "Content-type: image/png");

“iframe 是没有问题的”好吧!

您唯一的解决方案是使用 PHP image , PHP: imagecreate

创建文件myImg.php:

<?php
header( "Content-type: image/png" ); // set this php file as png file
$my_img = imagecreate( 200, 200 ); // image width & height
$background = imagecolorallocate( $my_img, 245,245,245 ); // set the background color
$text_colour = imagecolorallocate( $my_img, 255,66,121 ); // text color
imagestring( $my_img, 10, 50, 90, "Hello World", $text_colour );  // Our Hello World text!
imagepng( $my_img ); // outputs PNG image 
?>

index.html

<img src="myImg.php" /> // this will output the image from myImg.php

查看这些资源:

关于javascript - 将 Canvas 图像表示为其他页面中的实际图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19037254/

相关文章:

javascript - 如何使用 javascript 将 json 对象附加到 html 正文? (没有 jQuery)

php - 找到内存限制和(upload_max_filesize & post_max_size)的最佳平衡

php - Laravel MySQL whereIn 关闭不按预期工作

javascript - 使用 Ajax 更新 Rails 中的元素

javascript - 如何使用 javascript 在任何驱动器中创建文本文件

javascript - D3-如何在多线图上画点? (遍历数组)

javascript - POST 没有获取所有表单变量

html - 背景颜色过渡不起作用

javascript - 如何在Vue3中播放mp3文件

javascript - 如何为多个元素制作倒计时时钟?