<?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>Brain Lint &#187; rails</title>
	<atom:link href="http://www.monkeyatlarge.com/archives/tag/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.monkeyatlarge.com</link>
	<description>Random musings on life, technology and other miscellany.</description>
	<lastBuildDate>Fri, 03 Feb 2012 21:34:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Creating thumbnails of PDFs with attachment_fu</title>
		<link>http://www.monkeyatlarge.com/archives/2008/09/16/creating-thumbnails-of-pdfs-with-attachment_fu/</link>
		<comments>http://www.monkeyatlarge.com/archives/2008/09/16/creating-thumbnails-of-pdfs-with-attachment_fu/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 20:56:33 +0000</pubDate>
		<dc:creator>James Kebinger</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[attachment_fu]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[thumbnails]]></category>

		<guid isPermaLink="false">http://www.monkeyatlarge.com/?p=301</guid>
		<description><![CDATA[We needed to create some thumbnails from uploading PDF files for a new site feature &#8211; We&#8217;re using attachment_fu which doesn&#8217;t support that (yet?), but we&#8217;re using RMagick as our processor and it understands PDF files. I came up with the hack below (warning, first draft, only briefly tested) which works without having to modify [...]]]></description>
			<content:encoded><![CDATA[<p>
We needed to create some thumbnails from uploading PDF files for a new site feature &#8211; We&#8217;re using <a href="http://github.com/technoweenie/attachment_fu/tree/master">attachment_fu </a>which doesn&#8217;t support that (yet?), but we&#8217;re using <a href="http://rmagick.rubyforge.org/">RMagick</a> as our processor and it understands PDF files.
</p>
<p> I came up with the hack below (warning, first draft, only briefly tested) which works without having to modify the attachment_fu plugin itself. One day I&#8217;ll loop back and figure out a cleaner way to do this and see which of attachment_fu&#8217;s other image processors can even support pdfs.
</p>
<p>
There are three methods to override to make a go of this:</p>
<ol>
<li>self.image? :  consider pdf files as an image so thumbnail process will happen</li>
<li>thumbnail_name_for : change the extension of the saved thumbnail filename to png</li>
<li>resize_image: override to change format via block passed to to_blob</li>
</ol>
<p>Apologies for the crappy source formatting, I have to install a plugin to do that well one of these days</p>
<p><code><br />
###Hacks to allow creation of png thumbnails for pdf uploads - depends on RMagic being the configured processor<br />
## likely very fragile</p>
<p>def self.image?(content_type)<br />
(content_types +  ['application/pdf']).include?(content_type)<br />
end</p>
<p>alias_method <img src='http://www.monkeyatlarge.com/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> riginal_thumbnail_name_for, :thumbnail_name_for<br />
def thumbnail_name_for(thumbnail=nil)<br />
return original_thumbnail_name_for(thumbnail) unless (content_type == 'application/pdf' &amp;&amp; !thumbnail.blank?)<br />
basename = filename.gsub /\.\w+$/ do |s|<br />
ext = s; ''<br />
end<br />
"#{basename}_#{thumbnail}.png"<br />
end<br />
#copied from rmagick_processor with change in last few lines<br />
def resize_image(img, size)<br />
size = size.first if size.is_a?(Array) &amp;&amp; size.length == 1 &amp;&amp; !size.first.is_a?(Fixnum)<br />
if size.is_a?(Fixnum) || (size.is_a?(Array) &amp;&amp; size.first.is_a?(Fixnum))<br />
size = [size, size] if size.is_a?(Fixnum)<br />
img.thumbnail!(*size)<br />
else<br />
img.change_geometry(size.to_s) { |cols, rows, image| image.resize!(cols&lt;1 ? 1 : cols, rows&lt;1 ? 1 : rows) }<br />
end<br />
img.strip! unless attachment_options[:keep_profile]<br />
if content_type == 'application/pdf' # here force the output format to PNG if its a pdf<br />
self.temp_path = write_to_temp_file(img.to_blob {self.format = 'PNG'})<br />
else<br />
self.temp_path = write_to_temp_file(img.to_blob)<br />
end<br />
end</p>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.monkeyatlarge.com/archives/2008/09/16/creating-thumbnails-of-pdfs-with-attachment_fu/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

