imgscalr Documentation

repository·master·Indexed 22 days ago

https://github.com/rkalla/imgscalr

A high-performance, hardware-accelerated Java 2D library for image scaling and manipulation. It provides tools for resizing, cropping, and rotating images, featuring scaling modes like FIT_EXACT and high-accuracy methods such as ULTRA_QUALITY. The library includes AsyncScalr for asynchronous processing with rate limiting and support for applying BufferedImageOps like OP_ANTIALIAS.

Tokens
853
Snippets
2
Records
9
Agent score
29%

What's inside imgscalr

  1. Configure imgscalr scaling modes

    master

    imgscalr allows you to control how images are fitted into target dimensions using Mode. By default, resizing maintains proportions. Use Mode to override this behavior:

    • Mode.FIT_EXACT: Forces the image into the specific given dimensions, ignoring orientation and proportions.
    • Mode.FIT_TO_WIDTH: Treats width as the primary dimension and recalculates height to fit.
    • Mode.FIT_TO_HEIGHT: Treats height as the primary dimension and recalculates width to fit.
  2. Perform asynchronous scaling with AsyncScalr

    master

    To avoid overloading the host system or exhausting the heap in multi-user environments (like web apps), use the AsyncScalr class. It wraps Scalr and submits jobs to an internal ExecutorService.

    Key features:

    • Rate Limiting: Uses a fixed-size thread pool (default is 2 threads) to queue scaling operations.
    • Customization: You can customize the service by extending AsyncScalr and overriding createService() or createService(ThreadFactory).
    • Thread Factories: Provides DefaultThreadFactory and ServerThreadFactory (optimized for servers with daemon threads and LOW_PRIORITY).
    • Configuration: The number of threads can be tuned via the system property THREAD_COUNT.
  3. Handle GIF transparency and quality

    master

    Scaling animated GIFs is not supported. For static GIFs, be aware of the following:

    1. Transparency: If a GIF has an alpha channel (TYPE_INT_ARGB), saving it as a GIF in older Java runtimes may replace transparency with solid black.
    2. Recommendation: Save the resulting BufferedImage as a PNG to maintain transparency and avoid color quantization issues.
    3. Avoid Ops on GIFs: Applying BufferedImageOps (like ConvolveOp) to a scaled image before saving it as a GIF can corrupt the file. If you need to apply operations, save the result as a PNG.
  4. Troubleshoot OutOfMemoryErrors

    master

    Image manipulation requires significant memory because images are decoded into raw ARGB bytes in the BufferedImage instance. If you encounter OutOfMemoryException when processing large images, increase the JVM heap size using the -Xmx flag.

    Example:

    java -Xmx128m com.site.MyApp
  5. Quickstart: Scale an image proportionally

    master

    To scale an image to a specific width while maintaining its original proportions and letting the library choose the best scaling method, use Scalr.resize(BufferedImage, int).

    BufferedImage srcImage = ImageIO.read(...); // Load image
    BufferedImage scaledImage = Scalr.resize(srcImage, 150); // Scale image
  6. Use ULTRA_QUALITY for high-accuracy scaling

    master
    In version 4.2, a new Method.ULTRA_QUALITY was added. This method uses 3.5x more incremental steps than Method.QUALITY, providing much more accurate results. It is particularly effective for preventing jagged diagonal lines in small thumbnails.
  7. Apply custom BufferedImageOps to images

    master

    Most imgscalr methods accept a variable number of BufferedImageOps as arguments. These operations are applied to the final image result before it is returned.

    One common use case is applying a light softening effect (anti-aliasing) using the built-in constant:

    • Scalr.OP_ANTIALIAS