Plugin

Archived
Forum
(read-only)

CE Image

ExpressionEngine 1.x, ExpressionEngine 2, ExpressionEngine 3

Back to this add-on's main page
View Other Add-ons From Causing Effect

     

Equivalent of CSS background-size: contain, aka. add padding to an image

Feature (Resolved)

Michael C.
Michael C.

I’d like to be able to resize images to fit within specified dimensions, without cropping the images, and with the resulting image actually having the specified dimensions.

For example, if I take an all-red 200x100 image and do this:

{exp:ce_img:single src="source_200x100.jpg" width="100" height="100"

The resulting image will be a red 100x50px rectangle. If I add the crop parameter:

{exp:ce_img:single src="source_200x100.jpg" width="100" height="100" crop="yes"

The resulting image will be a red 100x100 square, but missing 25% of the original image contents on each side.

What I want is a new parameter, perhaps called “pad” or “contain”, that interfaces with the existing “bg_color” parameter:

{exp:ce_img:single src="source_200x100.jpg" width="100" height="100" pad="yes" bg_color="#0000ff"

This would result in an image that is 100x100px, has a 25px blue strip along the top, a 50px red strip below that, and another 25px blue strip along the bottom.

Perhaps the new “pad” or “contain” parameter can even have some of the sub-parameters that “crop” has - namely, the yes_or_no, position, and offset params.

Thoughts?

EDIT: Thought I’d add a use-case. I’m building an online store, and the product images have all sorts of w/h ratios. I want to display them as squares, without cropping out anything (book titles were disappearing). I can’t do it in CSS because of the responsive design solution in use, which needs the image’s max-width to be 100%.

Causing Effect - Aaron Waldon
# 1
Developer
Causing Effect - Aaron Waldon

Hi Michael!

That is a good idea, and I do want to add it eventually. In the meantime, there is a workaround that works quite nicely. :)