Skip to content

Archive for the 'Programming' Category

Gordon: A Flash runtime written in JavaScript

Ok, I’m super stoked about the possibilities of this one. Gordon is an open source Flashâ„¢ runtime written in pure JavaScript. Give some of these demos a try on your iPhone. They don’t run as fast as they do on the computer but OMG Flash on the iPhone!

(thanks Mark)

Programmatically Pull Attachments from WordPress Posts

One of my favorite tricks in using WordPress as a CMS is to have it handle photo resizing and insertion into pages automatically. For example, swissmiss and I just launched Convert (a NYC based Green Roof Service) today. Within the projects section they display photos of the green roofing projects they’ve done and let you download a project sheet PDF.

It would be quite a burden for the client to have to know how to create all of the appropriate image sizes, upload the photos and insert the correct HTML tags. So here’s what we’ve done instead:

First, we set the WordPress image sizes to match the thumbnail and large sizes that we needed by going to Settings->Media in the admin tool.

Next, the client just has to upload all of their photos and a PDF for the project sheet. The images are automatically scaled by WordPress and attached to the page. They can also change the order the photos appear by dragging/dropping them in the admin tool.

Finally, adding this simple snippet of code to the functions.php in their theme, I can ask for all of the images associated with the post.

// get all of the images attached to the current post
function aldenta_get_images($size = 'thumbnail') {
	global $post;

	$photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

	$results = array();

	if ($photos) {
		foreach ($photos as $photo) {
			// get the correct image html for the selected size
			$results[] = wp_get_attachment_image($photo->ID, $size);
		}
	}

	return $results;
}

So, if you want all of the thumbnail images (as an array of html tags) you can make the default call:

$photos = aldenta_get_images();

If you want all of the medium sized images you pass medium as the size:

$photos = aldenta_get_images('medium');

My function has a bit more going on inside of it but you get the idea of what you can do with this. Another way I like to use it is to pull the first image from the post. Sometimes, you want to display a thumbnail image that represents a post. Here’s how I like to do that:

// get the first image attached to the current post
function aldenta_get_post_image($size = 'thumbnail') {
	global $post;

	$photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

	if ($photos) {
		$photo = array_shift($photos);
		return wp_get_attachment_image($photo->ID, $size);
	}

	return false;
}

// the html tag for the first image or false if no image is found
$photo = aldenta_get_post_image();

What if you want to get the PDF attached to the post? No problem – it works the same but you change the mime type to application/pdf.

// get the first PDF attached to the current post
function aldenta_get_post_pdf() {
	global $post;

	$attachments = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'application/pdf', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

	if ($attachments) {
		$attachment = array_shift($attachments);
		return wp_get_attachment_url($attachment->ID);
	}

	return false;
}

// the url to the first pdf or false if no pdf is found
$pdf = aldenta_get_post_pdf();

This technique has helped me tremendously and I hope it does the same for you.

Plugin Update: PDF Bookmark bug fix and French translation

If you are having problems with the bookmark plugin not working for some documents, this update (1.2) should fix that. The update also adds a French translation to the Bookmark menu and provides an installation path for Linux users. Thanks to TitCouille for help with all of those items.

The plugin should work correctly for Adobe Reader 7, 8 and 9. If you’re interested in helping add a translation for another language please let me know. There are currently only 3 items that need translating – “Bookmark”, “Bookmark This Page” and “Go To Bookmark”.

See the full details and download the plugin.

WordPress 2.5 is officially out

WordPress 2.5

It’s official. WordPress 2.5 is now out with a crisp new admin interface and great new features. They’ve even redone the website. Here are some of my favorite changes:

  • A beautiful new admin interface.
  • A much more intuitive flow when making a post.
  • Multiple instant file uploads.
  • Thumbnail, medium and full-sized image options (and you can change the image dimensions in Settings).
  • A quick way to display your photos as a gallery
  • Easily add/edit tags so no more plugins are needed.
  • A visual editor that doesn’t break your code (this one is great for my clients).
  • Search now looks at posts and pages.

