캔버스를 사용하여 이미지를 자르고 요소로로드 할 base64 문자열을 얻으려고합니다. this post에서 예제로 작업하고 있었지만, .toDataURL()을 실행하면 base64가 잘리고 유효하지 않습니다. 난 단지 base64로 552 개 문자를 받고 오전 toDataURL을 내 jsfiddle캔버스로 이미지 자르기
var image = new Image(),
canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d');
image.onload = function(){
ctx.drawImage(image,
70, 20, // Start at 70/20 pixels from the left and the top of the image (crop),
50, 50, // "Get" a `50 * 50` (w * h) area from the source image (crop),
0, 0, // Place the result at 0, 0 in the canvas,
50, 50); // With as width/height: 100 * 100 (scale)
}
image.src = 'https://www.google.nl/images/srpr/logo3w.png';
var _img = document.getElementById('base64');
_img.innerHTML += canvas.toDataURL();
. 자른 이미지를 저장하고 이미지를 base64로 디코딩하면 약 3.8k 문자가됩니다.
제가 잘못하고있는 것을 볼 수 없습니다 - 어떤 아이디어입니까?
결과를 [' toDataURL'] (https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL)은 HTML이 아닌 _ ** 이미지 데이터 ** _입니다. 요소의 innerHTML에 할당 할 수 없습니다. 클라이언트 쪽에서는 단지 이미지의'src' 속성에 할당하는 것이 적절합니다. – hindmost
toDataUrl의 결과는 innerHTML에 절대적으로 할당 할 수있는 base64입니다. 이에 대한 예제가 있습니다. – Justin