Example Controller for Images

Example Image Controller by @WinterSilence:

Route::set(
    'assets_thumb', 
    'assets/images/<dir>/thumbs/<width>x<height>/<file>',
    ['dir' => '[-\w]+', 'width' => '\d+', 'height' => '\d+', 'file' => '[-.\w]+\.(gif|png|jpg|jpeg)']
)->defaults(['controller' => 'Assets', 'action' => 'image', 'width' => 100, 'height' => 100]);
class Controller_Assets extends Controller {
    public function action_image()
    {
        $dir = implode(
            DIRECTORY_SEPARATOR, 
            [DOCROOT.'assets', 'images', $this->request->param('dir')]
        );
        $file = $this->request->param('file');

        $source_file = $dir.DIRECTORY_SEPARATOR.$file;
        if (file_exists($source_file))
        {
            $width = $this->request->param('width');
            $height = $this->request->param('height');
            
            $dir .= DIRECTORY_SEPARATOR.'thumbs'.DIRECTORY_SEPARATOR.$width.'x'.$height;
            if ( ! is_dir($dir))
            {
                mkdir($dir, 0755, TRUE);
            }
            $thumb_file = $dir.DIRECTORY_SEPARATOR.$file;
            
            // Use `image` module for create thumbnail
            $image = Image::factory($source_file);
            $image->resize($width, $height)->save($thumb_file, 75);

            $this->response->headers([
                'Content-Type'  => $image->mime,
                'Last-Modified' => date('r', filemtime($thumb_file))
            ]);
            $this->response->body(file_get_contents($thumb_file));
        }
        else
        {
            $this->response->status(404);
        }
    }
}