| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of the Cecil/Cecil package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) Arnaud Ligny <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Cecil\Converter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Cecil\Assets\Image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Cecil\Builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Cecil\Util; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use ParsedownExtra; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class Parsedown extends ParsedownExtra | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var Builder */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     public function __construct(Builder $builder) | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         parent::__construct(); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $this->builder = $builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected function inlineImage($excerpt) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $image = parent::inlineImage($excerpt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         if (!isset($image)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $path = Util::joinFile( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $this->builder->getConfig()->getStaticTargetPath(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             ltrim($image['element']['attributes']['src']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if (Util::isExternalUrl($image['element']['attributes']['src'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $path = $image['element']['attributes']['src']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $width = $height = null; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         list($width, $height, $type) = getimagesize($this->removeQuery($path)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         // sets default attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $image['element']['attributes']['width'] = $width; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if ($type !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $image['element']['attributes']['loading'] = 'lazy'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         // captures query string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         // ie: "?resize=300&responsive" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $query = parse_url($image['element']['attributes']['src'], PHP_URL_QUERY); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if ($query === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             return $image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         parse_str($query, $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         // cleans URL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $image['element']['attributes']['src'] = $this->removeQuery($image['element']['attributes']['src']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $path = Util::joinFile( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $this->builder->getConfig()->getStaticTargetPath(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ltrim($image['element']['attributes']['src']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if (Util::isExternalUrl($image['element']['attributes']['src'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $path = $image['element']['attributes']['src']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $width = $height = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         list($width, $height, $type) = getimagesize($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |          * Should be responsive? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $responsive = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if (array_key_exists('responsive', $result) && !Util::isExternalUrl($image['element']['attributes']['src'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $responsive = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             // process | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $steps = 5; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $wMin = 320; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $wMax = 2560; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             if ($width < $wMax) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 $wMax = $width; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $srcset = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             for ($i = 1; $i <= $steps; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 $w = (int) ceil($wMin + ($wMax - $wMin) / $steps * $i); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 $img = (new Image($this->builder))->resize($image['element']['attributes']['src'], $w); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 $srcset .= sprintf('%s %sw', $img, $w); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 if ($i < $steps) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                     $srcset .= ', '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             // ie: srcset="/img-480.jpg 480w, img-800.jpg 800w" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $image['element']['attributes']['srcset'] = $srcset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |          * Should be resized? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         if (array_key_exists('resize', $result)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $size = (int) $result['resize']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $width = $size; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $imageResized = (new Image($this->builder)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 ->resize($image['element']['attributes']['src'], $size); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             if (Util::isExternalUrl($image['element']['attributes']['src'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 return $image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             $image['element']['attributes']['src'] = $imageResized; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $path = Util::joinPath( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 $this->builder->getConfig()->getOutputPath(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 ltrim($image['element']['attributes']['src']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             list($width) = getimagesize($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $image['element']['attributes']['width'] = $width; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         // if responsive: set 'sizes' attribute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         if ($responsive) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             // sizes="(max-width: 2800px) 100vw, 2800px" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             $image['element']['attributes']['sizes'] = sprintf('(max-width: %spx) 100vw, %spx', $width, $width); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         // set 'class' attribute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         if (array_key_exists('class', $result)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             $class = $result['class']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             $class = strtr($class, ',', ' '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             $image['element']['attributes']['class'] = $class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         return $image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     private function removeQuery(string $path): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         return strtok($path, '?'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |  |