<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.1" -->
<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/"
	>

<channel>
	<title>Ian Drysdale's Blog</title>
	<link>http://blog.iandrysdale.com</link>
	<description></description>
	<pubDate>Fri, 09 Nov 2007 12:59:10 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
	<language>en</language>
			<item>
		<title>Dynamically resizing text in Flash to fit a container</title>
		<link>http://blog.iandrysdale.com/2007/07/31/dynamically-resizing-text-in-flash-to-fit-a-container/</link>
		<comments>http://blog.iandrysdale.com/2007/07/31/dynamically-resizing-text-in-flash-to-fit-a-container/#comments</comments>
		<pubDate>Tue, 31 Jul 2007 15:23:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[actionscript]]></category>

		<category><![CDATA[code]]></category>

		<category><![CDATA[flash]]></category>

		<guid isPermaLink="false">http://823ian.owen.hostingrails.com/blog/?p=7</guid>
		<description><![CDATA[Earlier this year I ran into an interesting problem.  I was generating images of thought bubbles randomly appearing in a Flash movie containing people&#8217;s messages they&#8217;ve entered in a web form.  It was a nice interactive display.  The trouble was that people inevitably would write thoughts of varying lengths, and either the [...]]]></description>
			<content:encoded><![CDATA[<p style="margin-bottom: 0cm">Earlier this year I ran into an interesting problem.  I was generating images of thought bubbles randomly appearing in a Flash movie containing people&#8217;s messages they&#8217;ve entered in a web form.  It was a nice interactive display.  The trouble was that people inevitably would write thoughts of varying lengths, and either the short ones looked swamped by the bubble, or the long one&#8217;s over ran:</p>
<p style="margin-bottom: 0cm"><img src="http://iandrysdale.com/wp-content/uploads/2007/07/bubbles_overlap.gif" alt="Bubbles overlapping" /></p>
<p style="margin-bottom: 0cm">So, how could I determine the length and adjust the text size appropriately?  After spending sometime attempting to use a variety of formulas I thought of a different approach:  Firstly, start with the text size extremely large, say size 50.  Then, check if it breaks the boundary of the bubble, if it does, drop the size by 2 points.  Now does it break the boundary?  If it does, drop the size&#8230; and so on until the text fits.</p>
<p style="margin-bottom: 0cm">The result is neat thought bubbles with well sized text populating them:</p>
<p style="margin-bottom: 0cm"><img src="http://iandrysdale.com/wp-content/uploads/2007/07/bubbles_neat.gif" alt="Bubbles neatly sized" /></p>
<p style="margin-bottom: 0cm">I&#8217;ve stripped the Actionscript 2 code out of my application by means of example, though it won&#8217;t run out of the box:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="actionscript"><span style="color: #808080; font-style: italic;">//Generate a bubble</span>
<span style="color: #000000; font-weight: bold;">function</span> generateBubble<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Duplicate the bubble dummy</span>
	bubbleDummy.<span style="color: #0066CC;">duplicateMovieClip</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;bubble&quot;</span> + intDepth, <span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//And select it</span>
	objBubble = <span style="color: #0066CC;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;bubble&quot;</span> + intDepth<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Substitute in the response as dynamic text</span>
	objBubble.<span style="color: #006600;">txtResponse</span>.<span style="color: #0066CC;">text</span> 	= loadResponseObj<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;response&quot;</span>+intResponse<span style="color: #66cc66;">&#93;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Set up the TextFormat</span>
	myTextFormat = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	myTextFormat.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;my font&quot;</span>;
	myTextFormat.<span style="color: #0066CC;">size</span> = <span style="color: #cc66cc;">50</span>;
&nbsp;
	objBubble.<span style="color: #006600;">txtResponse</span>.<span style="color: #0066CC;">embedFonts</span> = <span style="color: #000000; font-weight: bold;">true</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Autosize the dynamic text field</span>
	objBubble.<span style="color: #006600;">txtResponse</span>.<span style="color: #0066CC;">setTextFormat</span><span style="color: #66cc66;">&#40;</span>myTextFormat<span style="color: #66cc66;">&#41;</span>;
	objBubble.<span style="color: #006600;">txtResponse</span>.<span style="color: #0066CC;">autoSize</span> = <span style="color: #ff0000;">&quot;left&quot;</span>;
&nbsp;
	<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span> objBubble.<span style="color: #006600;">txtResponse</span>.<span style="color: #0066CC;">_height</span> &amp;gt; <span style="color: #cc66cc;">140</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 	<span style="color: #808080; font-style: italic;">// the text field is taller than the bubble</span>
		myTextFormat.<span style="color: #0066CC;">size</span> -= <span style="color: #cc66cc;">2</span>; 			<span style="color: #808080; font-style: italic;">// shrink the font size by 2</span>
		objBubble.<span style="color: #006600;">txtResponse</span>.<span style="color: #0066CC;">setTextFormat</span><span style="color: #66cc66;">&#40;</span>myTextFormat<span style="color: #66cc66;">&#41;</span>;
		objBubble.<span style="color: #006600;">txtResponse</span>.<span style="color: #0066CC;">autoSize</span> = <span style="color: #ff0000;">&quot;left&quot;</span>;	<span style="color: #808080; font-style: italic;">// and resize the box...</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//Set the backing the same height as the text field</span>
	objBubble.<span style="color: #006600;">backing</span>.<span style="color: #0066CC;">_height</span> = objBubble.<span style="color: #006600;">txtResponse</span>.<span style="color: #0066CC;">_height</span> + <span style="color: #cc66cc;">40</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Keep our records tidy</span>
	intResponse ++;
	intDepth ++;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p style="margin-bottom: 0cm">Obviously this solution is an iterative loop, so I&#8217;d like to hear if there are more elegant solutions.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.iandrysdale.com/2007/07/31/dynamically-resizing-text-in-flash-to-fit-a-container/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Cropped thumbnails in attachment_fu using mini_magick</title>
		<link>http://blog.iandrysdale.com/2007/05/22/cropped-thumbnails-in-attachment_fu-using-mini_magick/</link>
		<comments>http://blog.iandrysdale.com/2007/05/22/cropped-thumbnails-in-attachment_fu-using-mini_magick/#comments</comments>
		<pubDate>Tue, 22 May 2007 17:57:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[code]]></category>

		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://823ian.owen.hostingrails.com/blog/?p=6</guid>
		<description><![CDATA[I&#8217;m in the process of building a ruby on rails application that allows people to upload images.  The application then takes these images and creates a series of thumbnails that are used throughout the site.
Having opted to use attachment_fu and mini_magick as my respective file upload plugin and image processor, I found an excellent [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m in the process of building a ruby on rails application that allows people to upload images.  The application then takes these images and creates a series of thumbnails that are used throughout the site.</p>
<p>Having opted to use <a href="http://svn.techno-weenie.net/projects/plugins/attachment_fu/">attachment_fu</a> and <a href="http://rubyforge.org/projects/mini-magick">mini_magick</a> as my respective file upload plugin and image processor, I found an excellent post by Khamsouk Souvanlasy explaining how to <a href="http://khamsouk.souvanlasy.com/2007/5/1/ajax-file-uploads-in-rails-using-attachment_fu-and-responds_to_parent" title="ajax file uploads in rails using attachment_fu and responds_to_parent">create the upload form using AJAX</a>.</p>
<p>The difficultly came when I wanted to make one of the custom thumbnails measure 100px x 100px, and crop the source image appropriately:</p>
<p><img src="http://iandrysdale.com/wp-content/uploads/2007/05/crop_example.jpg" alt="Example of ratio cropping" /></p>
<p>Thankfully Andy Croll had written <a href="http://www.deepcalm.com/writing/cropped-thumbnails-in-attachment_fu-using-imagescience">how he patched attachment_fu</a> to enable it to do just that, only using ImageScience as it&#8217;s image processor.</p>
<p>Therefore, I&#8217;ve extended this patch to allow you to use a ! flag to create an aspect ratio cropped image, using mini_magick as your processor.</p>
<h3>Step 1</h3>
<p>Modify <strong>attachment_fu/lib/geometry.rb</strong>, as suggested by Andy in <a href="http://www.deepcalm.com/writing/cropped-thumbnails-in-attachment_fu-using-imagescience">cropped thumbnails in attachment_fu using imagescience</a>.  This ensures the ! flag is interpretted correctly in attachment_fu.</p>
<h3>Step 2</h3>
<p>Modify the <em>resize_image</em> function of <strong>attachment_fu/processors/mini_magick_processor.rb</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> resize_image<span style="color:#006600; font-weight:bold;">&#40;</span>img, size<span style="color:#006600; font-weight:bold;">&#41;</span>
  size = size.<span style="color:#9900CC;">first</span> <span style="color:#9966CC; font-weight:bold;">if</span> size.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">Array</span><span style="color:#006600; font-weight:bold;">&#41;</span> &amp;&amp; size.<span style="color:#9900CC;">length</span> == <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> size.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Fixnum</span><span style="color:#006600; font-weight:bold;">&#41;</span> || <span style="color:#006600; font-weight:bold;">&#40;</span>size.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">Array</span><span style="color:#006600; font-weight:bold;">&#41;</span> &amp;&amp; size.<span style="color:#9900CC;">first</span>.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Fixnum</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> size.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Fixnum</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      size = <span style="color:#006600; font-weight:bold;">&#91;</span>size, size<span style="color:#006600; font-weight:bold;">&#93;</span>
      img.<span style="color:#9900CC;">resize</span><span style="color:#006600; font-weight:bold;">&#40;</span>size.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'x'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      img.<span style="color:#9900CC;">resize</span><span style="color:#006600; font-weight:bold;">&#40;</span>size.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'x'</span><span style="color:#006600; font-weight:bold;">&#41;</span> + <span style="color:#996600;">'!'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    n_size = <span style="color:#006600; font-weight:bold;">&#91;</span>img<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:width</span><span style="color:#006600; font-weight:bold;">&#93;</span>, img<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:height</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span> / size.<span style="color:#9900CC;">to_s</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> size.<span style="color:#9900CC;">ends_with</span>? <span style="color:#996600;">&quot;!&quot;</span>
      aspect = n_size<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_f</span> / n_size<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_f</span>
      ih, iw = img<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:height</span><span style="color:#006600; font-weight:bold;">&#93;</span>, img<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:width</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      w, h = <span style="color:#006600; font-weight:bold;">&#40;</span>ih * aspect<span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#006600; font-weight:bold;">&#40;</span>iw / aspect<span style="color:#006600; font-weight:bold;">&#41;</span>
      w = <span style="color:#006600; font-weight:bold;">&#91;</span>iw, w<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">min</span>.<span style="color:#9900CC;">to_i</span>
      h = <span style="color:#006600; font-weight:bold;">&#91;</span>ih, h<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">min</span>.<span style="color:#9900CC;">to_i</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> ih &gt; h
        shave_off =  <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>ih - h<span style="color:#006600; font-weight:bold;">&#41;</span> / <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">round</span>
        img.<span style="color:#9900CC;">shave</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;0x#{shave_off}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> iw &gt; w
        shave_off = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>iw - w <span style="color:#006600; font-weight:bold;">&#41;</span> / <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">round</span>
        img.<span style="color:#9900CC;">shave</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{shave_off}x0&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      img.<span style="color:#9900CC;">resize</span><span style="color:#006600; font-weight:bold;">&#40;</span>size.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      img.<span style="color:#9900CC;">resize</span><span style="color:#006600; font-weight:bold;">&#40;</span>size.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">temp_path</span> = img
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<h3>Step 3</h3>
<p>Modify your <strong>model</strong>, using the new <strong>!</strong> flag to force a cropped aspect ratio:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">has_attachment  <span style="color:#ff3333; font-weight:bold;">:storage</span> =&gt; <span style="color:#ff3333; font-weight:bold;">:file_system</span>,
                <span style="color:#ff3333; font-weight:bold;">:max_size</span> =&gt; <span style="color:#006666;">2</span>.<span style="color:#9900CC;">megabytes</span>,
                <span style="color:#ff3333; font-weight:bold;">:thumbnails</span> =&gt; <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:thumb</span> =&gt; <span style="color:#996600;">'100x100!'</span>, <span style="color:#ff3333; font-weight:bold;">:medium</span> =&gt; <span style="color:#996600;">'470x470'</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
                <span style="color:#ff3333; font-weight:bold;">:processor</span> =&gt; <span style="color:#ff3333; font-weight:bold;">:MiniMagick</span></pre></div></div>

<p>I think this is an excellent solution.  Thanks to everyone I&#8217;ve link to here, they&#8217;ve done the real work!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.iandrysdale.com/2007/05/22/cropped-thumbnails-in-attachment_fu-using-mini_magick/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
