<?php

/**
 * Tomthumb contains our dependencies 
 * (imageProcessor, autoload)
 */
if(is_file('image/tomthumb.loader.php')) {
    require_once('image/tomthumb.loader.php');
} elseif(is_file(dirname(dirname(dirname(dirname(__FILE__)))) . '/image/tomthumb.loader.php')) {
    require_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/image/tomthumb.loader.php');
} elseif(is_file('/usr/share/php/image/tomthumb.loader.php')){
    require_once('/usr/share/php/image/tomthumb.loader.php');
}


class s3resizer extends ImageProcessor {
 
    /**
     * Build our image processor
     * Bucket: cms-image-content
     */
    function __construct() {
        
        // aws_lambda.ini
        $conf = parse_ini_file('aws_lambda.ini');
    
        $this->aws_access_key   = $conf['AWS_KEY'];
        
        $this->aws_secret_access_key = $conf['AWS_SECRET'];
        
        if(!empty($conf['PHP_COMMAND'])) {
            $this->php_command = $conf['PHP_COMMAND'];
        }
        
        $this->setBucket('leisure');
    }
    
    /**
     * Create thumbs without forks.
     * 
     * @return type
     */
    public function createResizedImages() {
        $default = 
            $this->extra_thumb(559, 420, '_default');
            $this->extra_thumb(338, 224, '_large');
            $this->extra_thumb(295, 125, '_medium');
            $this->extra_thumb(126, 83,  '_small');

        return $default;
    }
    
    /**
     * Create thumbs via forks
     * 
     * @return type
     */
    public function createResizedImagesForks() {
        $queue = $this->buildResizedImagesQueue();
        return $this->fork($queue);
    }
    
    /**
     * Create queue array
     * 
     * @return type
     */
    public function buildResizedImagesQueue() {
        return [
            'queue' => [
                [ 
                    'path'  => "{$this->hash}/{$this->filename}-r_default{$this->extension}",
                    'w'     => 559,
                    'h'     => 420,
                    'crop'  => true
                ],[ 
                    'path'  => "{$this->hash}/{$this->filename}-r_large{$this->extension}",
                    'w'     => 338,
                    'h'     => 224,
                    'crop'  => true
                ],[ 
                    'path'  => "{$this->hash}/{$this->filename}-r_medium{$this->extension}",
                    'w'     => 295,
                    'h'     => 125,
                    'crop'  => true
                ],[ 
                    'path'  => "{$this->hash}/{$this->filename}-r_small{$this->extension}",
                    'w'     => 126,
                    'h'     => 83,
                    'crop'  => true
                ]
            ],
            'cache' => [
                'path'      => $this->reduce_path,
                'bucket'    => $this->getBucket(),
                'AWS_KEY'   => $this->aws_access_key,
                'AWS_SECRET' => $this->aws_secret_key,
            ]
        ];
    }
}

    