Eskillsfy

Home / Blog / Images

Images · 23 Nov 2026

Rotate & Flip Image: how it works and how to build one

Rotate by 90°, straighten by any angle and mirror images. This guide explains what happens behind the button and shows the code, so you can build your own.

What it does

Fix sideways phone photos, straighten a crooked horizon with a fine angle slider, or mirror an image horizontally or vertically. Corners created by straightening can stay transparent or be filled.

When to use it

  • Fixing sideways phone photos
  • Mirror selfies
  • Straightening scans

How to use it

  1. Choose or drop an image.
  2. Rotate, straighten or flip it.
  3. Download the result.

Try it free in your browser, nothing is uploaded: https://eskillsfy.com/tools/rotate-flip-image

How it works

Rotating on a canvas means moving the origin to the center, rotating the drawing context, and drawing the image centered. For 90° turns, width and height swap. For any angle, the new canvas must be big enough to hold the rotated corners: w·|cos| + h·|sin|.

Flipping is scale(-1, 1) for horizontal or scale(1, -1) for vertical.

Build it yourself

Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any TypeScript or JavaScript project.

rotate.ts (ts)

export function rotate(img: HTMLImageElement, deg: number, flipH = false) {
  const r = (deg * Math.PI) / 180, w = img.naturalWidth, h = img.naturalHeight;
  const W = Math.abs(w * Math.cos(r)) + Math.abs(h * Math.sin(r));
  const H = Math.abs(w * Math.sin(r)) + Math.abs(h * Math.cos(r));
  const c = Object.assign(document.createElement("canvas"), { width: Math.round(W), height: Math.round(H) });
  const g = c.getContext("2d")!;
  g.translate(W / 2, H / 2);
  g.rotate(r);
  if (flipH) g.scale(-1, 1);
  g.drawImage(img, -w / 2, -h / 2);
  return c;
}

Make it production-ready

  • Read EXIF orientation from phone photos and apply it automatically.

Questions

Why is my phone photo sideways?

Some apps ignore the rotation stored in the photo’s EXIF data. Rotating here bakes the rotation into the pixels so it shows correctly everywhere.

Try the Rotate & Flip Image

Free, runs in your browser, nothing is uploaded.

Open it
Keep reading

More in Images

All posts