Filters and Effects

This component allows you to apply various filters and effects to images, such as grayscale, sepia, brightness, contrast, and blur.

Original

Image before editing

Image after editing

Compressed

Edited Image

Installation

Install dependencies

1npm install pixel-craft --save

Copy the source code

components/ImageCompressor.tsx

1import React from 'react';
2
3type FilterProps = {
4  src: string;
5  alt?: string;
6  grayscale?: number;   // 0 to 1
7  sepia?: number;       // 0 to 1
8  brightness?: number;  // 0.5 to 2
9  contrast?: number;    // 0.5 to 2
10  blur?: number;        // in px
11  width?: string;
12  height?: string;
13};
14
15const ImageFilter: React.FC<FilterProps> = ({
16  src,
17  alt = 'Filtered Image',
18  grayscale = 0,
19  sepia = 0,
20  brightness = 1,
21  contrast = 1,
22  blur = 0,
23  width = '300px',
24  height = 'auto',
25}) => {
26  const filterStyle = 
27  
28    grayscale({grayscale})
29    sepia({sepia})
30    brightness({brightness})
31    contrast({contrast})
32    blur({blur}px)
33  
34
35  return (
36    <img
37      src={src}
38      alt={alt}
39      style={{
40        filter: filterStyle,
41        width,
42        height,
43        borderRadius: '8px',
44      }}
45    />
46  );
47};
48
49export default ImageFilter

Props

Prop nameTypeDefaultDescription
srcStringRequiredThe URL/path of the image to display.
altStringFiltered ImageAlt text for the image.
grayscalenumber0Grayscale effect from 0 (none) to 1)(fully grayscale).
sepianumber0Sepia effect from 0 (none) to 1)(fully sepia-toned).
brightnessnumber1Brightness multiplier (0.5 is darker, 2)is brighter).
contrastnumber1Contrast multiplier(0.5 is less contrast, 2 is more.
blurnumber0Blur effect in pixels(px).
widthstring"300px"Width of the image (any valid CSS unit).
heightstring"auto"Height of the image (any valid CSS unit).

Possible Errors and User Measures

Error/ IssueCauseUser's Measures/ Fixes
filterStyleis invalidThe filterStyle string is not composed correctly—uses invalid syntax like grayscale(grayscale)Construct the string properly (see corrected code below)
Watermark image not shownImage fails to loadInvalid srcis invalid or cross-origin error
Performance impact on large imagesToo many CSS filters on large filesRecommend compressing or resizing large images before applying filters
Unsupported CSS filtersOlder browser versions or incomplete CSS supportWarn users and provide fallback styling or check browser compatibility
Misconfigured filter valuesAccepting values out of valid range (e.g.,    grayscale: 3 )Add prop validation or clamping to keep values in supported ranges

Real World Uses

Image aesthetic previews

Users can preview filters like Instagram/Snapseed before applying

Theming / branding adjustments

Apply brand-specific tones (sepia, brightness) dynamically

Visual accessibility tools

Reduce brightness or contrast for better readability

Marketing previews

Simulate how images will look under different filters for ads or social media posts

Optimization Techniques



TechniquesBenefits
Compress before filteringReduces memory and rendering overhead
Use lazy-loading (loading="lazy")Improves performance when many filtered images are on screen
Cache processed images (if static)Avoid recomputing filters every time using canvas or exporting
Use low-res placeholder and swapFor perceived speed in slow connections
Serve modern formats (WebP/AVIF)Greatly reduce file size with no visible loss in quality