javascript 如何使用javascript将jpg图像转换为png
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10617710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert jpg image to png using javascript
提问by mehmood
Convert JPG image to PNG
将 JPG 图像转换为 PNG
I have to convert a jpg image into png image using javascript and resize the image to create a thumbnail of image.
我必须使用 javascript 将 jpg 图像转换为 png 图像并调整图像大小以创建图像缩略图。
采纳答案by Esse
you should take a look at processing.js library: http://processingjs.org/reference/PImage_resize_/http://processingjs.org/reference/save_/
你应该看看 processing.js 库:http: //processingjs.org/reference/PImage_resize_/http://processingjs.org/reference/save_/
回答by sazzy4o
If we have a look at the sourcefrom the JPG to PNGwebsite which uses pure javascript to convert images from JPG to PNG. We see that they:
如果我们查看JPG to PNG网站的源代码,该网站使用纯 javascript 将图像从 JPG 转换为 PNG。我们看到他们:
- Load the jpg image from file
- Create a canvas of the same size as the jpg
- Draw the jpg image covering the whole canvas
- Convert canvas to blob (if the image is small enough you can also use to
.toDataURL()
) - Download the blob
- 从文件加载 jpg 图像
- 创建一个与jpg相同大小的画布
- 绘制覆盖整个画布的jpg图像
- 将画布转换为 blob(如果图像足够小,您也可以使用 to
.toDataURL()
) - 下载 blob
回答by moribvndvs
It's not impossible to write a pure JavaScript library that allows you to manipulate and convert images, but I don't know any off the top of my head, nor would I use them.
编写一个允许您操作和转换图像的纯 JavaScript 库并非不可能,但我不知道有什么想法,也不会使用它们。
Instead, I'd upload the original image to a server framework of my choice (PHP, ASP.NET, etc.) and have it manipulate the image for you.
相反,我会将原始图像上传到我选择的服务器框架(PHP、ASP.NET 等)并让它为您处理图像。
回答by cancerbero
There are plenty ports of native png/JPEG libraries through emscripten and also a couple written purely in JavaScript, This is what it comes to my mind now:
通过 emscripten 有很多本地 png/JPEG 库的端口,还有一些完全用 JavaScript 编写的,这就是我现在想到的:
https://www.npmjs.com/package/jimp
https://www.npmjs.com/package/jimp
Jimp.read('lenna.png', (err, lenna) => {
if (err) throw err;
lenna
.write('lena-small-bw.jpg'); // save
});
But in general you want to search something like 'png to jpeg' in npm.org you will find plenty libraries.
但一般来说,您想在 npm.org 中搜索诸如“png to jpeg”之类的内容,您会发现很多库。