最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert PNG Base-64 string to TIFF Base-64 string - Stack Overflow

programmeradmin0浏览0评论

I'm using a plugin that returns a PNG encoded base64 string, I cannot change it, I must work with it, but what I really need is the tiff encoded value (base-64). Is there a way of doing this?

I tried to create a canvas, load the png base64 and then used toDataURL('image/tiff') but after some research, I'd found that tiff is not supported as output of toDataURL().

Any suggestions?

I'm using a plugin that returns a PNG encoded base64 string, I cannot change it, I must work with it, but what I really need is the tiff encoded value (base-64). Is there a way of doing this?

I tried to create a canvas, load the png base64 and then used toDataURL('image/tiff') but after some research, I'd found that tiff is not supported as output of toDataURL().

Any suggestions?

Share Improve this question edited May 27, 2015 at 22:41 user1693593 asked May 27, 2015 at 16:24 MrCujoMrCujo 1,3334 gold badges44 silver badges85 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 5

As TIFF is not generally supported as target file-format in the browser, you will have to manually encode the TIFF file by building up the file-structure using typed arrays and in accordance with the file specifications (see Photoshop notes here). It's doable:

  • Get the raw RGBA bitmap from canvas (remember that CORS matters)
  • Use typed arrays with DataView view to be able to write various data at unaligned positions
  • Build up file header, define minimum set of TAGS and encode the RGBA data in the way you need (unpressed is simple to implement, or a simple RLE pression).
  • Construct the final file buffer. From here you have an ArrayBuffer you can transfer as bytes, optionally:
  • Convert to Blob with ArrayBuffer and tiff mime-type.
  • Convert to Data-URI using ArrayBuffer as basis

Update canvas-to-tiff can be used to save canvas as TIFF images (disclaimer: I'm the author).

To get an Data-URI using canvas-to-tiff you can simply do:

CanvasToTIFF.toDataURL(canvasElement, function(url) {
   // url now contains the data-uri.
   window.location = url;    // download, does not work in IE; just to demo
});

Although, I would remend using toBlob(), or if you want to give the user a link, toObjectURL() (instead of toDataURL).

Demo using Data-URI

var c = document.querySelector("canvas"),
    ctx = c.getContext("2d");

// draw some graphics
ctx.strokeStyle = "rgb(0, 135, 222)";
ctx.lineWidth = 30;
ctx.arc(200, 200, 170, 0, 2*Math.PI);
ctx.stroke();

// Covert to TIFF using Data-URI (slower, larger size)
CanvasToTIFF.toDataURL(c, function(url) {
  var a = document.querySelector("a");
  a.href = url;
  a.innerHTML = "Right-click this link, select Save As to save the TIFF";
})
<script src="https://cdn.rawgit./epistemex/canvas-to-tiff/master/canvastotiff.min.js">
</script>
<a href=""></a><br>
<canvas width=400 height=400></canvas>

Demo using Object-URL

var c = document.querySelector("canvas"),
    ctx = c.getContext("2d");

// draw some graphics
ctx.strokeStyle = "rgb(0, 135, 222)";
ctx.lineWidth = 30;
ctx.arc(200, 200, 170, 0, 2*Math.PI);
ctx.stroke();

// Covert to TIFF using Object-URL (faster, smaller size)
CanvasToTIFF.toObjectURL(c, function(url) {
  var a = document.querySelector("a");
  a.href = url;
  a.innerHTML = "Right-click this link, select Save As to save the TIFF";
})
<script src="https://cdn.rawgit./epistemex/canvas-to-tiff/master/canvastotiff.min.js">
</script>
<a href=""></a><br>
<canvas width=400 height=400></canvas>

发布评论

评论列表(0)

  1. 暂无评论