I’ve always thought that WordPress is one of the best blogging and content management systems. It’s incredibly intuitive for the user (even more so now) and extremely flexible for developers.

Thanks so much to everyone who has helped with WordPress!

Plugin Update: PDF Bookmark for Adobe Reader 8

Some of you may already be using the PDF bookmark plugin I put together for Adobe Reader. It provides a quick and easy way to bookmark the page your on and return back to that spot later. It’s especially handy when reading large PDF books.

Thanks to the input of Michael Hartl, I updated the plugin to work properly with Adobe Reader 8. Adobe turned on some security settings by default in the new Adobe Reader which broke the previous version of the plugin.

See the full details and download the plugin.

Installing ImageMagick/RMagick on Leopard

I’ve heard many horror stories of developers trying to install ImageMagick/RMagick to manipulate images. Fortunately, when I needed to install RMagick to use with the attachment_fu plugin, I ran across a fantastic script at OnRails.org by Solomon White (many thanks). It gave the steps to install RMagick from source without MacPorts or Fink.

I made the following minor changes to get it working for me and posted the script below.

  • Changed ‘wget’ to ‘curl -O’
  • Updated a couple of links that weren’t working for me
  • Updated sourceforge links to the east coast
  • Updated links to latest version of source code (as of today)
#!/bin/sh
curl -O http://download.savannah.gnu.org/releases/freetype/freetype-2.3.5.tar.gz
tar xzvf freetype-2.3.5.tar.gz
cd freetype-2.3.5
./configure --prefix=/usr/local
make
sudo make install
cd ..

curl -O http://superb-east.dl.sourceforge.net/sourceforge/libpng/libpng-1.2.23.tar.bz2
tar jxvf libpng-1.2.23.tar.bz2
cd libpng-1.2.23
./configure --prefix=/usr/local
make
sudo make install
cd ..

curl -O http://www.ijg.org/files/jpegsrc.v6b.tar.gz
tar xzvf jpegsrc.v6b.tar.gz
cd jpeg-6b
ln -s `which glibtool` ./libtool
export MACOSX_DEPLOYMENT_TARGET=10.5
./configure --enable-shared --prefix=/usr/local
make
sudo make install
cd ..

curl -O ftp://ftp.remotesensing.org/libtiff/tiff-3.8.2.tar.gz
tar xzvf tiff-3.8.2.tar.gz
cd tiff-3.8.2
./configure --prefix=/usr/local
make
sudo make install
cd ..

curl -O http://superb-east.dl.sourceforge.net/sourceforge/wvware/libwmf-0.2.8.4.tar.gz
tar xzvf libwmf-0.2.8.4.tar.gz
cd libwmf-0.2.8.4
make clean
./configure
make
sudo make install
cd ..

curl -O http://www.littlecms.com/lcms-1.17.tar.gz
tar xzvf lcms-1.17.tar.gz
cd lcms-1.17
make clean
./configure
make
sudo make install
cd ..

curl -O http://superb-east.dl.sourceforge.net/sourceforge/ghostscript/ghostscript-8.61.tar.gz
tar zxvf ghostscript-8.61.tar.gz
cd ghostscript-8.61/
./configure  --prefix=/usr/local
make
sudo make install
cd ..

curl -O http://mirror.cs.wisc.edu/pub/mirrors/ghost/GPL/current/ghostscript-fonts-std-8.11.tar.gz
tar zxvf ghostscript-fonts-std-8.11.tar.gz
sudo mv fonts /usr/local/share/ghostscript

curl -O ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick-6.3.7-1.tar.gz
tar xzvf ImageMagick-6.3.7-1.tar.gz
cd ImageMagick-6.3.7
export CPPFLAGS=-I/usr/local/include
export LDFLAGS=-L/usr/local/lib
./configure --prefix=/usr/local --disable-static --with-modules --without-perl --without-magick-plus-plus --with-quantum-depth=8 --with-gs-font-dir=/usr/local/share/ghostscript/fonts
make
sudo make install
cd ..

To test that ImageMagick was installed properly you can check the version number.

convert -version

