Resize to Fit
What does it do?
The resize to Fit operation will rescale the image so that it fit into the defined pixel rectangle as closely as possible. Aspect ratio is preserved, so the image is not stretched and will end up being either the width or height specified but not both unless the aspect ratio of the image is the same as the specified new size.
Resize a BufferedImage in Java
Add JDeli to your project via Maven or Gradle, or see using JDeli with Java modules.
ImageProcessingOperations uses a builder pattern — chain transformation methods to define how the image should be processed, then pass the object to JDeli.convert() to apply them during format conversion.
ImageProcessingOperations operations = new ImageProcessingOperations();
// Chain multiple operations such as scale, blur, rotate, sharpen, and more
operations.resizeToFit(100, 100); // make image fit into a box 100x100 pixels preserving aspect ratio
// Apply the operations to a BufferedImage
BufferedImage modifiedImage = operations.apply(BufferedImage originalImage);
View Javadoc for the resize to fit operation
JDeli.convert() — Input/Output Methods
Pass an ImageProcessingOperations object to any JDeli.convert() overload to apply transformations during conversion. JDeli supports three input/output models:
Using File
Read from and write to disk paths directly.
File inputFile = new File("path/to/file");
File outputFile = new File("path/to/output-resizedToFit-file");
JDeli.convert(inputFile, outputFile, operations);
Using InputStream and OutputStream
Use for network streams, servlet responses, or any scenario where file paths are unavailable.
final InputStream inputStream = new FileInputStream(inputFile);
final OutputStream outputStream = new FileOutputStream(outputFile);
final String outputFormat = "png"; // Supported formats: avif, bmp, gif, heic, jpeg, pdf, png, tiff, webp
JDeli.convert(inputStream, outputStream, outputFormat, operations);
Using byte[]
Use for in-memory processing pipelines where data is already loaded.
byte[] inputData = Files.readAllBytes(Paths.get("/path/to/file"));
final String outputFormat = "png"; // Supported formats: avif, bmp, gif, heic, jpeg, pdf, png, tiff, webp
byte[] outputData = JDeli.convert(inputData, outputFormat, operations);
Frequently Asked Questions
Can I chain multiple image processing operations together?
Yes. ImageProcessingOperations is a builder — call as many methods as needed before passing the object to JDeli.convert(). Operations are applied in the order they are chained.
Can I apply operations without converting the image format?
Yes. Call operations.apply(BufferedImage originalImage) directly to get a processed BufferedImage without any format conversion.
Which input types does JDeli support for image processing?
JDeli supports File, InputStream/OutputStream, and byte[]. All three overloads of JDeli.convert() accept an ImageProcessingOperations parameter.
What output formats can I convert to when applying operations?
JDeli can write to AVIF, BMP, GIF, HEIC, JPEG, PDF, PNG, TIFF, and WebP when using JDeli.convert() with an ImageProcessingOperations object.