Skip to content

উন্নত উদাহরণ

এখানে Sharp-এর কিছু উন্নত ব্যবহারের উদাহরণ দেওয়া হয়েছে, যার মধ্যে রয়েছে জটিল ইমেজ প্রসেসিং অপারেশন।

ইমেজ কম্পোজিশন

ওয়াটারমার্ক যোগ করুন

javascript
import sharp from 'sharp';

// টেক্সট ওয়াটারমার্ক যোগ করুন
await sharp('input.jpg')
  .composite([
    {
      input: Buffer.from(`
        <svg width="200" height="100">
          <text x="10" y="50" font-family="Arial" font-size="24" fill="white">
            Watermark
          </text>
        </svg>
      `),
      top: 10,
      left: 10
    }
  ])
  .jpeg()
  .toFile('output.jpg');

ইমেজ ওভারলে

javascript
// একাধিক ইমেজ ওভারলে করুন
await sharp('background.jpg')
  .composite([
    {
      input: 'overlay1.png',
      top: 100,
      left: 100
    },
    {
      input: 'overlay2.png',
      top: 200,
      left: 200
    }
  ])
  .jpeg()
  .toFile('output.jpg');

চ্যানেল অপারেশন

চ্যানেল আলাদা করা

javascript
// RGB চ্যানেল আলাদা করুন
const channels = await sharp('input.jpg').separate();

// প্রতিটি চ্যানেল সংরক্ষণ করুন
await sharp(channels[0]).toFile('red-channel.jpg');
await sharp(channels[1]).toFile('green-channel.jpg');
await sharp(channels[2]).toFile('blue-channel.jpg');

চ্যানেল মার্জ করা

javascript
// আলাদা চ্যানেল ফাইল থেকে মার্জ করুন
await sharp('red-channel.jpg')
  .joinChannel(['green-channel.jpg', 'blue-channel.jpg'])
  .toFile('merged.jpg');

রঙ স্পেস কনভার্শন

RGB থেকে CMYK

javascript
await sharp('input.jpg')
  .toColourspace(sharp.colourspace.cmyk)
  .tiff()
  .toFile('output.tiff');

Lab রঙ স্পেসে রূপান্তর

javascript
await sharp('input.jpg')
  .toColourspace(sharp.colourspace.lab)
  .tiff()
  .toFile('output.tiff');

উন্নত ফিল্টার

কাস্টম শার্পেনিং

javascript
await sharp('input.jpg')
  .sharpen({
    sigma: 1,
    flat: 1,
    jagged: 2
  })
  .toFile('output.jpg');

গামা করেকশন

javascript
await sharp('input.jpg')
  .gamma(2.2)
  .toFile('output.jpg');

টিন্ট

javascript
await sharp('input.jpg')
  .tint({ r: 255, g: 0, b: 0 })
  .toFile('output.jpg');

মাল্টি-পেজ ইমেজ প্রসেসিং

TIFF মাল্টি-পেজ প্রসেস করুন

javascript
// সব পেজ প্রসেস করুন
await sharp('multi-page.tiff', { pages: -1 })
  .resize(800, 600)
  .tiff()
  .toFile('output.tiff');

// নির্দিষ্ট পেজ প্রসেস করুন
await sharp('multi-page.tiff', { pages: 0 })
  .resize(800, 600)
  .jpeg()
  .toFile('page0.jpg');

PDF প্রসেস করুন

javascript
// PDF প্রথম পেজ প্রসেস করুন
await sharp('document.pdf', { pages: 0 })
  .resize(800, 600)
  .jpeg()
  .toFile('page0.jpg');

রেসপন্সিভ ইমেজ জেনারেশন

একাধিক আকার জেনারেট করুন

javascript
async function generateResponsiveImages(inputPath) {
  const sizes = [
    { width: 320, height: 240, suffix: 'small' },
    { width: 640, height: 480, suffix: 'medium' },
    { width: 1280, height: 960, suffix: 'large' },
    { width: 1920, height: 1440, suffix: 'xlarge' }
  ];
  
  const promises = sizes.map(async ({ width, height, suffix }) => {
    await sharp(inputPath)
      .resize(width, height, { fit: 'cover' })
      .jpeg({ quality: 80 })
      .toFile(`output-${suffix}.jpg`);
  });
  
  await Promise.all(promises);
}

একাধিক ফরম্যাট জেনারেট করুন

javascript
async function generateMultipleFormats(inputPath) {
  const formats = [
    { format: 'jpeg', quality: 80, ext: 'jpg' },
    { format: 'webp', quality: 80, ext: 'webp' },
    { format: 'avif', quality: 80, ext: 'avif' }
  ];
  
  const promises = formats.map(async ({ format, quality, ext }) => {
    const image = sharp(inputPath).resize(800, 600);
    
    switch (format) {
      case 'jpeg':
        await image.jpeg({ quality }).toFile(`output.${ext}`);
        break;
      case 'webp':
        await image.webp({ quality }).toFile(`output.${ext}`);
        break;
      case 'avif':
        await image.avif({ quality }).toFile(`output.${ext}`);
        break;
    }
  });
  
  await Promise.all(promises);
}

