The Infallible Scale Expression

So, you are doing one of those projects with lots and lots of pictures. Some are 400×300 and some seem to be monstrous. Wouldn’t be nice if we could simply say “scale this stupid picture to the size of my comp, regardless of the size of my picture, comp resolution, picture composition or pixel aspect ratio.” Well, we can do that, but we have to say it differently. We have to say it like this:

w = thisLayer.width;
h = thisLayer.height;

compW = thisComp.width;

compH = thisComp.height;

p = thisComp.pixelAspect;

if ( w > h ){ //test if image is taller than wide

s = (compW / w ) * 100;

[ s * p, s ]

}else{

s = (compH / h ) * 100;

[ s , s ]

}

Let’s break this apart. First we have a simple if/else conditional statement. If w > h.. or if the width is less than the height.. do one computation, else do another. What this does is qualify our image as being vertically or horizontally composed.

Let’s say it is horizontal. From here we need to figure out the scale to get the width of the picture to match the width of the comp. Think way back to Algebra 1… you probably did one of these things:
This is exactly what we are doing here. Let’s say our compostion is 1,024 pixels wide and our picture is 800 pixels wide. If we say that the relationship between these two widths could be simplified to be expressed as a relationship of some number to 100, we’d have our scale factor, X (because AE scale is quantified as a 0 – 100 value) . Granted, in my expression.. I use “s” as the variable. I just used “X” for the High School nostalgia factor.

So, if we creatively flip this around using yet more Algebra, we get:

All we are really doing here is multiplying both sides by 100. In the expression, we see:

s = (compW / w ) * 100;

That’s all that is. And it’s the same for both width and height. Beyond that, for the width we need to multiply the pixel aspect as our Algebra is not aware of pixel aspect ratio. P is using the expression for the current comp pixel aspect.

As a simpler alternative, you could simply use the following section, to fill the screen with the image and not worry about composition.

h = thisLayer.height;
compH = thisComp.height;
s = (compH / h ) * 100;

[ s , s ]

As a part II of this coming soon, we’ll examine how to use this expression, but include a “camera zoom and pan” animation. This is part of a massive Preset I am working on that greatly simplifies doing “slideshow animations”. It will truly rock once I get the code tweaked. Check back!