惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
V
V2EX
博客园_首页
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
H
Help Net Security
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
云风的 BLOG
云风的 BLOG
D
Docker
Security Archives - TechRepublic
Security Archives - TechRepublic
Help Net Security
Help Net Security
N
News and Events Feed by Topic
Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
A
Arctic Wolf
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
博客园 - 三生石上(FineUI控件)
aimingoo的专栏
aimingoo的专栏
Hacker News: Ask HN
Hacker News: Ask HN
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy International News Feed
T
Troy Hunt's Blog
T
Tenable Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Recorded Future
Recorded Future
F
Fortinet All Blogs
D
DataBreaches.Net
B
Blog
T
Threat Research - Cisco Blogs
MyScale Blog
MyScale Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
The GitHub Blog
The GitHub Blog
Security Latest
Security Latest
M
MIT News - Artificial intelligence

ImageKit.io Blog

Next.js Image Optimization with ImageKit Use Video as a Background in Your Next.js Project How to Fix Autoplay Video in Next.js How Durian Scaled a Visual-First Retail Experience to 350K Monthly Visitors Online How Matsmart accelerated image delivery across countries with ImageKit AI in Digital Asset Management: From Smart Workflows to Agentic Automation How Joseph Joseph unified and secured global video delivery with ImageKit How Modall powers fast, effortless media delivery across 40+ projects with ImageKit Digital Asset Management (DAM) Trends: 2026 Report How to add a poster image to Video.js player (and automate it) HLS streaming with Video.js + React Building the future of storytelling with fast, AI-powered video delivery How PushOwl delivers 100M+ image-rich notifications seamlessly with ImageKit How Homify delivers millions of interior design images seamlessly with ImageKit Better event discovery with lightning‑fast videos & images Adding video player in React Native Video player in Angular applications Crop and resize videos in React Next.js image and video upload React image and video upload React video optimization How we quadrupled our traffic to 625K monthly page views How Apollo 24|7 boosted performance & reduced costs with ImageKit Simplify your media workflows with ImageKit DAM integrations Extending Lighthouse for custom image and video optimization analysis Brand Asset Management: What is it? How does it work? WordPress Digital Asset Management Guide - Manage your WP media assets better Why Shopify retailers need a digital asset management solution DAM vs. SharePoint: Which is best for you? AI-powered Metadata and Tagging in Digital Asset Management How Hopscotch built India's largest online Kids' fashion brand with ImageKit Dropbox Vs. DAM: Which Is The Right Tool For Digital Asset Management Digital Asset Management for Photographers: A Complete Guide Why digital asset management for agencies is essential Helping both Top and Bottom Line: SaffronStays rapid, profitable growth with ImageKit How KreditBee simplified media experiences with ImageKit Google Drive alternatives for businesses (with fast-growing teams) Node.js image upload ImageKit: The Secret Ingredient in Swiggy’s Expansion Journey Streamlining the Design Approval Process: A Comprehensive Guide AV1 Codec - Complete guide for video application devs PHP image and video upload Angular image & video upload AV1 vs VP9: Which codec should you choose? Adding video player in Next.js React Video Player VP8 vs VP9 - In the context of online video delivery Exploring WebM vs MP4 7 Free Digital Asset Management Software that are not Open-Source Comparing 9 Top Digital Asset Management Tools in the Market What are Brand Standards and Why do they Matter? Boost Sales and Brand Appeal: Essential Tips for eCommerce Image Management Brand Recall: The Strategy to Create Unforgettable Brands How to upload files in HTML? Branding for Small Businesses (2025 Edition) Everything you need to know about VP9 codec Recent updates from ImageKit and what's next Best Ways to Write RFP For Digital Asset Management (+ with free RFP template) What is Brand Dilution? How to Avoid It? Explained with [Examples] The Importance of Brand Identity: Leveraging Digital Asset Management for Impact From Launch to Scale: How to Launch a Brand Campaign Digital Asset Management Requirements - What do You Need to Evaluate and How? Marketing Collateral Management: A Quick End-to-End Guide Video Content Management System: What Is It And How To Choose One? Dropbox vs. Google Drive vs. Onedrive: The Best Cloud Storage Solution How to Build Brand Trust: Get Started In 2025 Google Drive vs. Box: A Detailed Comparison How Digital Asset Management Solutions Help Protect Brand Equity A DAM Solution Can Safeguard Your Digital Intellectual Property - Here’s How WebP Vs. PNG: Which Image Format Should You Use and Why? How to Resize Images in Bootstrap Easily Progressive jpegs (PJPEG): the key to loading images faster on your website Dropbox vs. Google Drive: The Best Cloud Storage For Digital Assets Dropbox Pros & Cons In 2024: An In-Depth Analysis and Why A DAM Solution Stands Out Google Drive Vs OneDrive: The Better Storage Option For Digital Assets Manage your video assets better with video metadata Understanding DAM's Role in Strengthening Brand Identity Digital asset management strategy: What to know before creating one The Ultimate Guide To Marketing Agency Onboarding 6 Solutions To Simplify Large File Sharing Over The Web A Step-by-Step Breakdown of a Video Production Workflow 13 Digital Asset Management Use Cases You Should Know How to Conduct a Brand Audit and Manage Your Brand Assets Costly Consequences of Inconsistent Branding And How DAM Can Help Dynamic Asset Transformation: What It Is, Why You Need It, and How ImageKit Can Help Everything You Need to Know About HTML Video Autoplay How To Select Your DAM Vendor: A Complete Guide How to Boost User Experience with Smart Digital Asset Management React Image Optimization: A Guide for Web Developers Why Should DAM Be A Part Of Your MarTech Stack? Unleashing the Power of Content Repurposing with ImageKit MKV vs MP4: Which Video File Format Is Better for Your Needs? Digital Asset Management For Ecommerce: A Complete Guide How an Image Tagging Software can Transform Your Image Search How to Manage Your Content Lifecycle Effectively M4V vs MP4: Which Video Format Should You Use and Why? Why Every Business Needs An Image Management System All The Questions To Ask During A Dam Demo Which is the Best Image Format for Your Website? Uploading Multiple Files Using JavaScript: A Comprehensive Guide
Image resizing in Java
Manu Chaudhary · 2020-12-15 · via ImageKit.io Blog

