File size: 1,122 Bytes
68f7925
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import sharp from 'sharp';
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

async function convertNoImageToWebP() {
  const inputPath = path.join(__dirname, '../public/images/no-image.jpg');
  const outputPath = path.join(__dirname, '../public/images/no-image.webp');

  try {
    await sharp(inputPath)
      .resize(800, 600, {
        fit: 'cover',
        position: 'center'
      })
      .webp({ quality: 65 })
      .toFile(outputPath);

    console.log(`✅ 画像を変換しました: ${inputPath} -> ${outputPath}`);

    // ファイルサイズの比較
    const originalSize = fs.statSync(inputPath).size;
    const newSize = fs.statSync(outputPath).size;
    const reduction = Math.round((1 - newSize / originalSize) * 100);

    console.log(`📊 ファイルサイズ: ${originalSize} bytes -> ${newSize} bytes (${reduction}% 削減)`);
  } catch (error) {
    console.error('❌ エラーが発生しました:', error);
    process.exit(1);
  }
}

convertNoImageToWebP();