lukechilds/merge-images

Use existing elements rather than URL

Closed this issue ยท 9 comments

Great script! I have images that are generated from SVG, not external URLs - so I have img elements I'd like to use with this - I tried converting your script to one that could use existing elements, without much luck. Do you have any tips?

thanks again!
Stacey

Hi Stacey,

merge-images just requires an array of any valid image source for an image. So you should be able to just pass your generated images straight in.

What format are they in, data URI?

Thanks! They are actually SVG that I am generating Data URI pngs for.

So you already have the SVG as a PNG data URI and you're trying to compose them together with merge-images? Or you have SVG markup that you're trying to feed into merge-images?

@mspanish If you have your SVG as markup (which isn't a valid image source) then you can easily convert them to a data URI like so:

const svgToURI = svg => 'data:image/svg+xml;base64,' + btoa(svg);

This function will convert SVG markup to a data URI which is a valid image source that can then be used by merge-images :

const svgMarkup = `<svg xmlns="http://www.w3.org/2000/svg" height="100" width="100">
  <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>`;

const dataURI = svgToURI(svgMarkup);

mergeImages([
  { src: dataURI, x: 25, y: 25 },
  { src: dataURI, x: -25, y: -25,}]
).then(console.log);
// data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAF8UlEQVR4Xu2d243UMBRA71QASPwDFQAFIKACoAJAFABUAFQA/CMBFSxUwKMBlgqAfySggkEnih3Hm8nkMRnf+NrSanZ3vBPHx/fp6+xmK7KVDNsHEbnXvq8XIvJc+61ucgXCxD8QkfdtAtdF5FQzlKyB/BWRWyLyvSEADKCobVkDYdY7CDwVkVdaiWQPhIln9qFQNwQHKfmpEYoJIEw8qutLQ+CziNwuQBLOQAcBlQbejIR0SAkOGI6YqmYKSIeUXBARbIqaZgpIh5SoCxbNAYmkBE/rihrxEBFzQJj8yyLyq6HwUETeaYFiEsgTEXndEFBl3E0CiRKPqtSWSSAIx6ato7AjKiJ3s0CiyF2NHTELhI0RfN66YVIwLcmbWSBRFlhNWt4skA47EpmVNMJSgDTzXoCkWYPNVaMAkXQ8gXzSZlpCIk+rAEm6FM9uWhUgqYFEVSkq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UZqXwF7fveS9JdAsjU8SIUlWf3SWFQOvWmxv6dJiB/nDfrvxl7Nxn01wTE/3OyLP9L2cDFUoAMnKhjddMCBC8N02Haw+L+C5BjLf2B19EChICTLHqVp1HzrwoGTuIhu2kB4vdktFWmH3Kyh3xWATJklo7YRwsQEpcE6EVlHRF+36WKl1XPjhYJKUC0AiGFzOlYq02LhDD/JXWiKDAsQJSpLIbDfkm1ybWGk05LqVRNKssXTVoODjUB8emTsoW7lPyN+9yWg2V1T0SThICPst2qmuVkx1MpxzFeX29tQEj0VoV5uVSzj10S2oB4O2I1QNQGhAVFgWNVIWmx+kQjEL834hNcY+V+xf01AqGWmEqgqlmTEo1A4OCNuzUp0QqEElQyKOakRCsQQPjSUktSohkIni8Jx8rjslKArRkIHPxeu5UssHYgrbQ8hgWvK9fzhdzsGoC0DLwP5Vcca/QNfQ1AGH/r4QY5Jx7/A3CGgzKuhyjPAAAAAElFTkSuQmCC

Which outputs this PNG of the two SVGs merged together:

Does that solve your problem?

That is super cool, I'll try it out today and post back. Thanks a ton!

@mspanish did you get this sorted? Can I close the issue?

Ah sorry will test shortly and post back.

looks to be working just fine - thanks again.

Great, no problem ๐ŸŽ‰