Skip to content

Commit

Permalink
uploadもwebpに対応
Browse files Browse the repository at this point in the history
  • Loading branch information
toshi-pono committed Dec 28, 2021
1 parent e6ef437 commit 273c014
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions server/src/converters/convert_image.js
Expand Up @@ -9,6 +9,16 @@ import sharp from 'sharp';
* @returns {Promise<Uint8Array>}
*/
async function convertImage(buffer, options) {
if (options.extension === 'webp') {
return sharp(buffer)
.resize({
fit: 'cover',
height: options.height,
width: options.width,
})
.webp({ lossless: true })
.toBuffer();
}
return sharp(buffer)
.resize({
fit: 'cover',
Expand Down
4 changes: 2 additions & 2 deletions server/src/routes/api/image.js
Expand Up @@ -9,7 +9,7 @@ import { convertImage } from '../../converters/convert_image';
import { UPLOAD_PATH } from '../../paths';

// 変換した画像の拡張子
const EXTENSION = 'jpg';
const EXTENSION = 'webp';

const router = Router();

Expand All @@ -29,7 +29,7 @@ router.post('/images', async (req, res) => {
// 画像の縦サイズを指定する (undefined は元画像に合わせる)
height: undefined,
// 画像の横サイズを指定する (undefined は元画像に合わせる)
width: undefined,
width: 600,
});

const filePath = path.resolve(UPLOAD_PATH, `./images/${imageId}.${EXTENSION}`);
Expand Down

0 comments on commit 273c014

Please sign in to comment.