Completed
Push — master ( 9873f2...bcf436 )
by Kanstantsin
02:30
created

Image   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentInternal() 0 7 1
B format() 0 17 6
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: konstantin
5
 * Date: 10/16/16
6
 * Time: 2:54 PM
7
 */
8
9
namespace tkanstantsin\fileupload\formatter;
10
11
use Imagine\Image\Box;
12
use Imagine\Image\ImageInterface;
13
use Imagine\Imagick\Imagine;
14
15
/**
16
 * Class ImageProcessor
17
 */
18
class Image extends File
19
{
20
    /**
21
     * Like background `cover` in css.
22
     */
23
    public const RESIZE_OUTBOUND = \Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND;
24
    /**
25
     * Like background `contain` in css.
26
     */
27
    public const RESIZE_INSET = \Imagine\Image\ImageInterface::THUMBNAIL_INSET;
28
    /**
29
     * Means that image may be smaller than defined in config, never bigger.
30
     */
31
    public const RESIZE_INSET_KEEP_RATIO = 'inset_keep_ratio';
32
33
    public const DEFAULT_EXTENSION = 'jpg';
34
35
    /**
36
     * @var int
37
     */
38
    public $width;
39
    /**
40
     * @var int
41
     */
42
    public $height;
43
    /**
44
     * @var string
45
     */
46
    public $mode = self::RESIZE_INSET;
47
    /**
48
     * Whether image must keep aspect ration when used inset mote.
49
     * Means that image would may be smaller than smaller
50
     * @var bool
51
     */
52
    public $keepRatio = true;
53
54
    /**
55
     * @var Imagine
56
     */
57
    protected $imagine;
58
59
    /**
60
     * @inheritdoc
61
     * @throws \UnexpectedValueException
62
     * @throws \Imagine\Exception\InvalidArgumentException
63
     * @throws \Imagine\Exception\RuntimeException
64
     */
65
    protected function getContentInternal()
66
    {
67
        $this->imagine = new Imagine();
68
        $image = $this->imagine->read(parent::getContentInternal());
69
        $image = $this->format($image);
70
71
        return $image->get($this->file->getExtension() ?? self::DEFAULT_EXTENSION);
72
    }
73
74
    /**
75
     * @param ImageInterface $image
76
     * @return ImageInterface
77
     * @throws \UnexpectedValueException
78
     * @throws \Imagine\Exception\RuntimeException
79
     * @throws \Imagine\Exception\InvalidArgumentException
80
     */
81
    protected function format(ImageInterface $image): ImageInterface
82
    {
83
        if ($this->width === null || $this->height === null) {
84
            return $image;
85
        }
86
87
        $box = new Box($this->width, $this->height);
88
89
        switch ($this->mode) {
90
            case self::RESIZE_OUTBOUND:
91
            case self::RESIZE_INSET:
92
                return $image->thumbnail($box, $this->mode);
93
            case self::RESIZE_INSET_KEEP_RATIO:
94
                // TODO: implement new resize mode.
95
                throw new \UnexpectedValueException(sprintf('Resize mode `%s` not supported yet', $this->mode));
96
            default:
97
                throw new \UnexpectedValueException(sprintf('Image resize mode `%s` not defined', $this->mode));
98
        }
99
    }
100
}