A picture fitted into a rectangle

A picture that comes from outside rarely has the proportions of the rectangle it has to go in. Stretched to fill the rectangle, it loses its proportions; kept at its own size, it leaves empty space around it or runs past the edge. That choice comes up again with every catalogue and every report you produce.

So the decision is made once and named: fit the whole picture inside, or fill the rectangle and let the edges be cut, or stretch it and accept the distortion, or leave it at the size it came. The same picture is shown here under eight such rules in a rectangle of one size — stood against an edge, given a margin, turned a quarter, turned by an angle, mirrored — and nothing on the page was measured by hand. Only the one that fills its rectangle overflows, and it is cut at the edge, on purpose.

What this example shows

  • Fit the whole picture inside, fill the rectangle and cut what overflows, or stretch it to the edges.
  • Nine places to stand it, of which this example uses one, top left; a margin inside the rectangle, three turns and a mirror.
  • The same picture in the same rectangle eight times, so the eight ways of fitting it can be compared.

The source code, complete

Nothing is left out and nothing is simplified: this is the program exactly as it runs, for the developer who uses this library. The third is the description itself: a plain list of what the page has to say, which is what the service is sent.

Read the Rust source Read the Python source Read the JSON source

The document itself

Open the document in English Open the document in French

A picture fitted into a rectangle, first page

Optimised, this document weighs 17.9 kB — at least 80 % lighter than most PDF generators would write it

The document is lighter than it was, and every page still looks exactly the same. See this document's row in the table of every example.

Examples near this one

Images on a page

A photograph that goes in exactly as it came out of the camera, and one with transparent parts that keeps them, each on a page cut to its own size.

A gallery of picture formats

The four picture formats read — JPEG, PNG, BMP and TIFF — shown across every picture shipped with the library, each captioned with what its own file declares.

Code 128 and retail barcodes

Stripes for the two jobs that want them: Code 128 for any text at all, and the three a till reads — EAN-13, UPC-A, EAN-8 — each with its last digit worked out rather than typed.

QR codes

The code an invoice, a boarding pass or a label ends up wanting, drawn into the document rather than fetched as a picture and pasted in.

Data Matrix codes

The code industry marks small parts with: still readable at sizes where a QR code has given up, on a circuit board or a phial in a laboratory tray.

Every example, by subject