1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 
<?php
namespace Omeka\File\Thumbnailer;

use Omeka\File\TempFile;

abstract class AbstractThumbnailer implements ThumbnailerInterface
{
    /**
     * @var string
     */
    protected $source;

    /**
     * @var File
     */
    protected $sourceFile;

    /**
     * @var array
     */
    protected $options;

    /**
     * {@inheritDoc}
     */
    public function setSource(TempFile $source)
    {
        $this->source = $source->getTempPath();
        $this->sourceFile = $source;
    }

    /**
     * {@inheritDoc}
     */
    public function setOptions(array $options)
    {
        // Set options only once for shared services.
        if (!isset($this->options)) {
            $this->options = $options;
        }
    }

    /**
     * Get an option.
     *
     * @param string $option
     * @param string $default
     * @return string
     */
    public function getOption($option, $default = null)
    {
        return isset($this->options[$option]) ? $this->options[$option] : $default;
    }

    /**
     * For the square strategy, get the required offset on the X axis.
     *
     * @param int $width Original image width
     * @param int $size Side size of the square region being selected
     * @param string $gravity
     * @return int
     */
    public function getOffsetX($width, $size, $gravity)
    {
        switch ($gravity) {
            case 'northwest':
            case 'west':
            case 'southwest':
                return 0;
            case 'northeast':
            case 'east':
            case 'southeast':
                return $width - $size;
            case 'north':
            case 'center':
            case 'south':
            default:
                return (int) (($width - $size) / 2);
        }
    }

    /**
     * For the square strategy, get the required offset on the Y axis.
     *
     * @param int $height Original image height
     * @param int $size Side size of square region being selected
     * @param string $gravity
     * @return int
     */
    public function getOffsetY($height, $size, $gravity)
    {
        switch ($gravity) {
            case 'northwest':
            case 'north':
            case 'northeast':
                return 0;
            case 'southwest':
            case 'south':
            case 'southeast':
                return $height - $size;
            case 'west':
            case 'center':
            case 'east':
            default:
                return (int) (($height - $size) / 2);
        }
    }
}