PNG Converter

by musicreader

HTML

Adjust SVG FILE<br/>
<input type="file" id="file_upload"/>

<img id="result"/>

JavaScript

function handleFileSelect(evt) {
  console.log("Start Uploading Files");
  var files = evt.target.files; // FileList object

  var counter = 0;

  // files is a FileList of File objects. List some properties.
  var output = [];
  for (var i = 0, f; f = files[i]; i++) {
    var reader = new FileReader();
    reader.onload = (function() {
      var text = reader.result;
      var viewbox_test=new RegExp(/(viewBox=)/ig);
      var height_patt=new RegExp(/(height=)["']*[0-9.]*['"]/ig);
      var width_patt=new RegExp(/(width=)["']*[0-9.]*['"]/ig);
      text=text.replace(/<svg[^>]*>/ig, function(svg_tag) {
      	console.log("TAG BEFORE:"+svg_tag);
      	if(!viewbox_test.test(svg_tag)){
        	var height_attr = height_patt.exec(svg_tag);
          var width_attr= width_patt.exec(svg_tag);
          var height=height_attr[0].substring(height_attr[1].length+1,height_attr[0].length-1);
          var width=width_attr[0].substring(width_attr[1].length+1,width_attr[0].length-1);
          //console.log("no ViewBox:"+width+"x"+height);
          svg_tag=svg_tag.substring(0,svg_tag.length-1)+" viewBox=\"0 0 "+width+" "+height+"\">";
        }
        console.log("TAG AFTER:"+svg_tag);
        return svg_tag;
      });
      
      function processTag(tag_color) {
          var fill_test=new RegExp(/(fill:)[^"';]*(#4e0047)/ig);//2b3654 //4e0047
      		var stroke_test=new RegExp(/(stroke:)[^"';]*(#4e0047)/ig);
      		var classesToAdd=[];
        	if(fill_test.test(tag_color)){
            classesToAdd[classesToAdd.length]="svg_fill";
          }
          if(stroke_test.test(tag_color)){
            classesToAdd[classesToAdd.length]="svg_stroke";
          }     
          if(classesToAdd.length>0){
          	console.log("TAG BEFORE:"+tag_color);
            console.log("classes to add:"+classesToAdd);
          	var classStart="class=\"";
          	var classIndex=tag_color.indexOf(classStart);
            if(classIndex>-1){
            	var...