PerfectVector
Studio
By Irene Kim

SVG feImage: Fit, Crop, or Stretch a Filter Image

Place an SVG filter image with feImage preserveAspectRatio. Compare meet, slice, and none in the same filter primitive region.

4 min read
On this page

Set preserveAspectRatio on feImage to choose how an image fits inside the filter primitive's rectangle. Use xMidYMid meet to center the entire image, xMidYMid slice to fill the rectangle and crop any excess, or none to stretch the image to both dimensions. W3C Filter Effects Level 1

Find the rectangle your image fits

The filter primitive subregion is the rectangle for one primitive. When you set x, y, width, and height on feImage, those values define the rectangle used here. primitiveUnits="userSpaceOnUse" makes the numbers use the current user coordinate system. The outer filter has its own region and can also clip the result. Filter primitive subregions

SettingWhat stays visible when a wide image goes into a square
xMidYMid meetBoth ends remain visible. There is space above and below.
xMidYMid sliceThe square fills without stretching. The left and right ends are cropped.
noneBoth ends remain visible and the square fills. The image changes proportions.

With meet or slice, xMidYMid centers the fit on both axes. Choose a different alignment when one edge contains something you need to keep. W3C SVG 2 aspect-ratio rules

Hold the filter rectangle steady and change the fit

Save this example as an SVG. The embedded 12 × 6 PNG has orange columns at both ends and a green center mark. The peach rectangle behind the filtered object makes uncovered space visible. The filter and primitive bounds are both 120 × 120, so switching one attribute isolates the effect of the fit.

<svg xmlns="http://www.w3.org/2000/svg" width="120" height="120"
     viewBox="0 0 120 120">
  <defs>
    <filter id="fit" x="0" y="0" width="120" height="120"
            filterUnits="userSpaceOnUse" primitiveUnits="userSpaceOnUse">
      <feImage x="0" y="0" width="120" height="120"
               href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAGCAIAAAB4jOjWAAAAHUlEQVR4nGN40uQiZxsCR9pNJUCELAJUwDBUFQEAvuJCVaRaCrYAAAAASUVORK5CYII="
               preserveAspectRatio="xMidYMid meet"/>
    </filter>
  </defs>
  <rect width="120" height="120" fill="#f6d9b9"/>
  <rect width="120" height="120" filter="url(#fit)"/>
</svg>

Open the file, then make two copies that replace xMidYMid meet with xMidYMid slice and none. Compare the left edge near the vertical center and the top edge near the horizontal center.

In our resvg 0.47.0 renders, meet kept orange at (3, 60) and exposed the peach background at (60, 3). With slice, the left sample changed to navy and the top sample was covered by the image. With none, the left sample stayed orange and the image covered the top. These samples distinguish crop, padding, and stretch for this fixture.

Three square feImage renders showing full motif with top and bottom space, cropped sides, and a stretched motif
The rectangle and source image stay fixed. Watch the orange edges and the peach background above the motif.

Check the fit before combining the image with another input

When feImage is one input in a larger filter, use it as the last primitive in a disposable copy. Inspect the resulting image and its bounds before restoring the composite operation. This checks the fit independently of which image a composite keeps or removes.

Changing preserveAspectRatio on the root SVG changes how the whole view fits its viewport. For a mismatch isolated to the filter image, change the attribute on feImage. Use the SVG preserveAspectRatio guide if the framing problem affects the entire SVG. For an ordinary image placed outside a filter, this fit can be set on the SVG image element. W3C SVG 2 image element

FAQ

What is the default fit for feImage?

The default is xMidYMid meet. It centers the image and scales it uniformly to keep the whole image inside the primitive rectangle.

What is the difference between meet and slice?

Both preserve the source aspect ratio. Meet keeps the entire image visible. Slice fills the rectangle by cropping any portion outside it.

Does preserveAspectRatio="none" crop the image?

It stretches the source to the target width and height. A wide motif can become taller relative to its width when you stretch it into a square.

Start with paths when the motif needs editing

PerfectVector can trace a flat raster motif into editable SVG paths. If you need to change its individual shapes in an SVG editor, vectorize the source image and check that its paths can be selected separately before reusing it. Use the fit settings above when you only need to place the source image inside a filter.

Sources

  1. W3C Filter Effects Level 1, feImage — Defines the image primitive and its default fit.
  2. W3C SVG 2, preserveAspectRatio — Defines the aspect fit and alignment values.
  3. W3C SVG 2, the image element — Documents aspect fit for an image placed outside a filter.

More from the blog

PerfectVector

Start with a cleaner SVG
that is easier to edit

Vectorize and inspect previews for free, with no daily limit.