最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

CompressResize jpeg and get the Base64 in Javascript - Stack Overflow

programmeradmin7浏览0评论

How to reduce the size of a base64 String for a Jpeg Image? I have the base64 of a 2000x1000px photo. How to get the 500x250px base64 pressed photo? Someone can help me?

How to reduce the size of a base64 String for a Jpeg Image? I have the base64 of a 2000x1000px photo. How to get the 500x250px base64 pressed photo? Someone can help me?

Share Improve this question asked May 9, 2012 at 9:47 KeyserSKeyserS 212 silver badges3 bronze badges 1
  • use it's data url to load it as an image, render it (resized) to a canvas element, store the result from toDataURL. – Yoshi Commented May 9, 2012 at 9:53
Add a ment  | 

1 Answer 1

Reset to default 6

put the base64 in an image tag

var img = document.createElement("img");
img.src = "data:image/gif;base64," + yourBase64;

draw it to a scaled canvas

var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.scale(0.25, 0.25);
ctx.drawImage(img);

get the base64

var base64 = canvas.toDataURL("image/jpeg");

I didn't actually test this so the resizing part might be wrong, but it would be something like this.

try searching how to resize images using the canvas tag.

发布评论

评论列表(0)

  1. 暂无评论