There are a couple of ways to resize & crop images in Java. This article will briefly compare different methods of resizing images in Java, along with pros and cons.

More importantly, after you finish reading, you will learn a novel approach to resizing images in Java applications, which doesn’t require writing any image resizing code or maintaining servers to handle a large volume of image manipulation requests.

There are broadly four ways to resize images in Java:-

  • Native Java - It does not require any third-party libraries.
  • Open-source image manipulation libraries - For example, ImageMagick.
  • Open source imaging service like Thumbor.
  • Free and paid Image CDN e.g. ImageKit.io.
MethodProsCons
Native Java functions
  • No third party library is required.
You will have to deal with a lot of low-level image processing and file operation.
Open source image processing libraries
  • High image quality.
  • More control over resizing and cropping options.
  • Advance features e.g. watermarking and text overlay.
You will have to install third-party libraries and find relevant options to get the desired results.
Open source thumbnail general service
  • Expressive API to resize & crop images for web pages.
  • High image quality.
  • More control over resizing and cropping options.
You will have to install a third-party service and maintain the infrastructure.
Free or paid image CDN
  • No third party image processing library required.
  • Since you are not running the service, you don’t have to maintain the infrastructure.
  • Get started within a few minutes.
  • Expressive API to resize & crop images for web pages.
  • Advance features such as smart cropping, object-aware cropping, and face detection.
  • More control over resizing and cropping options.
  • High image quality.
An overkill if you want to resize a handful of images. If you are serving images on the web, then you should be using an image CDN.

In this post, we will deep dive into different methods of resizing and cropping using ImageKit.io URL-parameters.

Image resizing in Java using ImageKit.io

We will learn the following image manipulation in Java using ImageKit.io. It doesn’t require you to install any third-party image processing library. You can scale from zero to millions of image requests per minute without having to worry about scaling or maintaining infrastructure.

  • Basic image resizing, height and width manipulation
  • Cropping & preserving aspect ratio while resizing images
  • Add watermark in images
  • Adding text over images
  • Generating encrypted secured image URLs
  • Converting image format

You can manipulate the height and width of an image using simple URL parameters. Your backend can generate these URLs in Java or in the frontend itself.

For example, the original image is 1280x853px.

https://ik.imagekit.io/ikmedia/woman.jpg
Original 1280x853 image
Original 1280x853 image

To get a 200px wide image, use tr=w-200 query parameter

https://ik.imagekit.io/ikmedia/woman.jpg?tr=w-200
200px wide image
200px wide image

To resize image to 400 width and 200 height, you can use tr=w-400,h-200

https://ik.imagekit.io/ikmedia/woman.jpg?tr=w-400,h-200
400x200 image
400x200 image

Using ImageKit.io Java SDK, you can quickly generate image URLs like this:

