<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Eric Juden &#187; image</title>
	<atom:link href="http://thejudens.com/eric/tag/image/feed/" rel="self" type="application/rss+xml" />
	<link>http://thejudens.com/eric</link>
	<description>Web Application Developer, Husband and Father</description>
	<lastBuildDate>Wed, 01 Sep 2010 20:11:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>jQuery Image Resize</title>
		<link>http://thejudens.com/eric/2009/07/jquery-image-resize/</link>
		<comments>http://thejudens.com/eric/2009/07/jquery-image-resize/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 13:44:06 +0000</pubDate>
		<dc:creator>Eric Juden</dc:creator>
				<category><![CDATA[In the Trenches]]></category>
		<category><![CDATA[aspectratio]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[resize]]></category>

		<guid isPermaLink="false">http://thejudens.com/eric/?p=160</guid>
		<description><![CDATA[Let me start off by saying, I LOVE jQuery! It&#8217;s the greatest thing since sliced bread! Now that I have that out of the way, I&#8217;ve been working on developing some WordPress themes for work. We are going to be launching a blogs site (using WordPress MU) fairly soon for faculty, staff, and eventually students. [...]]]></description>
			<content:encoded><![CDATA[<p>Let me start off by saying, I <em>LOVE</em> <a href="http://www.jquery.com" target="_blank">jQuery</a>! It&#8217;s the greatest thing since sliced bread! Now that I have that out of the way, I&#8217;ve been working on developing some WordPress themes for work. We are going to be launching a blogs site (using <a href="http://mu.wordpress.org" target="_blank">WordPress MU</a>) fairly soon for faculty, staff, and eventually students. So, in the process we are building a blog that will be used as an online newsletter. On the blog page, they want the top story to take precedence, and for the image to be larger. So to accomplish this, I wanted to scale down the other images and keep the aspect ratio. Enter jQuery&#8230;</p>
<p>I was looking out there at some of the solutions already completed. I didn&#8217;t see anything that quite did what I was looking for. So I made my own function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.story-small img'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> maxWidth <span style="color: #339933;">=</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Max width for the image</span>
        <span style="color: #003366; font-weight: bold;">var</span> maxHeight <span style="color: #339933;">=</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">// Max height for the image</span>
        <span style="color: #003366; font-weight: bold;">var</span> ratio <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// Used for aspect ratio</span>
        <span style="color: #003366; font-weight: bold;">var</span> width <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">// Current image width</span>
        <span style="color: #003366; font-weight: bold;">var</span> height <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// Current image height</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Check if the current width is larger than the max</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>width &gt; maxWidth<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            ratio <span style="color: #339933;">=</span> maxWidth <span style="color: #339933;">/</span> width<span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">// get ratio for scaling image</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;width&quot;</span><span style="color: #339933;">,</span> maxWidth<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Set new width</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;height&quot;</span><span style="color: #339933;">,</span> height <span style="color: #339933;">*</span> ratio<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// Scale height based on ratio</span>
            height <span style="color: #339933;">=</span> height <span style="color: #339933;">*</span> ratio<span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">// Reset height to match scaled image</span>
            width <span style="color: #339933;">=</span> width <span style="color: #339933;">*</span> ratio<span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">// Reset width to match scaled image</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Check if current height is larger than max</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>height &gt; maxHeight<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            ratio <span style="color: #339933;">=</span> maxHeight <span style="color: #339933;">/</span> height<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// get ratio for scaling image</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;height&quot;</span><span style="color: #339933;">,</span> maxHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">// Set new height</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;width&quot;</span><span style="color: #339933;">,</span> width <span style="color: #339933;">*</span> ratio<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">// Scale width based on ratio</span>
            width <span style="color: #339933;">=</span> width <span style="color: #339933;">*</span> ratio<span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">// Reset width to match scaled image</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The 2nd line of code says to grab every image with a class of &#8216;story-small&#8217;. The block for the height is just a safety net. I have it there in case the height is still larger than the max. There are a hundred ways to skin a cat, so I&#8217;m sure there are plenty of other ways to do this.</p>
]]></content:encoded>
			<wfw:commentRss>http://thejudens.com/eric/2009/07/jquery-image-resize/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
	</channel>
</rss>