[you should see something like this]
Version: ImageMagick 6.3.7 11/26/07 Q8 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2007 ImageMagick Studio LLC

To test that ImageMagick is working properly you can run the following test. It will create a copy of the ImageMagick logo.

convert logo: logo.gif

script.aculo.us Slider Demo : Update Text Field and Change Slider

For those of you using the script.aculo.us Slider demos I’ve just added a new one. Sometimes you may have a slider and a text field to show the value. This example lets you change the value in the text box and the slider will update to match the value.

Example: Change Slider Value by Changing Text Input Field

Moment of Open Source Zen

Every day, as a developer, I’m grateful for the thousands of programmers around the world who have so graciously shared their time, knowledge, and programming code. I have access to millions of lines of free code at any moment through the Internet which allows me to write programs more easily, learn new concepts, and make a difference in society.

Yes, every open source developer plays their part in making life better. Take, for example, the recent updates to Sean Coon’s blog. A few tweaks to some great free programming code and Sean’s rapidly spreading the word about The People, Yes which directly engages the homeless community to share their voice through blogging. He’s able to send a text message which is then automatically relayed to his friends/acquaintances (through Twitter) and also posted to his blog (using a modified version of Alex King’s Twitter Tools plugin). Don’t forget that in conjunction with this great plugin there are other pieces of the free code puzzle – the powerful blogging tool, the simple programming language, and the server’s solid operating system. All of this was made possible by many generous programmers sharing their time and skills.

So here’s to the programming language, plugin, framework, and code snippet givers around the globe. You truly make a difference in the world.

The Architect and Builder Dilemma

Blueprint
(Originally uploaded by sweetsexything)

One of the problems I’ve seen over the years, in corporations where I’ve worked and as a business owner, is the misconception that a builder (a developer in this case) can quote a project without the blueprints. I often times receive a request for a quote (10 pages long) and it says something like this:

We want a dynamic website with a unique design and easy to follow navigation that we can update ourselves.

That’s basically the equivalent of going to a car dealership and asking “how much is a car with wheels and doors?” Until you tell them the make, model, and all of the features you want they can’t give you a real price.

The Problem
The problem with this process is that the client and the developer both have expectations and a vision for the project but you’re not talking apples-to-apples. Sure, the developer can give a quote based on vague information but everyone loses during the process. The client loses because their expectations aren’t going to be met. The developer loses since they can’t possibly give a realistic quote. What do you do when you start working and the client expects the Ferrari (which they all do)? Both parties clash when they aren’t on the same page.

The Solution
Hire an architect. When I get a vague request for a quote I tell the client it will take a few hours of billable time and we’ll make a blueprint together. There is no reason for me to give a vague quote on a vague request. I’ll be quoting the Ferrari just to cover every possibility, and the client can’t afford the Ferrari. No one expects an architect to build a blueprint for free and it should be the same with a developer. Once the client has the blueprint they can send it out for quoting and will get apple-to-apple quotes back instead of a fruit basket.

No one is to blame for this misconception since the idea of web development is new and mysterious to most people. However, I challenge all developers to change things and educate the clients that come asking for help. They’ll appreciate the education, respect the architect idea, save money, save time, and you’ll prevent lots of future headaches.

script.aculo.us Slider Demos and Example Code

I’ve been following along the script.aculo.us Slider Demo discussion area recently and have tried to answer questions when possible. Since it’s difficult to post examples to the discussion I’ve compiled a more extensive set of Slider demos. Some are examples that I’ve wanted and others are in response to questions in the discussion area.

The demo code includes the following slider examples:

  • Standard Slider
  • Reversed Slider
  • Slider Controlled with Mouse Wheel
  • One Slider Controlling Multiple Sliders
  • Using Images to Spruce Up a Slider
  • Two Colored Slider
  • Submit the Slider Value in a Form
  • Use a Slider as a Scrollbar (added Nov 21, 2006)
  • Change Slider Value by Changing Text Input Field (added Jul 16, 2007)

It’s really amazing what you can do with the script.aculo.us library. Please let me know what you think!