PNG Thumbnails for PDF files. Take two

October 7th, 2008

Updating my previous post, I finished up the work of extending attachment_fu to optionally create PNG thumbnails of updated PDF files. Check out the fork on github

Creating thumbnails of PDFs with attachment_fu

September 16th, 2008

We needed to create some thumbnails from uploading PDF files for a new site feature – We’re using attachment_fu which doesn’t support that (yet?), but we’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 the attachment_fu plugin itself. One day I’ll loop back and figure out a cleaner way to do this and see which of attachment_fu’s other image processors can even support pdfs.

There are three methods to override to make a go of this:

  1. self.image? : consider pdf files as an image so thumbnail process will happen
  2. thumbnail_name_for : change the extension of the saved thumbnail filename to png
  3. resize_image: override to change format via block passed to to_blob

Apologies for the crappy source formatting, I have to install a plugin to do that well one of these days


###Hacks to allow creation of png thumbnails for pdf uploads - depends on RMagic being the configured processor
## likely very fragile

def self.image?(content_type)
(content_types +  ['application/pdf']).include?(content_type)
end

alias_method :o riginal_thumbnail_name_for, :thumbnail_name_for
def thumbnail_name_for(thumbnail=nil)
return original_thumbnail_name_for(thumbnail) unless (content_type == 'application/pdf' && !thumbnail.blank?)
basename = filename.gsub /\.\w+$/ do |s|
ext = s; ''
end
"#{basename}_#{thumbnail}.png"
end
#copied from rmagick_processor with change in last few lines
def resize_image(img, size)
size = size.first if size.is_a?(Array) && size.length == 1 && !size.first.is_a?(Fixnum)
if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
size = [size, size] if size.is_a?(Fixnum)
img.thumbnail!(*size)
else
img.change_geometry(size.to_s) { |cols, rows, image| image.resize!(cols<1 ? 1 : cols, rows<1 ? 1 : rows) }
end
img.strip! unless attachment_options[:keep_profile]
if content_type == 'application/pdf' # here force the output format to PNG if its a pdf
self.temp_path = write_to_temp_file(img.to_blob {self.format = 'PNG'})
else
self.temp_path = write_to_temp_file(img.to_blob)
end
end

Saving money on train tickets in the UK

September 1st, 2008

Skip raileurope.com if you’re in the US and buying rain tickets for travel within the UK. Their prices are outrageously marked up.  For a Saturday, second class ticket from Newcastle to Edinburgh in October, their quote is $96.00. Get a quote from one of the many web sites based in the UK, and its 9 and a half Pounds. That’s about $17.

The problem is many sites in the UK don’t let you purchase tickets with a non-UK billing address. If you’re going to be at a major station where you can pick up tickets from a machine, check out thetrainline.com. They’re cool with US billing addresses, and much cheaper as well.

Livepipe UI controls for JS

August 21st, 2008

I’ve been pretty pleased so far with the popup window control from LivePipe. It plays nice with Prototype and is easy to style with regular CSS. We had considered using Prototype Window but I was put off that all their default styles looks like operating system windows and restyling their windows required a table and 9 images.

I’d recommend anyone looking for a popup window solution at least consider Livepipe. There are downsides however, chiefly that the project is pretty immature – technically I suppose this is an alpha release since Beta One is being worked on, so the community remains small. While there are some folks already submitting patches, progress on merging the patches is alarmingly slow, as one can see from their lighthouse page.

If you’re doing RESTful stuff in Rails however, you will need the contents of ticket #10 which modifies the popup window to accept an option to use different HTTP verbs.

Scriptaculous docs

August 21st, 2008

For all the complaining I often do about the poor documentation of the scriptaculous project, I finally did something to help that today, creating (very thin) documentation for their new (if released in January is new) Effect.Tween function here on their github wiki.

I was creating a method to scroll the viewport so that the contents of an AJAX-loaded div would be fully visible on the screen – the (still undocumented) Effect.ScrollTo doesn’t quite do it because it doesn’t consider the height of the element it scrolls to, but in doing so I stumbled over Tween in the code. Once the math to figure out how much scrolling is needed, its easy to use Effect.Tween to smoothly scroll the window by repeatedly calling window.scrollBy();

