Oyyi

Rotate Image

Rotate an image by a specified angle with control over canvas expansion and background color.

POST /api/image/rotate

Request Parameters

ParameterTypeRequiredDescription
fileFileYesThe image file to rotate. Supported formats: JPEG, PNG, WebP, BMP, TIFF, GIF.
angleFloatYesThe angle of rotation in degrees. Positive values rotate clockwise, negative values rotate counterclockwise. Range: -360 to 360.
qualityIntegerNoThe quality of the output image for lossy formats (JPEG, WebP). Range: 1-100. Default: 90.
center_xFloatNoThe x-coordinate of the center of rotation, as a fraction of the image width (0.0 to 1.0). Default: 0.5 (center).
center_yFloatNoThe y-coordinate of the center of rotation, as a fraction of the image height (0.0 to 1.0). Default: 0.5 (center).

Response

Returns the rotated image in the specified or original format.

Example Request

    // example.sh
curl -X POST "https://oyyi.xyz/api/image/rotate"   -H "accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -F "file=@image.jpg" \
  -F "angle=90" \
  --output rotated.jpg
  

Example with Python

    // rotate.py
import requests

url = "https://oyyi.xyz/api/image/rotate"

files = {
    'file': open('image.jpg', 'rb')
}

data = {
    'angle': 90
}

response = requests.post(url, files=files, data=data)

# Save the rotated image
with open('rotated.jpg', 'wb') as f:
    f.write(response.content)
  

Example with JavaScript

    // rotate.js
// Using fetch API
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('angle', '90');
formData.append('expand', 'true');
formData.append('background_color', 'white');

fetch('https://oyyi.xyz/api/image/rotate', {
  method: 'POST',
  body: formData
})
.then(response => response.blob())
.then(blob => {
  // Create a URL for the blob
  const url = URL.createObjectURL(blob);

  // Create a link to download the image
  const a = document.createElement('a');
  a.href = url;
  a.download = 'rotated.jpg';
  a.click();

  // Clean up by revoking the URL
  URL.revokeObjectURL(url);
})
.catch(error => console.error('Error:', error));
  

Common Rotation Angles

90°

Rotate clockwise by 90 degrees (quarter turn)

180°

Rotate by 180 degrees (upside down)

270° or -90°

Rotate counterclockwise by 90 degrees

45°

Rotate clockwise by 45 degrees (diagonal)

Expand Parameter

expand = true

The output image dimensions are increased to fit the entire rotated image. This ensures no part of the image is cropped, but results in a larger canvas with empty corners filled by the background color.

expand = false

The output image maintains the same dimensions as the input image. Parts of the rotated image that extend beyond the original boundaries will be cropped. This preserves the original canvas size.

How It Works

The rotate endpoint rotates an image by a specified angle:

  1. Image Loading: The uploaded image is loaded into memory.
  2. Center Determination: The center of rotation is calculated based on the center_x and center_y parameters.
  3. Canvas Preparation: If expand is true, a new canvas is created with dimensions large enough to fit the entire rotated image.
  4. Background Filling: The canvas is filled with the specified background color.
  5. Rotation: The image is rotated around the specified center by the given angle using interpolation to maintain image quality.
  6. Format Conversion: If an output_format is specified, the rotated image is converted to that format.
  7. Quality Setting: For lossy formats (JPEG, WebP), the specified quality level is applied.

The result is an image rotated by the specified angle, with appropriate handling of the canvas size and background.

Background Color

Common background color options:

  • white - Standard white background, good for documents and most images
  • black - Dark background, good for night scenes or artistic effects
  • transparent - Transparent background (only works with formats that support transparency: PNG, WebP)
  • Hex codes (e.g., #FF0000 for red) - Custom colors for specific design needs

Error Responses

Status CodeDescription
400Bad request. Missing required parameters, invalid angle value, invalid expand value, invalid background_color, invalid center coordinates, invalid output_format, invalid quality value, or invalid file format.
413Payload too large. The file size exceeds the maximum allowed limit.
500Internal server error. Something went wrong on the server.

Notes

  • For 90, 180, or 270 degree rotations, the image quality is perfectly preserved as these are lossless transformations.
  • For other angles, some interpolation is required, which may slightly reduce image quality.
  • When using expand=true with angles that aren't multiples of 90 degrees, the output image dimensions will be larger than the input.
  • The transparent background color only works with formats that support transparency (PNG, WebP). For JPEG and other formats without transparency support, transparent areas will be filled with white.
  • For animated GIFs, only the first frame will be rotated unless the output format is also GIF.
  • The maximum file size allowed is 50MB.
  • This operation preserves the alpha channel (transparency) if present in the original image and the output format supports it.

Use Cases

  • Correcting the orientation of photos taken in portrait or landscape mode
  • Straightening crooked or tilted images
  • Creating artistic compositions with angled elements
  • Preparing images for specific layout requirements
  • Converting between portrait and landscape orientations
  • Correcting the orientation of scanned documents
  • Creating rotated variations of logos or design elements
  • Implementing image editing functionality in applications