List<Map<String, String>> transformation = new ArrayList<Map<String, String>>();
Map<String, String> scale = new HashMap<>();
scale.put("height","200");
scale.put("width","400");
transformation.add(scale);
   
Map<String, Object> options = new HashMap();
options.put("urlEndpoint","https://ik.imagekit.io/ikmedia/");
options.put("path","/woman.jpg");
options.put("transformation", transformation);

String url = ImageKit.getInstance().getUrl(options);
// https://ik.imagekit.io/ikmedia/tr:w-400,h-200/woman.jpg?ik-sdk-version=java-VERSION

Cropping & preserving aspect ratio while resizing images in Java

If only one of the height(h) or width(w) is specified, then ImageKit.io adjusts the other dimension accordingly to preserve the aspect ratio, and no cropping occurs.

But when you specify both height(h) and width(w) dimension and want to preserve the aspect ratio - you have the following three options:

  • Crop some part of the image. You can choose which area to crop, where to focus etc.
  • Add padding around the image. You can control the background color of the padding to match the layout.
  • Let ImageKit change either height or width so that the whole image is visible. In this case, only one of the height or width matches the request dimension.

Let’s understand different cropping options with examples.

No cropping, force-fit image in requested dimensions

If you need an image in exact dimension as requested even if the aspect ratio is not preserved, use forced crop strategy.

https://ik.imagekit.io/ikmedia/woman.jpg?tr=w-200,h-200,c-force
Force fit
Force fit

Default center cropping

If you don’t choose any cropping strategy, ImageKit.io will crop the edges by default and show the center of the image.

https://ik.imagekit.io/ikmedia/woman.jpg?tr=w-200,h-200
Default center crop
Default center crop

Fit inside the container (no cropping)

If you want the image to fit inside the container as per request height and width, use c-at_max. The full image content is preserved (no cropping), the aspect ratio is maintained, but the resulting height & width might differ from what is requested.

The output image is less than or equal to the dimensions specified in the URL, i.e., at least one dimension will exactly match the output dimension requested, and the other dimension will be equal to or smaller than the corresponding output dimension requested.

It is equivalent to object-fit:contain or background-size:contain CSS properties.

https://ik.imagekit.io/ikmedia/woman.jpg?tr=w-400,h-200,c-at_max
300x200 image fits inside 400x200 container
300x200 image fits inside 400x200 container

Fill container (no cropping)

If you want the image to cover the whole container as per the requested height and width, use c-at_least. The entire image content is preserved i.e. no cropping, the aspect ratio is maintained, but the resulting height & width might be different from what is requested.

One of the dimensions will be the same as what is requested, while the other dimension will be equal to or larger than what is asked for.

It is roughly equivalent to object-fit:cover or background-size:cover CSS properties.

https://ik.imagekit.io/ikmedia/woman.jpg?tr=w-400,h-200,c-at_least
400x267 image covers 400x200 container
400x267 image covers 400x200 container

No cropping, add padding around the image

If you don’t want the image to be cropped while maintaining the aspect ratio, you can add padding around the edges to get the desired dimension. You can also control the background color of this padding.

https://ik.imagekit.io/ikmedia/woman.jpg?tr=w-200,h-100,cm-pad_resize,bg-DDDDDD
200x100 image with padding
200x100 image with padding

Smart cropping

ImageKit.io can intelligently crop image such that the object of interest remains in the center using smart cropping. In the thumbnail generated below, the woman is kept in the center.

https://ik.imagekit.io/ikmedia/woman.jpg?tr=w-200,h-200,fo-auto
Smart crop
Smart crop

Object-aware cropping:

If your image contains multiple objects and you want to crop out a specific one, object-aware cropping allows you to do so using the object's name. Use the parameter  fo-<object name>

With over 70 objects available to choose from, you can precisely isolate and crop the desired item from your image.

Watch how by using fo-couch the image gets automatically cropped to showcase the couch and fo-chair gives you the cropped image of the chair.

Here's how the URLs look like when you apply these transforms:

https://ik.imagekit.io/ikmedia/Graphics/Furniture.jpg?tr=fo-chair

https://ik.imagekit.io/ikmedia/Graphics/Furniture.jpg?tr=fo-couch

Face cropping

You can crop and focus around the human face using fo-face parameter.

Face crop
Face crop

Add watermark in images in Java

You can protect your images by adding your logo over them. If you try to achieve this in Java or using open-source image processing libraries, you will have to deal with low-level file operations and library-specific technicalities.

With ImageKit, it is straightforward to add a watermark in images in Java. Let’s say we want to put our logo on an image.

