| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace PiedWeb\CMSBundle\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use WebPConvert\Convert\ConverterFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use WebPConvert\Convert\Converters\Stack; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use WebPConvert\Convert\Exceptions\ConversionFailed\ConversionSkippedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperationalException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use WebPConvert\Convert\Exceptions\ConversionFailedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use WebPConvert\Options\GhostOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Waiting for this PR being accepted : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * https://github.com/rosell-dk/webp-convert/pull/251. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | class WebPConverter extends Stack | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     protected $converterUsed; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     protected function doActualConvert() | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         $options = $this->options; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         $beginTimeStack = microtime(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         $anyRuntimeErrors = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $converters = $options['converters']; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         if (\count($options['extra-converters']) > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             $converters = array_merge($converters, $options['extra-converters']); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             /*foreach ($options['extra-converters'] as $extra) { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |                 $converters[] = $extra; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             }*/ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         // preferred-converters | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         if (\count($options['preferred-converters']) > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             foreach (array_reverse($options['preferred-converters']) as $prioritizedConverter) { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |                 foreach ($converters as $i => $converter) { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |                     if (\is_array($converter)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |                         $converterId = $converter['converter']; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |                     } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |                         $converterId = $converter; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |                     if ($converterId == $prioritizedConverter) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |                         unset($converters[$i]); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |                         array_unshift($converters, $converter); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                         break; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             // perhaps write the order to the log? (without options) - but this requires some effort | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         // shuffle | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         if ($options['shuffle']) { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             shuffle($converters); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         //$this->logLn(print_r($converters)); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         //$options['converters'] = $converters; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         //$defaultConverterOptions = $options; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         $defaultConverterOptions = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         foreach ($this->options2->getOptionsMap() as $id => $option) { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             if ($option->isValueExplicitlySet() && ! ($option instanceof GhostOption)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                 //$this->logLn('hi' . $id); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |                 $defaultConverterOptions[$id] = $option->getValue(); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         //unset($defaultConverterOptions['converters']); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         //unset($defaultConverterOptions['converter-options']); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         $defaultConverterOptions['_skip_input_check'] = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         $defaultConverterOptions['_suppress_success_message'] = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         unset($defaultConverterOptions['converters']); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         unset($defaultConverterOptions['extra-converters']); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         unset($defaultConverterOptions['converter-options']); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         unset($defaultConverterOptions['preferred-converters']); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         unset($defaultConverterOptions['shuffle']); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  | //        $this->logLn('converters: ' . print_r($converters, true)); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         //return; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         foreach ($converters as $converter) { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             if (\is_array($converter)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |                 $converterId = $converter['converter']; | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |                 $converterOptions = isset($converter['options']) ? $converter['options'] : []; | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |                 $converterId = $converter; | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |                 $converterOptions = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |                 if (isset($options['converter-options'][$converterId])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |                     // Note: right now, converter-options are not meant to be used, | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |                     //       when you have several converters of the same type | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |                     $converterOptions = $options['converter-options'][$converterId]; | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             $converterOptions = array_merge($defaultConverterOptions, $converterOptions); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             /* | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             if ($converterId != 'stack') { | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |                 //unset($converterOptions['converters']); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |                 //unset($converterOptions['converter-options']); | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |                 //$converterOptions['converter-options'] = | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |                 $this->logLn('STACK'); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |                 $this->logLn('converterOptions: ' . print_r($converterOptions, true)); | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             }*/ | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |             $beginTime = microtime(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             $this->ln(); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |             $this->logLn('Trying: '.$converterId, 'italic'); | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |             $converter = ConverterFactory::makeConverter( | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |                 $converterId, | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |                 $this->source, | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |                 $this->destination, | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |                 $converterOptions, | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |                 $this->logger | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |             try { | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |                 $converter->doConvert(); | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |                 //self::runConverterWithTiming($converterId, $source, $destination, $converterOptions, false, $logger); | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |                 $this->logLn($converterId.' succeeded :)'); | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |                 $this->converterUsed = $converterId; | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |                 //throw new ConverterNotOperationalException('...'); | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |                 return; | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |             } catch (ConverterNotOperationalException $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |                 $this->logLn($e->getMessage()); | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |             } catch (ConversionSkippedException $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |                 $this->logLn($e->getMessage()); | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |             } catch (ConversionFailedException $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |                 $this->logLn($e->getMessage(), 'italic'); | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |                 $prev = $e->getPrevious(); | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |                 if (null !== $prev) { | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |                     $this->logLn($prev->getMessage(), 'italic'); | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |                     $this->logLn(' in '.$prev->getFile().', line '.$prev->getLine(), 'italic'); | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |                     $this->ln(); | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |                 //$this->logLn($e->getTraceAsString()); | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |                 $anyRuntimeErrors = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |             $this->logLn($converterId.' failed in '.round((microtime(true) - $beginTime) * 1000).' ms'); | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |         $this->ln(); | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |         $this->logLn('Stack failed in '.round((microtime(true) - $beginTimeStack) * 1000).' ms'); | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |         // Hm, Scrutinizer complains that $anyRuntimeErrors is always false. But that is not true! | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |         if ($anyRuntimeErrors) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |             // At least one converter failed | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |             throw new ConversionFailedException('None of the converters in the stack could convert the image.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |             // All converters threw a SystemRequirementsNotMetException | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |             throw new ConverterNotOperationalException('None of the converters in the stack are operational'); | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     public function getConverterUsed(): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 166 |  |  |         return $this->converterUsed; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 167 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 168 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |  |