This certainly isn’t rocket science, but here’s an outline of how to do it (this code only deals with downward vertical scrolling):

var elementHeight = element.getHeight();
var screenHeight = document.viewport.getHeight();
var elementScreenPos =element.viewportOffset()[1];
var amountToScroll = elementHeight - (screenHeight - elementScreenPos);
if (amountToScroll > 0){
var scrollPos = document.viewport.getScrollOffsets().top;
new Effect.Tween(null,scrollPos,scrollPos+amountToScroll,{},function(n) { window.scrollTo(0,n);});
}
}

iPhone can’t keep time

August 18th, 2008

Every now and then my iPhone has this issue where it can’t tell time properly. I wake it up, and it shows me a time several hours ago, then as if waking from a drunken stupor, slowly tries to catch up to reality, moving the clock forward by a small, random number of minutes. During these episodes the whole UI is sluggish, and it apparently doesn’t even accept phone calls. When “phone” is 5/6 of your name one would think at least that would work all of the time!

Check out this screenshot from the missed call sheet. It recorded 3 missed calls that arrived over the course of an afternoon all with the exact same arrival time, 9:40 AM. The phone never rang.

That was with v2.01, so I sure hope this is fixed in the future.

Update: to Frank’s comment – this wasn’t a matter of the phone bouncing between time zones. The phone’s time isn’t a whole number of hours behind.

NYTimes application for the iPhone

August 18th, 2008

I think the New York Times application for the iPhone is pretty good. My chief complaint (other than the random crashing) is that the head shots of the authors are too damn big.

A concoction most foul

August 18th, 2008

The building I work in threw a building BBQ last week. There were hot dogs that were actually pretty tasty, there were tough hamburgers that made me think of beef recalls, and there was Tropicana “Lemondade” Juice Drink that made me rethink all the connotations that Tropicana had for me.

It used to be that the Tropicana brand meant some combination of pure, natural, and juice. Indeed if you go to their web site, there are real people who make their juice talking to you about how great it is, and I think everything there is 100% juice. Unfortunately there’s a seamy underside to the brand. I present to you, Tropicana Brand Lemonade Flavored Juice Beverage.

Only after drinking a third of the bottle did I stop to ponder the label. 260 Calories per bottle (cleverly divided up into 8oz servings that no one ever actually drinks), lots of high fructose corn syrup, and my favorite: glycerol ester of wood rosin. If that doesn’t quench your thirst, I don’t know what will. Oh there’s some lemon juice in there too, just one of thirteen ingredients.

The tropicana web site fails to acknowledge this line of delicous and nutritous beverages, which is a real shame. There’s only an entry here at a PepsiCo FAQ.

I just think its a shame when companies whore out their relatively good name like this.

99 Percent Honesty

August 8th, 2008

It finally comes out that John Edwards cheated on his wife who had cancer at the time. His official statement about it since don’t do much for me but dig the whole deeper. From his official statement:

Although I was honest in every painful detail with my family, I did not tell the public. When a supermarket tabloid told a version of the story, I used the fact that the story contained many falsities to deny it. But being 99% honest is no longer enough.

I don’t think denying an affair because some of the details are inaccurate is being 99% honest. More like 1% honest.

The other gem of a comment from the Times’ coverage is this:

The network said that Mr. Edwards “made a point of telling Woodruff that his wife’s cancer was in remission when he began the affair with Hunter.”

I guess cheating on your wife is cool if her cancer is in remission.

RCN “Analog Crush” slow in coming to Somerville

August 8th, 2008

Just called RCN to ask when their vaunted “analog crush” all-digital upgrade is going to arrive (so we get more HD channels). Turns out Somerville is last on the list in Massachusetts, with an ETA of Jan 2009.

The backstory is that Somerville is apparently a really antiquated system so we already get less HD channels than RCN subscribers in Boston, who pay the same amount as we do across the river.

And we’re not getting the special NBC Olympics Basketball and Soccer channels.