Crop Image with Format Change Introduction
This article explains how to crop an existing image. The API requires specifying the position as well as the dimensions of the cropping rectangle, whereas the format of the cropped image relies on the value specified by the format parameter. Please check Common Operations Format Support Map to know the supported conversion formats.
There are two APIs to crop an image:
The first API expects you to first upload an image to Cloud Storage then pass its name in the API URL. After updating the image parameters, the API returns the updated image in the response. If you would like to save the updated image on the Cloud Storage, you explicitly need to do this as shown in the below examples.
On the other hand, with the second API, you can directly pass the image in the request body. It also lets you save the updated image on the Cloud Storage by specifying the outPath parameter value. However, if you do not specify the value, the response contains a streamed image.
Resource URI
With Swagger UI you can call the REST API directly from the browser. Description of API parameters are also given there.
cURL Example
Cloud SDKs
Using an SDK (API client) is the quickest way for a developer to speed up the development. An SDK takes care of a lot of low-level details of making requests and handling responses and lets you focus on writing code specific to your particular project. Checkout our GitHub repository for a complete list of Aspose.Imaging Cloud SDKs along with working examples, to get you started in no time. Please check Available SDKs article to learn how to add an SDK to your project.
SDK Examples
.NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-imaging-cloud/aspose-imaging-cloud-dotnet
public void CropImageFromStorage()
{
// Input formats could be one of the following:
// BMP, GIF, DJVU, WMF, EMF, JPEG, JPEG2000, PSD, TIFF, WEBP, PNG, DICOM, CDR, CMX, ODG, DNG and SVG
string fileName = "WaterMark.bmp";
// Upload local image to Cloud Storage
using (FileStream localInputImage = File.OpenRead(ImagingBase.PathToDataFiles + fileName))
{
var uploadFileRequest = new UploadFileRequest(fileName, localInputImage);
FilesUploadResult result = this.ImagingApi.UploadFile(uploadFileRequest);
}
// Please refer to https://docs.aspose.cloud/display/imagingcloud/Supported+File+Formats#SupportedFileFormats-Crop
// for possible output formats
string format = "jpg"; // Resulting image format.
int? x = 10;
int? y = 10;
int? width = 100;
int? height = 150;
string folder = null; // Input file is saved at the root of the storage
string storage = null; // We are using default Cloud Storage
var request = new CropImageRequest(fileName, format, x, y, width, height, folder, storage);
Stream updatedImage = this.ImagingApi.CropImage(request);
// Save updated image to local storage
string outPath = "Watermark_out." + format;
using (var fileStream = File.Create(ImagingBase.PathToDataFiles + outPath))
{
updatedImage.Seek(0, SeekOrigin.Begin);
updatedImage.CopyTo(fileStream);
}
}
Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String APP_KEY = ""; // Get AppKey and AppSID from https:dashboard.aspose.cloud/
String APP_SID = ""; // Get AppKey and AppSID from https:dashboard.aspose.cloud/
String DATA_PATH = "src/main/resources/";
ImagingApi imagingApi = new ImagingApi(APP_KEY, APP_SID);
String fileName = "WaterMark.bmp";
try {
// Upload local image to storage
File inputFile = new File(Main.DATA_PATH + fileName);
FileInputStream localInputImageStream = new FileInputStream(inputFile);
byte[] localInputImage = IOUtils.toByteArray(localInputImageStream);
UploadFileRequest uploadFileRequest = new UploadFileRequest(fileName, localInputImage, null);
FilesUploadResult result = imagingApi.uploadFile(uploadFileRequest);
String format = "jpg"; // Resulting image format. Currently, BMP, PSD, JPG, TIFF, GIF, PNG, J2K and WEBP are supported.
Integer x = 10;
Integer y = 10;
Integer width = 100;
Integer height = 150;
String outPath = null; // Path to updated file (if this is empty, response contains streamed image)
String folder = null; // Input file is saved at the root of the storage
String storage = null; // We are using default Cloud Storage
GetImageCropRequest getImageCropRequest = new GetImageCropRequest(fileName, format, x, y,
width, height, outPath, folder, storage);
byte[] updatedImage = imagingApi.getImageCrop(getImageCropRequest);
// Save updated image to local storage
FileOutputStream fos = new FileOutputStream(Main.DATA_PATH + "Watermark_out." + format);
fos.write(updatedImage);
fos.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
.NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-imaging-cloud/aspose-imaging-cloud-dotnet
public void CreateCroppedImageFromRequestBody()
{
// Input formats could be one of the following:
// BMP, GIF, DJVU, WMF, EMF, JPEG, JPEG2000, PSD, TIFF, WEBP, PNG, DICOM, CDR, CMX, ODG, DNG and SVG
string fileName = "WaterMark.bmp";
using (FileStream inputImageStream = File.OpenRead(ImagingBase.PathToDataFiles + fileName))
{
// Please refer to https://docs.aspose.cloud/display/imagingcloud/Supported+File+Formats#SupportedFileFormats-Crop
// for possible output formats
string format = "jpg"; // Resulting image format.
int? x = 10;
int? y = 10;
int? width = 100;
int? height = 150;
string storage = null; // We are using default Cloud Storage
string outPath = null; // Path to updated file (if this is empty, response contains streamed image)
var request = new CreateCroppedImageRequest(inputImageStream, format, x, y, width, height, outPath, storage);
Stream updatedImage = this.ImagingApi.CreateCroppedImage(request);
// Save updated image to local storage
using (var fileStream = File.Create(ImagingBase.PathToDataFiles + "Watermark_out." + format))
{
updatedImage.Seek(0, SeekOrigin.Begin);
updatedImage.CopyTo(fileStream);
}
}
}
Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String APP_KEY = ""; // Get AppKey and AppSID from https:dashboard.aspose.cloud/
String APP_SID = ""; // Get AppKey and AppSID from https:dashboard.aspose.cloud/
String DATA_PATH = "src/main/resources/";
ImagingApi imagingApi = new ImagingApi(APP_KEY, APP_SID);
try {
File file = new File(Main.DATA_PATH + "WaterMark.bmp");
byte[] imageData = Files.readAllBytes(file.toPath());
String format = "jpg"; // Resulting image format. Currently, BMP, PSD, JPG, TIFF, GIF, PNG, J2K and WEBP are supported.
Integer x = 10;
Integer y = 10;
Integer width = 100;
Integer height = 150;
String outPath = null; // Path to updated file (if this is empty, response contains streamed image)
String storage = null; // We are using default Cloud Storage
PostImageCropRequest postImageCropRequest = new PostImageCropRequest(imageData, format, x, y, width, height,
outPath, storage);
byte[] updatedImage = imagingApi.postImageCrop(postImageCropRequest);
// Save updated image to local storage
FileOutputStream fos = new FileOutputStream(Main.DATA_PATH + "Watermark_out." + format);
fos.write(updatedImage);
fos.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}