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