### Create a thumbnail from an image file using String filenames Source: https://github.com/coobird/thumbnailator/wiki/Examples Alternative to the File-based example; accepts file names as String objects, useful for quick prototypes or scripting languages. ```java Thumbnails.of("original.jpg") .size(160, 160) .toFile("thumbnail.jpg"); ``` -------------------------------- ### Crop to size with Thumbnails.crop(Positions.CENTER) Source: https://github.com/coobird/thumbnailator/wiki/Examples Creates a 100x100 thumbnail that maintains the aspect ratio of the original image, cropping any part that exceeds the specified size. The crop position is determined by the Position parameter passed to the crop method; this example uses Positions.CENTER. Writes the result directly to a file. ```java Thumbnails.of("path/to/image") .crop(Positions.CENTER) .size(100, 100) .toFile("path/to/thumbnail"); ``` -------------------------------- ### Create thumbnail to fit within size (Thumbnails.of().size().asBufferedImage()) Source: https://github.com/coobird/thumbnailator/wiki/Examples Creates a thumbnail that fits within the specified 200x200 dimensions while preserving the aspect ratio. The resulting thumbnail may be smaller than the specified size if the source image's aspect ratio differs; for example, a 300x400 source produces a 150x200 thumbnail. ```java BufferedImage originalImage = ImageIO.read(new File("original.png")); BufferedImage thumbnail = Thumbnails.of(originalImage) .size(200, 200) .asBufferedImage(); ``` -------------------------------- ### Builder interface usage with Thumbnails class Source: https://github.com/coobird/thumbnailator/wiki/Changes Shows the builder interface entry point using the Thumbnails class to create a thumbnail. This example is referenced in the summary to illustrate the builder interface mentioned in the release notes. ```java Thumbnails.of("path/to/image") .size(10, 10) .toFile("path/to/thumbnail"); ``` -------------------------------- ### Write thumbnails to a directory with Rename.PREFIX_DOT_THUMBNAIL Source: https://github.com/coobird/thumbnailator/wiki/Examples Creates 200x200 thumbnails from three source images and writes them to the specified destination directory, renaming each with 'thumbnail.' prepended. Output files are thumbnail.apple.jpg, thumbnail.banana.jpg, and thumbnail.cherry.jpg. ```java File destinationDir = new File("path/to/output"); Thumbnails.of("apple.jpg", "banana.jpg", "cherry.jpg") .size(200, 200) .toFiles(destinationDir, Rename.PREFIX_DOT_THUMBNAIL); ``` -------------------------------- ### Scale image by factor with Thumbnails.scale() Source: https://github.com/coobird/thumbnailator/wiki/Examples Creates a thumbnail that is 25% of the original image using the default scaling technique. Reads the original image from a file and returns the result as a BufferedImage. ```java BufferedImage originalImage = ImageIO.read(new File("original.png")); BufferedImage thumbnail = Thumbnails.of(originalImage) .scale(0.25) .asBufferedImage(); ``` -------------------------------- ### Create a thumbnail from an image file using File objects Source: https://github.com/coobird/thumbnailator/wiki/Examples Resizes original.jpg to 160x160 and saves to thumbnail.jpg. Uses File objects to specify the source and destination. ```java Thumbnails.of(new File("original.jpg")) .size(160, 160) .toFile(new File("thumbnail.jpg")); ``` -------------------------------- ### Create thumbnail with watermark using Thumbnails.of and watermark Source: https://github.com/coobird/thumbnailator/wiki/Examples Loads an original image and a watermark image, then creates a 200x200 thumbnail with the watermark positioned at BOTTOM_RIGHT and 0.5f opacity. The opacity argument ranges from 0.0f (completely transparent) to 1.0f (completely opaque). ```java BufferedImage originalImage = ImageIO.read(new File("original.jpg")); BufferedImage watermarkImage = ImageIO.read(new File("watermark.png")); BufferedImage thumbnail = Thumbnails.of(originalImage) .size(200, 200) .watermark(Positions.BOTTOM_RIGHT, watermarkImage, 0.5f) .asBufferedImage(); ``` -------------------------------- ### Create a thumbnail with rotation and a watermark Source: https://github.com/coobird/thumbnailator/wiki/Examples Resizes original.jpg to 160x160, rotates 90 degrees clockwise, adds a half-transparent watermark at the bottom-right, and saves with 80% compression quality. ```java Thumbnails.of(new File("original.jpg")) .size(160, 160) .rotate(90) .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("watermark.png")), 0.5f) .outputQuality(0.8) .toFile(new File("image-with-watermark.jpg")); ``` -------------------------------- ### Create a thumbnail and write to an OutputStream Source: https://github.com/coobird/thumbnailator/wiki/Examples Resizes large-picture.jpg to a maximum dimension of 200x200 (maintaining aspect ratio) and writes the result as a PNG to the specified OutputStream. ```java OutputStream os = ...; Thumbnails.of("large-picture.jpg") .size(200, 200) .outputFormat("png") .toOutputStream(os); ``` -------------------------------- ### Force exact thumbnail size with forceSize() Source: https://github.com/coobird/thumbnailator/wiki/Examples Creates a thumbnail that is exactly 200x200 pixels using the forceSize method. This is an alternative to using .size() with .keepAspectRatio(false) and also does not preserve the aspect ratio, potentially causing distortion. ```java BufferedImage originalImage = ImageIO.read(new File("original.png")); BufferedImage thumbnail = Thumbnails.of(originalImage) .forceSize(200, 200) .asBufferedImage(); ``` -------------------------------- ### Rotate image 90 degrees with Thumbnails.rotate() Source: https://github.com/coobird/thumbnailator/wiki/Examples Creates a 200x200 thumbnail rotated clockwise by 90 degrees from the original image. Reads the original from a file and returns the result as a BufferedImage. ```java BufferedImage originalImage = ImageIO.read(new File("original.jpg")); BufferedImage thumbnail = Thumbnails.of(originalImage) .size(200, 200) .rotate(90) .asBufferedImage(); ``` -------------------------------- ### Write thumbnails to a directory preserving original filenames with Rename.NO_CHANGE Source: https://github.com/coobird/thumbnailator/wiki/Examples Creates 200x200 thumbnails from three source images and writes them to the specified destination directory without renaming, preserving the original filenames. Output files are apple.jpg, banana.jpg, and cherry.jpg. Available since Thumbnailator 0.4.7. ```java File destinationDir = new File("path/to/output"); Thumbnails.of("apple.jpg", "banana.jpg", "cherry.jpg") .size(200, 200) .toFiles(destinationDir, Rename.NO_CHANGE); ``` -------------------------------- ### Force exact thumbnail size with keepAspectRatio(false) Source: https://github.com/coobird/thumbnailator/wiki/Examples Creates a thumbnail that is exactly 200x200 pixels by disabling aspect ratio preservation. The thumbnail may appear squashed or stretched if the source image's aspect ratio differs from the target size. ```java BufferedImage originalImage = ImageIO.read(new File("original.png")); BufferedImage thumbnail = Thumbnails.of(originalImage) .size(200, 200) .keepAspectRatio(false) .asBufferedImage(); ``` -------------------------------- ### Rotate thumbnails with fluent interface Source: https://github.com/coobird/thumbnailator/wiki/Features Uses Thumbnails.of with size and rotate to generate rotated thumbnails for angles 0, 90, 180, 270, and 45 degrees. The example writes each result to a file named image-rotated-.png. ```java for (int i : new int[] {0, 90, 180, 270, 45}) { Thumbnails.of(new File("coobird.png")) .size(100, 100) .rotate(i) .toFile(new File("image-rotated-" + i + ".png")); } ``` -------------------------------- ### Resize with width constraint using Thumbnails.width(int) Source: https://github.com/coobird/thumbnailator/wiki/Changes Creates a thumbnail by specifying only the width, preserving the aspect ratio. For a 400x300 image with width 200, the thumbnail becomes 200x150. Uses the width(int) method added in 0.3.5. ```java Thumbnails.of("/path/to/image-400x300") .width(200) .toFile("/path/to/thumbnail-200x150") ``` -------------------------------- ### Image.getScaledInstance with Image.SCALE_SMOOTH Source: https://github.com/coobird/thumbnailator/wiki/Comparison Resizing using Image.getScaledInstance with the SCALE_SMOOTH hint. The scaled image is drawn onto a new BufferedImage. ```java BufferedImage thumbnail = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g = thumbnail.createGraphics(); g.drawImage(originalImage.getScaledInstance(WIDTH, HEIGHT, Image.SCALE_SMOOTH), 0, 0, WIDTH, HEIGHT, null); g.dispose(); ``` -------------------------------- ### Configure watermark insets with .watermark(Position, BufferedImage, float, int) Source: https://github.com/coobird/thumbnailator/wiki/Changes Use this snippet to position a watermark with a specified inset from the corner of the thumbnail. The fourth argument sets the inset in pixels; here the watermark is placed five pixels from the bottom-right corner. Requires a BufferedImage for the watermark and a float opacity value. ```java // Position watermark so that the bottom-right corner is five pixels away // from the bottom-right corner of the thumbnail. Thumbnails.of("path/to/image") .size(100, 100) .watermark(Positions.BOTTOM_RIGHT, watermarkImage, 0.5f, 5) .toFile("path/to/thumbnail"); ``` -------------------------------- ### Create thumbnail from source region using Thumbnails.sourceRegion(Position, int, int) Source: https://github.com/coobird/thumbnailator/wiki/Changes Creates a 200x200 thumbnail from the center 400x400 region of the source image. Uses the sourceRegion(Position, int, int) method added in 0.3.4. ```java Thumbnails.of("/path/to/image") .sourceRegion(Positions.CENTER, 400, 400) .size(200, 200) .toFile("/path/to/thumbnail") ``` -------------------------------- ### Graphics.drawImage with bilinear interpolation Source: https://github.com/coobird/thumbnailator/wiki/Comparison Resizing using Graphics.drawImage with bilinear interpolation and antialiasing enabled. Sets rendering hints for smoother scaling. ```java BufferedImage thumbnail = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g = thumbnail.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.drawImage(originalImage, 0, 0, WIDTH, HEIGHT, null); g.dispose(); ``` -------------------------------- ### Image.getScaledInstance with Image.SCALE_DEFAULT Source: https://github.com/coobird/thumbnailator/wiki/Comparison Resizing using Image.getScaledInstance with the SCALE_DEFAULT hint. The scaled image is drawn onto a new BufferedImage. ```java BufferedImage thumbnail = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g = thumbnail.createGraphics(); g.drawImage(originalImage.getScaledInstance(WIDTH, HEIGHT, Image.SCALE_DEFAULT), 0, 0, WIDTH, HEIGHT, null); g.dispose(); ``` -------------------------------- ### Graphics.drawImage with bicubic interpolation Source: https://github.com/coobird/thumbnailator/wiki/Comparison Resizing using Graphics.drawImage with bicubic interpolation and antialiasing enabled. Sets rendering hints for higher-quality scaling. ```java BufferedImage thumbnail = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g = thumbnail.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.drawImage(originalImage, 0, 0, WIDTH, HEIGHT, null); g.dispose(); ``` -------------------------------- ### Using List with from... methods in Thumbnailator 0.4.0 Source: https://github.com/coobird/thumbnailator/wiki/Changes Works without modification in Thumbnailator 0.3.x and 0.4.x. The from...(Collection) methods were removed, but from...(Iterable) methods are functionally equivalent for most scenarios, so passing a List works the same as before. ```java List filenames = new ArrayList(); filenames.add("path/to/image1.jpg"); filenames.add("path/to/image2.jpg"); Thumbnails.of(filenames) .size(200, 200) .toFiles(Rename.PREFIX_DOT_THUMBNAIL); ``` -------------------------------- ### Create JPEG thumbnails from directory files with Thumbnails.of Source: https://github.com/coobird/thumbnailator/blob/master/README.md Uses Thumbnailator's fluent interface to create JPEG thumbnails of all image files in a directory, resized to a maximum dimension of 640x480 pixels while preserving aspect ratio. The output files are prefixed with 'thumbnail.' using Rename.PREFIX_DOT_THUMBNAIL. ```java Thumbnails.of(new File("path/to/directory").listFiles()) .size(640, 480) .outputFormat("jpg") .toFiles(Rename.PREFIX_DOT_THUMBNAIL); ``` -------------------------------- ### Thumbnailator resizing Source: https://github.com/coobird/thumbnailator/wiki/Comparison Resizing using Thumbnailator's fluent API. Creates a thumbnail of the original image with the specified size and returns it as a BufferedImage. ```java BufferedImage thumbnail = Thumbnails.of(originalImage) .size(WIDTH, HEIGHT) .asBufferedImage(); ``` -------------------------------- ### Graphics.drawImage resizing Source: https://github.com/coobird/thumbnailator/wiki/Comparison Conventional image resizing using Graphics.drawImage without interpolation hints. Creates a new BufferedImage and draws the original image scaled to WIDTH and HEIGHT. ```java BufferedImage thumbnail = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g = thumbnail.createGraphics(); g.drawImage(originalImage, 0, 0, WIDTH, HEIGHT, null); g.dispose(); ``` -------------------------------- ### Add Sonatype OSSRH snapshot repository to pom.xml Source: https://github.com/coobird/thumbnailator/wiki/Maven Add this repository to your pom.xml to obtain snapshot artifacts from Sonatype's OSSRH. Note that snapshot releases are not necessarily stable nor free of known bugs, and may not be always available. SNAPSHOT versions will be removed once the corresponding release version is available. ```xml ossrh https://oss.sonatype.org/content/repositories/snapshots true false ``` -------------------------------- ### Add Thumbnailator dependency to pom.xml Source: https://github.com/coobird/thumbnailator/wiki/Maven Add this dependency to the dependencies section of your pom.xml to fetch the newest available version of Thumbnailator in the 0.4.x version range. Replace [0.4, 0.5) with a specific version number like 0.4.3 if a specific version is required. ```xml net.coobird thumbnailator [0.4, 0.5) ```