ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

'Infallible Scale Expression' - Problems with auto scaling images to comp size

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
malcolm ricci
'Infallible Scale Expression' - Problems with auto scaling images to comp size
on Nov 21, 2019 at 4:28:38 pm

Hey guys,

I'm pretty new to expressions but I'm currently trying to set up a template which could potentially have images auto scaled according to the comp size, while still retaining their aspect ratio.

So I've come across this link: http://aefreemart.com/the-infallible-scale-expression/

The expression he uses is the following:

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 ]
}

In general it seems to work, however I've had instances where the image still does not fill the composition completely.

Apart from this, I'm having issues when it comes to importing images that are square rather than rectangular. I've tried to include an 'else if' prompt to the expression but haven't managed to figure it out yet.

Does anyone know of a similar expression to the above that is a little more fool-proof?

Also, could anyone help me with including an 'else if' clause for square images?

Thanks and much appreciated.

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 ]
}


Return to posts index

Dan Ebberts
Re: 'Infallible Scale Expression' - Problems with auto scaling images to comp size
on Nov 22, 2019 at 1:33:36 am

Try this:


w = thisLayer.width;
h = thisLayer.height;
try{
p = thisLayer.source.pixelAspect;
}catch(e){
p = 1;
}
compW = thisComp.width;
compH = thisComp.height;
compP = thisComp.pixelAspect;
if ((w*p)/h > (compW*compP)/compH){
s = ((compW*compP)/(w*p))*100;
}else{
s = (compH/h)*100;
}
[s,s]



Dan



Return to posts index

malcolm ricci
Re: 'Infallible Scale Expression' - Problems with auto scaling images to comp size
on Nov 22, 2019 at 12:41:24 pm

Thanks for this Dan.

So it does seem to work and fit the image better than it was before, however it is still not filling out the whol comp.

https://i1.creativecow.net/u/281575/screenshot2019-11-2213.38.57.png

Here's a screenshot of the comp. I hope that the link works.

Would there be a way for the image to fill out all of the comp while still being centre aligned?

Thanks.


Return to posts index


Dan Ebberts
Re: 'Infallible Scale Expression' - Problems with auto scaling images to comp size
on Nov 22, 2019 at 5:30:49 pm

So you want the image to be cropped (either top and bottom, or left and right) to fill the comp? Or did you want it stretched? Is pixel aspect ratio an issue, or is it always square pixels?

In any case, a scale expression won't take care of alignment issues. Are the images not centered (with anchor point centered) to begin with?

Dan



Return to posts index

malcolm ricci
Re: 'Infallible Scale Expression' - Problems with auto scaling images to comp size
on Nov 27, 2019 at 3:31:30 pm

Apologies for my delayed response.

So ideally I'd like the comp to be completely filled out with the image. Now if the image were a square then it would fill out the comp entirely while still maintaining it's aspect ration, which would lead to it having some of it's height spilling over the top and bottom.

I will try to keep on experimenting with the expression that you sent me as well as making sure the image is centrally aligned and take it for there.

Thanks for the input :)


Return to posts index

Dan Ebberts
Re: 'Infallible Scale Expression' - Problems with auto scaling images to comp size
on Nov 27, 2019 at 4:49:59 pm

>So ideally I'd like the comp to be completely filled out with the image.

Then I think you just need to swap two lines like this:


w = thisLayer.width;
h = thisLayer.height;
try{
p = thisLayer.source.pixelAspect;
}catch(e){
p = 1;
}
compW = thisComp.width;
compH = thisComp.height;
compP = thisComp.pixelAspect;
if ((w*p)/h > (compW*compP)/compH){
s = (compH/h)*100;
}else{
s = ((compW*compP)/(w*p))*100;
}
[s,s]



Dan



Return to posts index


malcolm ricci
Re: 'Infallible Scale Expression' - Problems with auto scaling images to comp size
on Dec 6, 2019 at 9:26:46 am

Thanks for this.

Apologies for the delay, but it's working exactly as I need it to now ☺

Malcolm.


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]