简体   繁体   English

根据图像的路径将SVG转换为PNG

[英]Convert an SVG to PNG based on the path of the image

Is it possible to convert an SVG image to the PNG format in an angular project?是否可以在角度项目中将 SVG 图像转换为 PNG 格式?

I found this library save-svg-as-png , but it requires the id of an svg canvas.我找到了这个库save-svg-as-png ,但它需要一个 svg 画布的 ID。 Whereas in my code, I am displaying the SVG image with the img tag:而在我的代码中,我使用img标签显示 SVG 图像:

<img src="path/to/mysvg/mysvg.svg">

Is there a way to access the inline svg that is inside the image, or another library where it is possible to provide the url of the svg image instead of the id of the canvas?有没有办法访问图像内的内联 svg,或者其他可以提供 svg 图像的 url 而不是画布 id 的库?

PS: I found some libraries like svg2png and convert-svg-to-png that takes the path of the svg as a parameter, but they both depend on nodejs. PS:我发现了一些库,如svg2pngconvert-svg-to-png ,它们以 svg 的路径为参数,但它们都依赖于 nodejs。

Working with angular-svg-icon saved the day.使用angular-svg-icon拯救了这一天。

In my HTML, I displayed my svg image this way:在我的 HTML 中,我以这种方式显示了我的 svg 图像:

<svg-icon #mySVGImg src="path/to/mysvg/mysvg.svg" [svgStyle]="{ 'width.px':140, 'height.px':140 }"></svg-icon>

and in my typescript, I called the saveSvgAsPng method like this:在我的打字稿中,我像这样调用了saveSvgAsPng方法:

import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import * as svg from 'save-svg-as-png';
// Other imports

@Component({
  //....
})
export class MyTSPage implements OnInit {

    @ViewChild("mySVGImg", { read: ElementRef }) mySVGImg: ElementRef;

    myConvertMethod() {
        svg.saveSvgAsPng(this.mySVGImg.nativeElement.querySelector("svg"), "pngtest.png");
    }
}

Calling myConvertMethod method saved a new png file with the name pngtest.png调用myConvertMethod方法保存了一个名为pngtest.png的新 png 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM