Dead Simple Image Processing with ImageMagick
January 22nd, 2007
I have previously mentioned using the ruby plugin MiniMagick to resize images. However, I recently realized there is a much easier way that only requires ImageMagick and would therefore work for any web development environment including PHP, Ruby, ASP etc. This technique was inspired by the MiniMagick plugin and involves shelling out directly to the ImageMagick command-line utilities.
I will cover two common techniques:
- resizing an image
- determining an image’s width and height
Resizing An Image
To resize an image, you can use ImageMagick’s mogrify utility. For example, to resize an image to 100×100 you would type:
mogrify -thumbnail "100x100>" someimage.gif
The > symbol will make mogrify maintain the aspect ratio. To run this command from RubyOnRails, you can just use backticks to execute it from a command shell like so:
`mogrify -thumbnail "100x100>" someimage.gif`
Determining Image Width and Height
To determine the width and height of the image, we can use ImageMagick’s identify utility like so:
identify -format "%w %h" someimage.gif
This will return the width and height separated by a space. So, to parse these from Ruby you could do:
w, h = `identify -format "%w %h" someimage.gif`.split
It’s that easy. One other side benefit is by running this process from the command-line your webapp will not consume the additional memory needed for the resizing. The command shell will allocate and free this memory. For a RubyOnRails application, this is a huge benefit as one of the biggest issues with other techniques such as RMagick is that they typically may consume too much memory and will crash your web application.
Converted From Typo To WordPress
January 18th, 2007
So, I finally got around to converting this site off of Typo. All permalinks should still work. I did not migrate the comments over however.
I chose Typo because I had bought into the hype like everyone else and thought since I am actively working with Rails I should be using a blog written in Rails. Well, since then the creator of Typo has moved on from the project and even his blog is now using Mephisto. But the main reason I migrated from Typo is it just doesn’t have the usability and features of a more mature blogging engine such as WordPress. It was almost painful to write a post in.
At first I spent a few days converting this blog to Mephisto to try to keep it in the Rails family only to find out that the memory requirements for Mephisto are roughly 47MB per fcgi! I think that may have more to do with Rails 1.2 than with Mephisto though but I haven’t spent any time digging in. In any case, beware if you are on a shared host as Mephisto currently is not supported in that environment.
The conversion process was a snap thanks to WordPress’s RSS importer. The rough steps I took were:
- Modify Typo’s xml_controller to increase the maximum number of articles in the articles feed. It just has to be greater than the number of posts you have.
- Log into your WordPress admin section and click Import -> RSS.
- To keep your links the same, you will need to change your WordPress Options -> Permalinks to Date and name based. Then to allow the “articles” portion of Typo urls, you can add this to the generated .htaccess file in the root of your WordPress install:
RewriteBase /articles RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule articles/(.*) /index.php/$1 [L]
Active Resource Removed From Rails 1.2
November 19th, 2006
Active Resource was just removed from the Rails 1.2 release branch. So, it looks like it will not be shipping with the release.
After a quick search I couldn’t find any more information about it, but I would expect an announcement will follow shortly. So you may need to stick with edge for a while longer if you are counting on Active Resource support.
I personally think it is better for them to get this one right before mass distribution since this feature has such a wide impact on Rails development. Better to pull it now and fine tune it versus releasing something that will change drastically between releases.
TextMate Snippets for RadRails
November 7th, 2006
Dr Nic just ported over all of TextMate’s Rails snippets as RadRails templates. These should definitely add a boost to your productivity as you can type most commands for Rails development in just a few keystrokes. I have been meaning to spend more time getting Eclipses templates incremented into my dev process and this is just the boost I needed.
Get your templates and read Dr. Nic’s announcement on his blog.
New Gift Hat Release: Ratings, Sorting and more
November 3rd, 2006
I just released another version of the Gift Hat. You can read all about it on the Gift Hat blog. Stop by and check it out.
This should free up some time for me to start blogging a bit more. I have a bit of a backlog of topics I want to discuss. You should start seeing some more posts starting next week.
Dynamic Gmail Addresses
October 10th, 2006
I just ran across an interesting feature of gmail. You can append a + sign and random text to the end of your existing gmail email address and gmail will send it to your existing gmail account. For example, if your gmail acount was fakeemail@gmail.com, then you could send email to fakeemail+nospam@gmail.com and you will still receive it. You can then setup a gmail filter to move these emails into the trash, or tag them with a label, etc.
I have been using this a lot lately when signing up with various accounts. I also setup a separate notification email address that I use to send automated notifications with a monitoring script when any of my sites are down (more details on this monitoring script to follow in a future post).
Rdiculous tracks rdiculously named websites
September 19th, 2006
After seeing one too many sites named with a trailing r, similar to Flickr, my friend Brian and I decided to start cataloguing them on a website. The site is called rdiculous.com to poke a little fun at this bizarre web2.0 website naming trend. It is incredible how many there are and we have just started to scratch the surface.
Stop on by and check it out at rdiculous.com. We are posting more of them nearly every day, so if you know of any that we missed please drop us a line.
Got API?
August 29th, 2006
Just ran across this very useful AJAXified api reference while trolling through the RoR mailing list:
It has support for a ton of different languages including:
Ruby, RubyOnRails, Javascript, HTML, CSS, XML, Actionscript, Java, PHP, Perl, C, C++, and more…
I am pretty impressed with the interface from my brief experimentation. Very snappy. Check it out.
Runit cheat sheet
August 28th, 2006
Here is a nice RUnit cheat sheet courtesy of nubyonrails.
I am all about the cheat sheets, such as this handy prototype cheat sheet
Fetching Data The Easy Way with Ruby
August 23rd, 2006
I recently ran into some difficulties while fetching random html pages and images using ruby so I thought others may benefit from my experience.
Ruby has two (and probably many more) great libraries for fetching http data:
OpenURI has a much nicer API and takes care of things like http redirects for you. However, if you try to fetch data over HTTPS, you will see a message like Certificate Verify Failed.
To get around this limitation I’ve written this simple method for fetching data that will enable ssl when necessary and follow all redirects:
def self.get_data(url, limit = 10)
raise "Too Many Redirects" if limit == 0
uri = URI.parse url
server = Net::HTTP.new uri.host, uri.port
server.use_ssl = uri.scheme == 'https'
server.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = server.get uri.request_uri
case response
when Net::HTTPSuccess then response['host'] = uri.host; response['path'] = uri.path; response
when Net::HTTPRedirection then self.get_data(response['location'], limit - 1)
else
response.error!
end
end
Also, you will notice that I add the host and path into the response since in my case I use that information for building the full path to the images.
Enjoy!
Subscribe to the feed!