|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from 'fs' |
| 4 | +import path from 'path' |
| 5 | +import { fileURLToPath } from 'url' |
| 6 | + |
| 7 | +/** |
| 8 | + * Crop QR code image - cut 340px from top, 230px from bottom, 100px from left, 100px from right |
| 9 | + * Uses pure Node.js with Canvas API for image processing |
| 10 | + */ |
| 11 | + |
| 12 | +import { createCanvas, loadImage } from 'canvas' |
| 13 | + |
| 14 | +// Get __dirname equivalent in ES modules |
| 15 | +const __filename = fileURLToPath(import.meta.url) |
| 16 | +const __dirname = path.dirname(__filename) |
| 17 | + |
| 18 | +async function cropImage () { |
| 19 | + try { |
| 20 | + const inputPath = path.join(__dirname, '../src/static/electerm-wechat-group-qr.jpg') |
| 21 | + const outputPath = path.join(__dirname, '../src/static/electerm-wechat-group-qr.jpg') |
| 22 | + |
| 23 | + // Check if input file exists |
| 24 | + if (!fs.existsSync(inputPath)) { |
| 25 | + console.error('Input file does not exist:', inputPath) |
| 26 | + process.exit(1) |
| 27 | + } |
| 28 | + |
| 29 | + console.log('Loading image:', inputPath) |
| 30 | + |
| 31 | + // Load the image |
| 32 | + const image = await loadImage(inputPath) |
| 33 | + const originalWidth = image.width |
| 34 | + const originalHeight = image.height |
| 35 | + |
| 36 | + console.log(`Original dimensions: ${originalWidth}x${originalHeight}`) |
| 37 | + |
| 38 | + // Calculate new dimensions |
| 39 | + const cropTop = 340 |
| 40 | + const cropBottom = 230 |
| 41 | + const cropLeft = 120 |
| 42 | + const cropRight = 120 |
| 43 | + const newWidth = originalWidth - cropLeft - cropRight |
| 44 | + const newHeight = originalHeight - cropTop - cropBottom |
| 45 | + |
| 46 | + if (newHeight <= 0 || newWidth <= 0) { |
| 47 | + console.error('Error: Cropping dimensions would result in zero or negative dimensions') |
| 48 | + console.error(`Original dimensions: ${originalWidth}x${originalHeight}`) |
| 49 | + console.error(`Crop values - top: ${cropTop}, bottom: ${cropBottom}, left: ${cropLeft}, right: ${cropRight}`) |
| 50 | + process.exit(1) |
| 51 | + } |
| 52 | + |
| 53 | + console.log(`New dimensions: ${newWidth}x${newHeight}`) |
| 54 | + console.log(`Cropping: ${cropTop}px from top, ${cropBottom}px from bottom, ${cropLeft}px from left, ${cropRight}px from right`) |
| 55 | + |
| 56 | + // Create canvas with new dimensions |
| 57 | + const canvas = createCanvas(newWidth, newHeight) |
| 58 | + const ctx = canvas.getContext('2d') |
| 59 | + |
| 60 | + // Draw the cropped portion of the image |
| 61 | + ctx.drawImage( |
| 62 | + image, |
| 63 | + cropLeft, cropTop, // Source x, y (start from cropLeft pixels right and cropTop pixels down) |
| 64 | + newWidth, newHeight, // Source width, height |
| 65 | + 0, 0, // Destination x, y |
| 66 | + newWidth, newHeight // Destination width, height |
| 67 | + ) |
| 68 | + |
| 69 | + // Save the cropped image |
| 70 | + const buffer = canvas.toBuffer('image/jpeg', { quality: 0.9 }) |
| 71 | + fs.writeFileSync(outputPath, buffer) |
| 72 | + |
| 73 | + console.log('Successfully cropped image saved to:', outputPath) |
| 74 | + console.log(`Final dimensions: ${newWidth}x${newHeight}`) |
| 75 | + } catch (error) { |
| 76 | + console.error('Error processing image:', error) |
| 77 | + process.exit(1) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// Check if canvas module is available |
| 82 | +try { |
| 83 | + await import('canvas') |
| 84 | +} catch (error) { |
| 85 | + console.error('Canvas module not found. Please install it with:') |
| 86 | + console.error('npm install canvas') |
| 87 | + console.error('\nNote: Canvas requires some system dependencies.') |
| 88 | + console.error('On macOS: brew install pkg-config cairo pango libpng jpeg giflib librsvg') |
| 89 | + process.exit(1) |
| 90 | +} |
| 91 | + |
| 92 | +// Run the crop function |
| 93 | +cropImage() |
0 commit comments