AgentSkillsCN

Image Processing

图像处理

SKILL.md

Image Processing Skill

Image handling standards and best practices for frontend applications.

When to Apply

  • Uploading images
  • Processing image data
  • Canvas manipulation
  • Format conversion
  • Coordinate calculations

Image Source Types

SourceFormatTypical Use Case
User uploadFile objectForm file inputs
Remote URLString URLExternal images
API responsebase64 / URLAI generation, backend
CanvasHTMLCanvasElementDrawing, manipulation
ClipboardBlob / FilePaste operations

File Validation Standards

Allowed Types

TypeMIMEExtension
JPEGimage/jpeg.jpg, .jpeg
PNGimage/png.png
WebPimage/webp.webp
GIFimage/gif.gif (if animations supported)

Size Limits

LimitRecommended Value
Max file size10 MB
Max dimensions4096 x 4096 px
Min dimensions100 x 100 px

Validation Checklist

  • File type in allowed list
  • File size under limit
  • Image loads successfully
  • Dimensions within bounds

Format Conversion Guidelines

Conversion Methods

FromToMethod
Filebase64FileReader.readAsDataURL
base64Blobfetch(base64).blob()
Blobbase64FileReader.readAsDataURL
URLBlobfetch(url).blob()
CanvasBlobcanvas.toBlob()
Canvasbase64canvas.toDataURL()

Quality Settings

FormatQuality ParameterRecommended
JPEG0.0 - 1.00.85 - 0.95
PNGN/A (lossless)N/A
WebP0.0 - 1.00.80 - 0.90

Dimension Handling

Aspect Ratio Calculations

ScenarioFormula
Scale to fit widthheight = width / aspectRatio
Scale to fit heightwidth = height * aspectRatio
Aspect ratiowidth / height

Letterboxing (Object-Fit: Contain)

Image AspectContainer AspectResult
WiderNarrowerLetterbox top/bottom
TallerWiderLetterbox left/right
EqualEqualNo letterbox

Coordinate Systems

SystemOriginUse Case
NaturalTop-left of imageOriginal image pixels
RenderedTop-left of containerDisplay coordinates
Percentage0-100%Relative positioning

Canvas Standards

Canvas Operations

OperationConsideration
Create canvasSet width/height before drawing
Get contextUse '2d' for image manipulation
Draw imageUse naturalWidth/naturalHeight for source
ExportUse toBlob for file operations

Canvas Performance

GuidelineDescription
Minimize redrawsBatch operations, use requestAnimationFrame
Limit canvas sizeMax ~4096x4096 on most devices
Clean upRemove canvas from DOM when done
Offscreen canvasUse for heavy processing

API Integration Standards

FormData for Image Upload

FieldDescription
image / image[]Image blob(s)
filenameDescriptive filename
Content-TypeAutomatically set by FormData

Image API Response Handling

Response TypeHandling
base64 stringDirect use in img src
URLFetch and convert if needed
BinaryConvert to Blob

Error Handling Standards

Error Types

ErrorCauseUser Message
Invalid typeWrong file extension"Please upload a JPEG, PNG, or WebP image"
Too largeExceeds size limit"Image must be under 10MB"
Load failedCorrupt or network error"Failed to load image"
Process failedCanvas/conversion error"Failed to process image"

Error Handling Guidelines

GuidelineDescription
Validate earlyCheck type/size before processing
User-friendly messagesNo technical details
Graceful degradationProvide fallback/retry option
Log detailsConsole.error with context for debugging

Performance Guidelines

DO

  • Validate file type/size before processing
  • Use Blob URLs for display (URL.createObjectURL)
  • Revoke Blob URLs when done (URL.revokeObjectURL)
  • Process large images in Web Workers
  • Use appropriate compression for uploads

DON'T

  • Load full-resolution images for thumbnails
  • Keep unused Blob URLs in memory
  • Block UI thread with heavy processing
  • Skip dimension validation
  • Store images in state as base64 (use Blob URLs)

Browser Compatibility

Feature Support

FeatureSupport
FileReaderAll modern browsers
CanvasAll modern browsers
BlobAll modern browsers
createImageBitmapMost modern (check Safari)
OffscreenCanvasPartial (check before use)

Mobile Considerations

ConsiderationRecommendation
Camera uploadsAccept image/* capture
Memory limitsProcess images in chunks
OrientationCheck EXIF, rotate if needed
Touch gesturesConsider pinch-zoom for canvas

Code Review Checklist

  • File type validation implemented
  • File size validation implemented
  • Blob URLs properly revoked
  • Error handling with user-friendly messages
  • Canvas properly sized and cleaned up
  • No base64 strings in component state
  • Async operations properly handled
  • Memory leaks prevented