2014-10-04 2 views
2

유스 케이스는 데이터베이스에 크기가 조정 된 svg를 저장합니다. 이 질문에는 2 부분이 있습니다.브라우저 기반 로컬 파일 시스템 대 SVG base64 데이터 문자열

  1. 로드 된 이미지 파일에서 데이터 문자열을 가져 오는 방법.

  2. 캔버스를 통해 데이터 문자열 이미지의 크기를 조정하는 방법.

내가 window.URL.createObjectURL 다음과 같은 코드를 사용 할 수있었습니다() 멋진 문자열을 얻을 수 있지만 각도에서, 내가 잘못 혼합 상태 (UI-라우터)가있을 수 있습니다. 그래서 CONSOLE.LOG에 의해 생성 된 링크를 클릭하면

function createObjectURL() { 
       console.log('file',file); 
       return (window.URL) ? window.URL.createObjectURL(file) : window.webkitURL.createObjectURL(file); 

      }; 

      function revokeObjectURL(url) { 
       console.log('url',url); 
       return (window.URL) ? window.URL.revokeObjectURL(url) : window.webkitURL.revokeObjectURL(url); 


      }; 

      (function myUploadOnChangeFunction() { 

         console.log('file',file); 
         var src = createObjectURL(file); 
       console.log('src',src); 

       var image = new Image(); 
       console.log('image',image); 

       src = src.toLowerCase().replace(/'+/g, '').replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "-").replace(/^-+|-+$/g, ''); 

         image.src = src; 
         console.log('image.src',image.src); 
       api.testMe = image.src; 
       return api.testMe; 
       // Do whatever you want with your image, it's just like any other image 
         // but it displays directly from the user machine, not the server! 


      })(); 

방울 : HTTP % 3A // % 3A9000/66705b7a-148A-4921-a48b-090fd2ceba52 그것이 제공하는 링크입니다.

+0

[잉크 스케이프 (http://inkscape.org/doc/tracing/tutorial- tracing.html) 또는 [Adobe Indesign] (http://www.adobe.com/inspire/2013/09/exporting-svg-illustrator.html)이 도움이 될 수 있습니다. Havent는 좋은 브라우저 기반 변환기를 보았지만 https://cloudconvert.org/png-to-svg를 시도했습니다. –

+0

건배 Alvin, 내 주요 사용 사례는 클라이언트 측 AngularJS CMS 앱에 업로드 크기를 조정하고 저장하는 것입니다. 귀하의 링크는 그것이 가능하다는 것을 보여 주며, 분명히 우리가 화면에서 볼 수있는 어떤 이미지라도 바로 그 품질로 저장 될 수 있습니까? 그래서 html5 캔버스 도구를 사용하여 이미지를 조작하고 최종적으로 벡터 svg base64 문자열을 클라우드 데이터베이스 문서에 업로드 할 수 있습니다. – irth

+0

Emmet 인코딩/디코딩 방식을 이해하는 것이 유리할 수 있습니다. http://docs.emmet.io/actions/base64/ –

답변

-1

먼저 얻는 데이터 URI : 다음 (https://github.com/nicam/js-resize/blob/master/resize.js 찍은)의 크기를 조정

var reader = new FileReader(); 
     console.log('reader',reader); 
     reader.onload = (function(lefile){ 
       return function(e){ 
        console.log("Filereader done"); 
        console.log('-- RESULT',e.target.result, '-- NAME:', lefile.name); 

:

var resize = function (image) { 
    mainCanvas = document.createElement("canvas"); 
    mainCanvas.width = 1024; 
    mainCanvas.height = 768; 
    var ctx = mainCanvas.getContext("2d"); 
    ctx.drawImage(image, 0, 0, mainCanvas.width, mainCanvas.height); 
    size = parseInt($('#size').get(0).value, 10); 
    while (mainCanvas.width > size) { 
     mainCanvas = halfSize(mainCanvas); 
    } 
    $('#outputImage').attr('src', mainCanvas.toDataURL("image/jpeg")); 
+1

-1 질문에 명시한대로 "캔버스를 통해 이미지를 svg로 변환하는 방법"이 없습니다 –

+0

@ tnt-rox 좋은 지적입니다. 캔버스가 이미지를 변환하는 데 필요한 것이라고 생각하는 것이 내게 무지했습니다. FileReader는 이미지를 base64 데이터 문자열로 변환하는 반면 canvas는 크기를 조정하는 데 사용됩니다. 관심을 가져 주셔서 감사합니다. 나는 그 문제를 해결할 것이다. – irth

관련 문제