Logo can be accessed at -

https://ik.imagekit.io/ikmedia/logo/light-icon_GTyhLlWNX-.svg

The image we want to watermark -

https://ik.imagekit.io/ikmedia/woman.jpg?tr=w-600

Using the overlay image layer, we can generate a watermarked image.

https://ik.imagekit.io/ikmedia/woman.jpg?tr=l-image,i-logo@@light-icon_GTyhLlWNX-.svg,l-end
Watermarked image
Watermarked image

You can also control the position, size, and other manipulation options of the overlay image. Learn more about layer from our docs.

Adding text over images in Java

You can add text over images in Java using ImageKit.io text overlay layer. You can create engaging visuals in Java using URL-based parameters to add watermark and text on images dynamically.

For example - We have added l-text,i-Sky is the limit,l-end in the URL to add a string “Sky is the limit” over the image.

https://ik.imagekit.io/ikmedia/backlit.jpg?tr=l-text,i-Sky is the limit,bg-FFFFFF90,pa-20,fs-70,co-00000,lx-N0,ly-30,l-end
Text overlay
Text overlay

You can generate photo collage in Java using the same methods. Learn more about how to create a photo collage using ImageKit.

You can use Java SDK to simplify URL generation in your backend application.

List<Map<String, String>> transformation = new ArrayList<Map<String, String>>();
Map<String, String> overlayUsingRaw = new HashMap<>();
overlayUsingRaw.put("raw","l-text,i-Sky is the limit,bg-FFFFFF90,pa-20,fs-70,co-00000,lx-N0,ly-30,l-end");
transformation.add(overlayUsingRaw);
    
Map<String, Object> options = new HashMap();
options.put("urlEndpoint","https://ik.imagekit.io/ikmedia/");
options.put("path","/backlit.jpg");
options.put("transformation", transformation);

String url = ImageKit.getInstance().getUrl(options);
// https://ik.imagekit.io/ikmedia/backlit.jpg?tr=l-text,i-Sky is the limit,bg-FFFFFF90,pa-20,fs-70,co-00000,lx-N0,ly-30,l-end

Generating encrypted secured image URLs in Java

At times, you might want to secure access to your media assets to limit misuse. Or you might want to set a time-based expiry on the image URLs to make it hard for web scarpers to use your image URLs.

To solve this, you can create signed image URLs in your backend application written in Java. A signed URL is a secure URL that can be generated only by you using your account's private key.

For example, let’s create a signed URL that expires in 300 seconds with the default URL endpoint and other query parameters:

List<Map<String, String>> transformation=new ArrayList<Map<String, String>>();
Map<String, String> scale=new HashMap<>();
scale.put("height","600");
scale.put("width","400");

transformation.add(format);

Map<String, Object> options=new HashMap();
options.put("path","/default-image.jpg");
options.put("signed",true);
options.put("expireSeconds",300);
String url = ImageKit.getInstance().getUrl(options);
// https://ik.imagekit.io/your_imagekit_id/tr:h-600,w-400/default-image.jpg?ik-t=1567358667&ik-s=f2c7cdacbe7707b71a83d49cf1c6110e3d701054&ik-sdk-version=java-VERSION

If someone tries to modify the image transformation or use it beyond its intended expiry time of 300 seconds, a 401 Unauthorised response is returned instead of the image.

Converting image format in Java

In Java, you can convert image format using write() function provided by the class ImageIO under javax.imageio package. The supported output formats are JPG, JPEG, PNG, BMP, WBMP, and GIF.

However, you will have to deal with low-level file operations and image buffer. There is a different and easy way to convert image format in Java. Using an image CDN like ImageKit, you can easily convert image format using URL based transformation.

For example - The actual format of the following image is JPG.

https://ik.imagekit.io/ikmedia/woman.jpg

You can convert it to PNG using tr=f-png transformation.

https://ik.imagekit.io/ikmedia/woman.jpg?tr=f-png

You can covert it to WebP using tr=f-webp

https://ik.imagekit.io/ikmedia/woman.jpg?tr=f-webp

ImageKit also offers features such as automatic best format selection, quality optimization, and metadata manipulation out of the box. This reduces the final size of the output image and improves your website's images load time while maintaining visual quality.

Summary

Here is what you need to know:

  • If you only have a handful of images, use Java’s native image manipulation using the native Java ImageIO class.
  • If you need easy to use image resizing capabilities, go for a free image CDN like ImageKit.io. With a forever free plan, you get 20GB of viewing bandwidth every month. It is sufficient for small scale businesses.