Griffith Designs

Determine IMG Scale Ratio

by webmasterjunkie on Aug.27, 2011, under Coldfusion, Internet, Programming, Web

Well, recently I had to vertically center an image in a square DIV. This had to be done with the image being scaled to a specific width, 90px. I know there are probably hundreds of ways to do this, but this is the one that I used to determine how much top margin to set on the image in order to center it vertically.

<cfobject type="JAVA" action="Create" name="tk" class="java.awt.Toolkit"></cfobject>
<cfobject type="JAVA" action="Create" name="img" class="java.awt.Image"></cfobject>
<!--- requires knowing the desired width of the image/div --->
<cfset imgDims = 90 />
<cfscript>
 imagePath = ExpandPath ("/uploads/Image/sponsors/#sm_image#");
 img = tk.getDefaultToolkit().getImage("#imagePath#");
 imgWidth = img.getWidth();
 imgHeight = img.getHeight();
 percentage = (imgDims / imgWidth) * 100;
 newHeight = round((imgHeight * percentage) / 100);
 margin = (imgDims - newHeight) / 2;
</cfscript>

Now this makes the task easy using the built in capabilities of JAVA in ColdFusion! Once that was done, I simply outputted the margin variable in the inline css of the page. I tried a few “dynamic” CSS options, but none of them seemed to do the trick for me.

:,

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Connect