The Uploader

The Uploader is the core of Hitch. It directs your application how and where to store files. Simply define an uploader by extending Hitch\Uploader.

class MyUploader extends \Hitch\Uploader {}

The most important method provided by an uploader is its store() method. Simply provide a File object, and Hitch will take care of the rest:

public function controllerAction()
{
     $file = $this->getRequest()->getFile('file');

     $uploader = new MyUploader();

     $uploader->store($file);
}

Storage Adapters

The uploader passes the files that it generates off to Storage Adapters. You can specify which storage adapters to use by overriding the uploader’s getStorageAdapters() method:

public function getStorageAdapters()
{
    $root_path = get_path_of_public_dir();

    return array(
        new \Hitch\Storage\File($root_path)       
    );
}

File Storage Adapter

Hitch\Storage\File provides a means to store a file on the local filesystem. It’s also the default storage adapter. Simply pass the root upload directory as the constructor, and your files will be stored:

new \Hitch\Storage\File($root . "/public/images/");

Version Descriptions

Hitch will automatically generate different versions of each uploaded files for you. You must describe each version that you require:

public function getVersionDescriptions()
{
    'thumb' => array(
        'resizeKeepAspect' => array(100, 100)
    ),
    'icon' => array(
        'resize' => array(16, 16)
    )
}

resize process

resize an image to exactly the supplied dimensions

"resize" => array(100, 100)
_images/original.jpg

becomes

_images/resize.100x100.jpg

resizeKeepAspect process

Resize an image while maintaining its aspect ratio.

"resizeKeepAspect" => array(100, 100)
_images/original.jpg

becomes

_images/resizeKeepAspect.100x100.jpg

Available processes:

  • resizeKeepAspect($width, $height) - Resize the image to the specified size, while keeping the aspect ratio

File Naming

You may specify how your files are named by overriding getFilename() and getVersionPath():

public function getFilename($original, $version = null)
{
    // Save files with timestamps
    $extension = $original->getExtension();
    return microtime(true) . $extension;
}

public function getVersionPath($original, $version = null)
{
    $filename = $this->getFilename($original, $version);

    // Save versions in their own subdirectory
    if (is_null($version)) {
        return "images/" . $filename;
    } else {
        return "images/" . $version . "/" . $filename;
    }
}