ইমেজ অ্যানালাইসিস

ইমেজ পরিসংখ্যান পান

javascript
async function analyzeImage(filePath) {
  const metadata = await sharp(filePath).metadata();
  const stats = await sharp(filePath).stats();
  
  return {
    metadata,
    stats: {
      isOpaque: stats.isOpaque,
      dominant: stats.dominant,
      channels: stats.channels
    }
  };
}

ইমেজ টাইপ ডিটেক্ট করুন

javascript
async function detectImageType(filePath) {
  const metadata = await sharp(filePath).metadata();
  
  return {
    format: metadata.format,
    hasAlpha: metadata.hasAlpha,
    isOpaque: metadata.isOpaque,
    channels: metadata.channels,
    colorSpace: metadata.space
  };
}

উন্নত ক্রপ

স্মার্ট ক্রপ

javascript
async function smartCrop(inputPath, outputPath, width, height) {
  // ইমেজ তথ্য পান
  const metadata = await sharp(inputPath).metadata();
  
  // ক্রপ এলাকা গণনা করুন
  const aspectRatio = width / height;
  const imageAspectRatio = metadata.width / metadata.height;
  
  let cropWidth, cropHeight, left, top;
  
  if (aspectRatio > imageAspectRatio) {
    // লক্ষ্য আরও প্রশস্ত, উচ্চতা অনুযায়ী
    cropHeight = metadata.height;
    cropWidth = cropHeight * aspectRatio;
    top = 0;
    left = (metadata.width - cropWidth) / 2;
  } else {
    // লক্ষ্য আরও উঁচু, প্রস্থ অনুযায়ী
    cropWidth = metadata.width;
    cropHeight = cropWidth / aspectRatio;
    left = 0;
    top = (metadata.height - cropHeight) / 2;
  }
  
  await sharp(inputPath)
    .extract({ left: Math.round(left), top: Math.round(top), width: Math.round(cropWidth), height: Math.round(cropHeight) })
    .resize(width, height)
    .toFile(outputPath);
}

ইমেজ অপ্টিমাইজেশন

স্বয়ংক্রিয় অপ্টিমাইজেশন

javascript
async function autoOptimize(inputPath, outputPath) {
  const metadata = await sharp(inputPath).metadata();
  
  let image = sharp(inputPath);
  
  // ইমেজ টাইপ অনুযায়ী সেরা ফরম্যাট নির্বাচন করুন
  if (metadata.hasAlpha) {
    // ট্রান্সপারেন্সি আছে, PNG ব্যবহার করুন
    image = image.png();
  } else {
    // ট্রান্সপারেন্সি নেই, JPEG ব্যবহার করুন
    image = image.jpeg({ quality: 80, progressive: true });
  }
  
  await image.toFile(outputPath);
}

প্রগ্রেসিভ JPEG

javascript
await sharp('input.jpg')
  .jpeg({ 
    quality: 80, 
    progressive: true,
    mozjpeg: true 
  })
  .toFile('output.jpg');

ব্যাচ উন্নত প্রসেসিং

ব্যাচ ওয়াটারমার্ক

javascript
const fs = require('fs').promises;

async function batchWatermark(inputDir, outputDir, watermarkPath) {
  const files = await fs.readdir(inputDir);
  
  for (const file of files) {
    if (file.match(/\.(jpg|jpeg|png|webp)$/i)) {
      try {
        await sharp(path.join(inputDir, file))
          .composite([
            {
              input: watermarkPath,
              top: 10,
              left: 10
            }
          ])
          .jpeg({ quality: 80 })
          .toFile(path.join(outputDir, `watermarked_${file}`));
        
        console.log(`ওয়াটারমার্ক যোগ সম্পন্ন: ${file}`);
      } catch (error) {
        console.error(`${file} প্রসেসিং ব্যর্থ:`, error.message);
      }
    }
  }
}

ব্যাচ ফরম্যাট কনভার্শন এবং অপ্টিমাইজেশন

javascript
async function batchOptimize(inputDir, outputDir) {
  const files = await fs.readdir(inputDir);
  
  for (const file of files) {
    if (file.match(/\.(jpg|jpeg|png|webp)$/i)) {
      try {
        const metadata = await sharp(path.join(inputDir, file)).metadata();
        const image = sharp(path.join(inputDir, file));
        
        // ইমেজ বৈশিষ্ট্য অনুযায়ী সেরা ফরম্যাট নির্বাচন করুন
        if (metadata.hasAlpha) {
          await image.png({ compressionLevel: 9 }).toFile(path.join(outputDir, file.replace(/\.[^.]+$/, '.png')));
        } else {
          await image.jpeg({ quality: 80, progressive: true }).toFile(path.join(outputDir, file.replace(/\.[^.]+$/, '.jpg')));
        }
        
        console.log(`অপ্টিমাইজেশন সম্পন্ন: ${file}`);
      } catch (error) {
        console.error(`${file} প্রসেসিং ব্যর্থ:`, error.message);
      }
    }
  }
}

সম্পর্কিত লিঙ্ক

Apache 2.0 লাইসেন্সের অধীনে রিলিজ করা হয়েছে।