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

Image before editing
Image after editing

Edited Image
Installation
Install dependencies
1npm install pixel-craft --saveCopy 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 ImageFilterProps
| Prop name | Type | Default | Description |
|---|---|---|---|
src | String | Required | The URL/path of the image to display. |
alt | String | Filtered Image | Alt text for the image. |
grayscale | number | 0 | Grayscale effect from 0 (none) to 1)(fully grayscale). |
sepia | number | 0 | Sepia effect from 0 (none) to 1)(fully sepia-toned). |
brightness | number | 1 | Brightness multiplier (0.5 is darker, 2)is brighter). |
contrast | number | 1 | Contrast multiplier(0.5 is less contrast, 2 is more. |
blur | number | 0 | Blur effect in pixels(px). |
width | string | "300px" | Width of the image (any valid CSS unit). |
height | string | "auto" | Height of the image (any valid CSS unit). |
Possible Errors and User Measures
| Error/ Issue | Cause | User's Measures/ Fixes |
|---|---|---|
filterStyleis invalid | The filterStyle string is not composed correctly—uses invalid syntax like grayscale(grayscale) | Construct the string properly (see corrected code below) |
| Watermark image not shown | Image fails to load | Invalid srcis invalid or cross-origin error |
| Performance impact on large images | Too many CSS filters on large files | Recommend compressing or resizing large images before applying filters |
| Unsupported CSS filters | Older browser versions or incomplete CSS support | Warn users and provide fallback styling or check browser compatibility |
| Misconfigured filter values | Accepting 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
| Techniques | Benefits |
|---|---|
| Compress before filtering | Reduces 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 swap | For perceived speed in slow connections |
| Serve modern formats (WebP/AVIF) | Greatly reduce file size with no visible loss in quality |