티스토리 뷰
특정 폴더에 있는 png 이미지들을 webp로 변환시켜주는 툴을 만들어 보겠습니다.
간단하게 Node js 로 제작합니다.
우선 프로젝트 폴더를 만들어 주고
$ npm init
으로 프로젝트를 생성합니다.
그 후, 이미지 변환에 필요한 패키지들을 설치해 줍니다. (Global로 설치하셔도 무방합니다.)
$ npm install imagemin imagemin-webp --save
imagemin : 이미지 미니파이 해주는 툴
imagemin-webp : imagemin의 webp 플러그인
설치가 완료 되었으면, /webp.js 파일을 만들어 줍니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const imagemin = require("imagemin"); | |
const webp = require("imagemin-webp"); | |
//source 폴더의 png 파일들을 변환해서 images 폴더로 넣겠다. | |
//만약 images 폴더가 존재하지 않는 다면 생성됨 | |
imagemin(["sources/*.png"], "images", { | |
use: [ | |
webp({ | |
quality: 75 //변환되는 webp의 퀄리티를 지정. 손실 있음. 75%. | |
}) | |
] | |
}).then(function() { | |
console.log("Images converted!"); //완료되었을때 로그 | |
}); | |
퀄리티가 75% 이므로 손실이 있습니다. 만약 무손실 옵션을 사용하고 싶다면
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
webp({ | |
lossless: true | |
}) | |
로 작성해줍니다.
작성이 완료 되었으면 sources 폴더에 png 이미지 파일들을 넣어주시고
$ node wep
실행.
Images converted!
메세지를 확인하고 images 폴더를 열어봅니다.
짠.
간단하게 자신만의 webp 변환기를 만들고 한번에 여러개의 파일을 webp로 변환해주세요.
코드는 https://github.com/yunyong/node-webp-converter 에서 확인하실 수 있습니다.
'javascript > etc.' 카테고리의 다른 글
자바스크립트 객체지향 프로그래밍 Javascript Object Oriented Programming (0) | 2018.11.12 |
---|---|
console.log : 우리가 모르는 유용한 기능들 (0) | 2018.07.31 |
DataTables 한글 - language 옵션 설정 (8) | 2018.01.09 |
DataTables 버튼 (복사, pdf, 엑셀, 프린트 버튼) (10) | 2018.01.08 |
DataTables 장단점 및 속도 이슈 (2) | 2017.02.03 |
Kakao 다음웹툰 / 웹표준 및 ft기술, 웹접근성 / 세아이 아빠, 제주 거주 중..
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday