RotateFlip an Image Introduction
This article explains how to rotate and/or flip an existing image. The RotateFlip method parameter value can be one of the following:
Rotate180FlipNone, Rotate180FlipX, Rotate180FlipXY, Rotate180FlipY, Rotate270FlipNone, Rotate270FlipX, Rotate270FlipXY, Rotate270FlipY, Rotate90FlipNone, Rotate90FlipX, Rotate90FlipXY, Rotate90FlipY, RotateNoneFlipNone, RotateNoneFlipX, RotateNoneFlipXY or RotateNoneFlipY.
You can rotate and/or flip an image using one of the following two APIs:
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
Aspose.Imaging Cloud APIs Swagger UI lets you call REST APIs directly from the browser. The description of the API and its parameters are also given there.
cURL Example
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. Check out 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 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 RotateFlipImageFromStorage()
{
// 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 = "Sample.psd";
// 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-RotateFlip
// for possible output formats
string format = "gif";
string method = "Rotate90FlipX"; // RotateFlip method
string folder = null; // Folder with image to process.
string storage = null; // We are using default Cloud Storage
RotateFlipImageRequest getImageRotateFlipRequest = new RotateFlipImageRequest(fileName, format,
method, folder, storage);
Stream updatedImage = this.ImagingApi.RotateFlipImage(getImageRotateFlipRequest);
// Save updated image to local storage
using (var fileStream = File.Create(ImagingBase.PathToDataFiles + "Sample_out." + format))
{
updatedImage.Seek(0, SeekOrigin.Begin);
updatedImage.CopyTo(fileStream);
}
}
Java
This file contains 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 = "Sample.psd";
try {
// Upload local image to Cloud 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 = "gif";
String method = "Rotate90FlipX"; // RotateFlip method
String outPath = null; // Path to updated file (if this is empty, response contains streamed image).
String folder = null; // Folder with image to process.
String storage = null; // We are using default Cloud Storage
GetImageRotateFlipRequest getImageRotateFlipRequest = new GetImageRotateFlipRequest(fileName, format,
method, outPath, folder, storage);
byte[] updatedImage = imagingApi.getImageRotateFlip(getImageRotateFlipRequest);
// Save updated image to local storage
FileOutputStream fos = new FileOutputStream(Main.DATA_PATH + "Sample_out.gif");
fos.write(updatedImage);
fos.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
.NET
This file contains 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 CreateRotateFlippedImageFromRequestBody()
{
// 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 = "Sample.psd";
using (FileStream inputImageStream = File.OpenRead(ImagingBase.PathToDataFiles + fileName))
{
// Please refer to https://docs.aspose.cloud/display/imagingcloud/Supported+File+Formats#SupportedFileFormats-RotateFlip
// for possible output formats
String format = "gif";
String method = "Rotate90FlipX"; // RotateFlip method
String outPath = null; // Path to updated file (if this is empty, response contains streamed image).
String storage = null; // We are using default Cloud Storage
CreateRotateFlippedImageRequest createRotateFlippedImageRequest = new CreateRotateFlippedImageRequest(inputImageStream, format,
method, outPath, storage);
Stream updatedImage = this.ImagingApi.CreateRotateFlippedImage(createRotateFlippedImageRequest);
// Save updated image to local storage
using (var fileStream = File.Create(ImagingBase.PathToDataFiles + "Sample_out." + format))
{
updatedImage.Seek(0, SeekOrigin.Begin);
updatedImage.CopyTo(fileStream);
}
}
}
Java
This file contains 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 + "Sample.psd");
byte[] imageData = Files.readAllBytes(file.toPath());
String format = "gif";
String method = "Rotate90FlipX"; // RotateFlip method
String outPath = null; // Path to updated file (if this is empty, response contains streamed image).
String storage = null; // We are using default Cloud Storage
PostImageRotateFlipRequest postImageRotateFlipRequest = new PostImageRotateFlipRequest(imageData, format,
method, outPath, storage);
byte[] updatedImage = imagingApi.postImageRotateFlip(postImageRotateFlipRequest);
// Save updated image to local storage
FileOutputStream fos = new FileOutputStream(Main.DATA_PATH + "Sample_out.gif");
fos.write(updatedImage);
fos.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}