| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the `liip/LiipImagineBundle` project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE.md | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Liip\ImagineBundle\Imagine\Filter\Loader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Imagine\Image\ImageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Imagine\Image\ImagineInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Liip\ImagineBundle\Exception\Imagine\Filter\LoadFilterException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Liip\ImagineBundle\Exception\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\OptionsResolver\Exception\ExceptionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\OptionsResolver\Options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\OptionsResolver\OptionsResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class ResampleFilterLoader implements LoaderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var ImagineInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $imagine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param ImagineInterface $imagine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function __construct(ImagineInterface $imagine) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->imagine = $imagine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param ImageInterface $image | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param array          $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @throws LoadFilterException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @return ImageInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     public function load(ImageInterface $image, array $options = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $options = $this->resolveOptions($options); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $tmpFile = $this->getTemporaryFile($options['temp_dir']); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             $image->save($tmpFile, $this->getImagineSaveOptions($options)); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             $image = $this->imagine->open($tmpFile); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             $this->delTemporaryFile($tmpFile); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         } catch (\Exception $exception) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             $this->delTemporaryFile($tmpFile); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             throw new LoadFilterException('Unable to save/open file in resample filter loader.', null, $exception); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         return $image; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @throws \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     private function getTemporaryFile($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if (!is_dir($path) || false === $file = tempnam($path, 'liip-imagine-bundle')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             throw new \RuntimeException(sprintf('Unable to create temporary file in "%s" base path.', $path)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return $file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @throws \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     private function delTemporaryFile($file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         if (file_exists($file)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             unlink($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     private function getImagineSaveOptions(array $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $saveOptions = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             'resolution-units' => $options['unit'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             'resolution-x' => $options['x'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             'resolution-y' => $options['y'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         if (isset($options['filter'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $saveOptions['resampling-filter'] = $options['filter']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return $saveOptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     private function resolveOptions(array $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $resolver = new OptionsResolver(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $resolver->setRequired(['x', 'y', 'unit', 'temp_dir']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $resolver->setDefined(['filter']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $resolver->setDefault('temp_dir', sys_get_temp_dir()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $resolver->setDefault('filter', 'UNDEFINED'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $resolver->setAllowedTypes('x', ['int', 'float']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $resolver->setAllowedTypes('y', ['int', 'float']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $resolver->setAllowedTypes('temp_dir', ['string']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $resolver->setAllowedTypes('filter', ['string']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $resolver->setAllowedValues('unit', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             ImageInterface::RESOLUTION_PIXELSPERINCH, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             ImageInterface::RESOLUTION_PIXELSPERCENTIMETER, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         $resolver->setNormalizer('filter', function (Options $options, $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             foreach (['\Imagine\Image\ImageInterface::FILTER_%s', '\Imagine\Image\ImageInterface::%s', '%s'] as $format) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 if (\defined($constant = sprintf($format, mb_strtoupper($value))) || \defined($constant = sprintf($format, $value))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                     return \constant($constant); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             throw new InvalidArgumentException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                 'Invalid value for "filter" option: must be a valid constant resolvable using one of formats '. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 '"\Imagine\Image\ImageInterface::FILTER_%s", "\Imagine\Image\ImageInterface::%s", or "%s".' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             return $resolver->resolve($options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         } catch (ExceptionInterface $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             throw new InvalidArgumentException(sprintf('Invalid option(s) passed to %s::load().', __CLASS__), null, $exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 153 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 154 |  |  |  |