Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 捕获一个div并另存为jpeg_Javascript_Php_Jquery_Canvas - Fatal编程技术网

Javascript 捕获一个div并另存为jpeg

Javascript 捕获一个div并另存为jpeg,javascript,php,jquery,canvas,Javascript,Php,Jquery,Canvas,#cap是我想要捕捉的一个div。 我使用了ctx.drawImage($('#cap').get(0),0,0640480);window.open(canvas.getDataURL('image/jpeg')) 出现类型错误 如何将其发送到php以另存为a.jpeg 更新 不起作用帮助html2canvas不是一个函数而不是用javascript打开图像,只需将其发布到您的服务器,以便PHP可以接收它,将其保存在服务器上,然后将其发送回浏览器 我在这里找到了这个DivsToJPG()函数:

#cap
是我想要捕捉的一个div。 我使用了
ctx.drawImage($('#cap').get(0),0,0640480);window.open(canvas.getDataURL('image/jpeg'))

出现
类型错误
如何将其发送到php以另存为
a.jpeg

更新


不起作用帮助
html2canvas不是一个函数

而不是用javascript打开图像,只需将其发布到您的服务器,以便PHP可以接收它,将其保存在服务器上,然后将其发送回浏览器

我在这里找到了这个
DivsToJPG()
函数:


不要用javascript打开图像,只需将其发布到您的服务器,以便PHP可以接收它,将其保存在服务器上,然后将其发送回浏览器

我在这里找到了这个
DivsToJPG()
函数:

无法使用drawImage()方法在画布上绘制
元素。请查看库

例如:

下面是无法使用drawImage()方法在画布上绘制的

元素。请查看库

例如:


这是

,但也无法创建图像。如果请求对跨域图像进行编码,toDataURL将失败。也许最好的解决方法是(1)让页面将html和css上传到服务器,(2)让服务器将跨域图像下载到自身,(3)在服务器上使用“无头浏览器”加载收到的html和css,(4)将页面另存为图像。PhantomJS是一个很好的无头浏览器…或者只是使用SnagIt捕获屏幕:-),但也不能创建图像。如果要求对跨域图像进行编码,toDataURL将失败。也许最好的解决方法是(1)让页面将html和css上传到服务器,(2)让服务器将跨域图像下载到自身,(3)在服务器上使用“无头浏览器”加载收到的html和css,(4)将页面另存为图像。PhantomJS是一个很好的无头浏览器…或者只是使用SnagIt来捕获屏幕:-)注意html2canvas不会从div中捕获任何跨域图像元素。是的,它不会与跨域图像元素一起工作……但解决方法是使用代理。您提供的小提琴断开了html2canvas库的链接…下面是一个工作版本。请注意,html2canvas不会从div捕获任何跨域图像元素。是的,它不会与跨域图像元素一起工作……但解决方法是使用代理。您提供的小提琴已断开html2canvas库的链接……这是一个工作版本。
var DivsToJPG = function( parent ) {
    this.canvasSizeX = 0;
    this.canvasSizeY = 0;
    this.init = function( parent ) {
        this.images = parent.find('img');
        this.setSizes();
        this.createCanvas();
        this.drawImages();
        this.exportJPG();
    }

    this.setSizes = function() {
        for (var i = 0, l = this.images.length; i < l ; i++) {
            var currentImage = this.images.eq(i);
            var posX = currentImage.position().left;
            var width = currentImage.width();
            this.canvasSizeX = this.canvasSizeX > (posX+width) ? this.canvasSizeX : posX + width;
            //console.log(this.canvasSizeX);
            var posY = currentImage.position().top;
            var height = currentImage.height();
            this.canvasSizeY = this.canvasSizeY > (posY+height) ? this.canvasSizeY : posY + height;

         }
    }

    this.createCanvas = function() {
        this.canvas = document.createElement('canvas');
        this.canvas.id     = "exportCanvas";
        this.canvas.width  = this.canvasSizeX;
        this.canvas.height = this.canvasSizeY;
        this.ctx = this.canvas.getContext("2d");
        document.body.appendChild(this.canvas);
    }

    this.drawImages = function() {
        for (var i = 0, l = this.images.length; i < l ; i++) {
            var currentImage = this.images[i];
            var $currentImage = this.images.eq(i);
            this.ctx.drawImage(currentImage, $currentImage.position().left, $currentImage.position().top, $currentImage.width(), $currentImage.height());
        }
    }

    this.exportJPG = function() {
        var dataURL = this.canvas.toDataURL();
        this.img = document.createElement('img');
        this.img.id = "createdImage";
        this.img.src     = dataURL;
        document.body.appendChild(this.img);
    }

    this.init( parent );
}

var divsToJPG = new DivsToJPG($('#cap'));

$.ajax({
  type : "POST",
  url  : "make-image.php",
  data : { 
     imgBase64 : divsToJPG
  }
}).done(function(data) {

});
$img = base64_decode($_POST['imgBase64']);
 html2canvas(document.getElementById('DivToCapture'), {
      onrendered: function(canvas) {
         // document.body.appendChild(canvas);
       window.open(canvas.toDataURL('image/jpeg'));
     }
  });