ImageIIIF::info()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 23
rs 9.7998
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Conlect\ImageIIIF;
4
5
use Intervention\Image\ImageManager;
6
use Noodlehaus\Config;
7
8
class ImageIIIF
9
{
10
    use \Conlect\ImageIIIF\Traits\SupportedFormats;
11
12
    public $manager;
13
14
    protected $config;
15
16
    public $image;
17
18
    public function __construct(ImageManager $manager, Config $config = null)
19
    {
20
        $this->manager = $manager;
21
        $this->config = $config;
22
    }
23
24
    public function load($file)
25
    {
26
        // dd($file);
27
        $this->image = $this->manager->read(file_get_contents($file));
28
29
        return $this;
30
    }
31
32
    public function withParameters(array $parameters)
33
    {
34
        $this->applyParameters($parameters);
35
36
        return $this;
37
    }
38
39
    public function stream()
40
    {
41
        return $this->image;
42
    }
43
44
    protected function applyParameters(array $parameters)
45
    {
46
        $iiifParameters = [
47
            'region' => \Conlect\ImageIIIF\Parameters\Region::class,
48
            'size' => \Conlect\ImageIIIF\Parameters\Size::class,
49
            'rotation' => \Conlect\ImageIIIF\Parameters\Rotation::class,
50
            'quality' => \Conlect\ImageIIIF\Parameters\Quality::class,
51
            'format' => \Conlect\ImageIIIF\Parameters\Format::class,
52
        ];
53
54
        foreach ($parameters as $parameter => $options) {
55
            if (! in_array($parameter, array_keys($iiifParameters))) {
56
                continue;
57
            }
58
59
            $this->image = (new $iiifParameters[$parameter]($this->image))->apply($options);
60
        }
61
    }
62
63
    public function hasValidParameters(array $parameters)
64
    {
65
        $validators = [
66
            'region' => \Conlect\ImageIIIF\Validators\RegionValidator::class,
67
            'size' => \Conlect\ImageIIIF\Validators\SizeValidator::class,
68
            'rotation' => \Conlect\ImageIIIF\Validators\RotationValidator::class,
69
            'quality' => \Conlect\ImageIIIF\Validators\QualityValidator::class,
70
            'format' => \Conlect\ImageIIIF\Validators\FormatValidator::class,
71
        ];
72
73
        foreach ($parameters as $parameter => $value) {
74
            if (! in_array($parameter, array_keys($validators))) {
75
                continue;
76
            }
77
78
            if (! (new $validators[$parameter]($this->config, $this->image))->valid($value)) {
79
                return false;
80
            }
81
        }
82
83
        return true;
84
    }
85
86
    public function info(string $prefix, string $identifier)
87
    {
88
        // TODO
89
        // Optional - maxWidth, maxHeight, maxArea
90
        // sizes - prefered w,h pairs
91
        // rights - CC license
92
        return [
93
            '@context' => 'http://iiif.io/api/image/3/context.json',
94
            'id' => $this->config['base_url'] . '/' . $prefix . '/' . $identifier,
95
            'type' => 'ImageService3',
96
            'protocol' => 'http://iiif.io/api/image',
97
            'profile' => 'level2',
98
            'height' => $this->image->height(),
99
            'width' => $this->image->width(),
100
            'tiles' => [
101
                [
102
                    'width' => $this->config['tile_width'],
103
                    'scaleFactors' => $this->getScaleFactors(),
104
                ],
105
            ],
106
            'extraFormats' => $this->getExtraFormats(),
107
            'extraQualities' => ['color', 'gray'],
108
            'extraFeatures' => $this->config['extraFeatures'],
109
        ];
110
    }
111
112
    protected function getScaleFactors()
113
    {
114
        $scaleFactors = [];
115
        $maxSize = max($this->image->width(), $this->image->height());
116
        $total = (int) ceil($maxSize / $this->config['tile_width']);
117
        $factor = 1;
118
        while ($factor / 2 <= $total) {
119
            $scaleFactors[] = $factor;
120
            $factor = $factor * 2;
121
        }
122
        if (count($scaleFactors) <= 1) {
123
            return;
124
        }
125
126
        return $scaleFactors;
127
    }
128
129
    protected function getExtraFormats()
130
    {
131
        $formats = $this->getSupportedFormats((string) $this->config['driver']);
132
133
        // All formats except level2 deafults of jpg and png
134
        return array_intersect($formats, ['jpg', 'png']);
135
    }
136
}
137