Creating a Photo Tag Wall With Twilio Picture Messaging & PHP

Twilio's recently announced Picture Messaging has vastly opened up what we can do with text messaging, now we can attach photos to our text messages and have them get used in different ways.

In our case, we are going to build a Photo Tag Wall, which will contain photos linked to tags that will be displayed on a website.

This can be handy for events, or parties, or just about anything where you want to associate photos and tags.

To process our photos, we’ll be doing a few different things; We’re going to store them locally, and then resize them to display on our screens nicer.

We're going to use the Jolt Microframework for PHP, and Idiorm and Paris for our MySql handling.


Getting Started

Ok, first let's set up our database:

We're setting up two tables, one to store the tag, and one for the photos and the id to the tag they are associated with.

This table will store the tag, the image, and some meta data about the phone number that sent the photo.

We're also going to need to download the Jolt framework, the Twilio PHP Library, and Idiorm and Paris.

The first thing you'll want to do is grab these packages from their respective websites:

Now that you have all the packages downloaded to your computer, it's time to setup your directory structure. We'll be putting the files into our site's root folder.

We're putting the web services related files inside the Services folder, since it helps us watch where things are.

Ok, let's set up our config.ini file, open up config.ini in your editor and modify the following settings:

You can see what you'll have to fill in here, your site name, and URL, your database info and your Twilio info.


Now for the Coding!

To get started, let's set up our models. We'll create a file inside the system folder called models.php:

This is a pretty basic model layout, but one nice thing about it, is that we're using Paris to establish a relationship with the tag table. In fact, because we've previously built our database to have a tag_id field in the photo table, this model knows to associate all photos with the tag_id, where tag_id is the table name and the primary key field in the tag table.

The same is true for the Photo class, where we've set it to belong to a tag as specified in the tag() function.

This is handy for building a quick model system without a lot of overhead.

We also want to create our functions.php file, which we will also keep inside the system folder:

functions.php will contain two core functions, one function, slugify(), will convert tag names into slugs, and the cropResize() function will take the image we pass to it, and save it within new dimensions.

We'll be using these functions quite a lot coming up.

Most of our code will be stored inside index.php, so let's set up the bare bones for it:

Ok, we've included our files, and nothing happened. Now, let's get Jolt up and running:

The above code just sets up Jolt and tells it to read the config.ini file and set our configuration settings, now let's connect to our database:

Our final piece of bootstrapping, we want to set up our Twilio client:

This is our bootstrap section, so far all we've done is included our files, set up our Jolt app, connected to our database and initialized our Twilio client.

Right now, if you run your app, you'll get a few errors. This is fine, we'll be taking care of those errors next.


Routing

Now we have to set up our routes and tell our app what to do based on certain rules. These rules will be either get or post.

Our initial rules will be the home page, the tag page, and the listener:

We've just set up the initial bare bones actions for our homepage, which is represented by the '/', our tag page, and our listener.

You'll notice the listener is a post rather than a get, that is because this is the handler from Twilio when new messages are received.

Lastly, you'll see the $app->listen(); method call. This is the most important method call we have, as it tells the app to start running.


There's No Place Like Home

Let's set up the home page, and build the view that we'll be displaying for everybody.

Replace the original homepage route with this one:

You'll also notice that we tell it to render something called 'home', in the views folder, there is a home.php file, open it up and edit it as follows:

This file will take the variables we pass from the $app->render() function and make use of them here.

We're going to display a count of total tags, along with total images, and a list of tags that a visitor can click on.

The actual page layout is controlled by a file called layout.php. Let's go ahead and update that file now:

This is pretty bare bones HTML, but it covers what we're needing. All output gets sent to the $pageContent variable in layout.php.


Picture Messaging!

Ok, now let's handle the actual uploading of pictures from Twilio.

Log into your Twilio account and point a phone number to http://MYSITEURL/listener for SMS messages, where MYSITEURL is the address where you've uploaded your app.

We're going to replace our listener route with this one:

There is no view associated with this action. Now, let's go over what it does.

This is only called during a post, hence the $app->post() statement.

When it is activated by someone sending in a message, we check to see if there are any images attached, and if there are, then we cycle through them and save them in the database.

First, we check to see if there are any tags already stored in our database that match the tag we attached to our image, and if there is, then we grab the id from the database, otherwise, we save a new record containing that tag's information.

Next, we cycle through the uploaded files and make sure they are images. Each image is downloaded locally and stored inside the images/original folder. We then resize and crop the images to be a more manageable size, and store the new files inside the images/processed folder.

Finally, we store the images inside the database, along with some meta data on the call itself, and send a text message back to the sender to tell him or her to check out the tag page.

If no images were attached, then we send them a message that there was an error.


The Photo Wall

Now, we've set up the home page, and we've set up the listener. What's left, is to set up the photo wall itself.

This will go inside the $app->get(‘/tag/:tag') call.

Replace the original placeholder with the following code:

Notice the $app->filter(), This is a handy method we can set up that will grab the tag and its photos each time the $tag_slug variable is passed, this lets us save on extra queries.

Now, we need to set up a gallery.php page inside views:

This will display the gallery, and use jQuery masonry to float all the images nicely.


In Conclusion

So that completes our app. You've now built a handy little photo wall that can be used to show photos from events. Be sure to checkout the links provided above to learn more about the libraries and frameworks used throughout this article. Thanks for reading.

Tags:

Comments

Related Articles