Create a Photo Gallery with PHP and the Rackspace Cloud Files API

Setting up and working with the Rackspace Cloud Files API is not as straight-forward as it could be, so when I had a project that required figuring it out I decided to go all the way and create a complete photo gallery app that I could share on GitHub - and hopefully help a few people.

This article is mostly going to cover installation of the php-opencloud library, setting up the MySQL database and a few other items; but don’t worry: everything else you’ll need is available in the GitHub repo - and that code is heavily commented - so you shouldn’t have any problem understanding what’s going on.

Assumption Alert!
This article makes some assumptions: that you're familiar with PHP; that you can use the command line (Terminal); that you have a website on which to host files (local or remote), that you have access to MySQL, and that you have - or will have - a Rackspace Cloud Files account.

Download the Cloud Files Image Gallery repo, unzip it and rename the folder to opencloud. The contents of this folder will be our site, so place it on a server where it’s accessible from a browser via http; this can be a local Mamp/Wamp installation or a remote web server…

2. Install Composer

We need Composer to install the Rackspace libraries, therefore we need to install Composer - and we’ll do that from the command line - so open a shell (Terminal) and cd to the opencloud/assets/plugins/open-cloud directory.

cd opencloud/assets/plugins/open-cloud

To install Composer, copy the following line and paste it into your shell.

curl -sS https://getcomposer.org/installer | php

3. Install the SDK

After Composer has finished installing we need to install the PHP SDK for OpenStack/Rackspace APIs. Copy/paste the following into your shell.

php composer.phar require rackspace/php-opencloud

4. Configure Uploads

Since we’ll be uploading files we need a local directory in which to process each image before sending it off to Rackspace, so we need to make sure the permissions are correct on our uploads directory. Copy/paste the following into your shell.

chmod 0777 ../../../uploads

We’re finished with the command line, you can close the shell window now.

5. Create a Database

Every file you upload to Rackspace is considered an object, and each object needs to be stored in a container, so we need a database to keep track of the names of our objects and in which container they’re stored.

Create a new MySQL database and name it open-cloud, then copy the following SQL queries and run them against our new open-cloud database. These queries are also available in the open-cloud.sql file in the root of our site.

CREATE TABLE `galleries` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(60) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `container` varchar(60) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `private` char(1) NOT NULL DEFAULT '0',
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `container` (`container`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `photos` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `gallery_id` int(11) unsigned NOT NULL,
  `photo` char(39) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `container` varchar(60) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `gallery_id` (`gallery_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

6. Setup ezSQL

Our example site uses ezSQL as the database class, so if you want to get up and running quickly (without changing any code), use ezSQL; here’s all you need to do.

  1. Download ezSQL from GitHub, unzip it and place it into the plugins directory. Don’t rename the ezSQL directory because our site’s config file is already pointing at it. It should be at /assets/plugins/ezSQL-master.

7. Enable ImageMagick

Since we’re creating galleries we’ll need to resize and rename our images, so we’ll be using ImageMagick for that. If you don’t have ImageMagick (or don’t want to use it) you will need to change some code in assets/php/upload.php; the file is commented so you can easily find it, and the code is simple, so you can easily change it.

If you’re using Mamp Pro >= 3.x, here’s how to enable ImageMagick.

  1. Open Mamp Pro
  2. Go to File > Edit Template > PHP > PHP 5.6.x
  3. The php.ini file will open.
  4. Go to File > Find, then search for magick
  5. Uncomment ;extension=imagick.so, so it’s now extension=imagick.so
  6. Save and close php.ini and restart the servers. That’s it!

8. Configuration

Open assets/config.php in a text editor and make the appropriate edits marked by the comments. You’ll be entering your database information, your Rackspace API credentials (Your API key is in your Rackspace Account Settings) and changing the container location if you don’t want the default (Dallas/Fort Worth).

9. Create Galleries!

Finally, open the website in your browser and start uploading!


Additional Resources

PHP SDK for OpenStack/Rackspace APIs
SDK Sample Code