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 = null) |
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($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'] = strtok($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
|
|
|
$image['element']['attributes']['src'] = (new Image($this->builder)) |
113
|
|
|
->resize($image['element']['attributes']['src'], $size); |
114
|
|
|
|
115
|
|
|
$path = Util::joinPath( |
116
|
|
|
$this->builder->getConfig()->getOutputPath(), |
117
|
|
|
ltrim($image['element']['attributes']['src']) |
118
|
|
|
); |
119
|
|
|
list($width) = getimagesize($path); |
120
|
|
|
$image['element']['attributes']['width'] = $width; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// if responsive: set 'sizes' attribute |
124
|
|
|
if ($responsive) { |
125
|
|
|
// sizes="(max-width: 2800px) 100vw, 2800px" |
126
|
|
|
$image['element']['attributes']['sizes'] = sprintf('(max-width: %spx) 100vw, %spx', $width, $width); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// set 'class' attribute |
130
|
|
|
if (array_key_exists('class', $result)) { |
131
|
|
|
$class = $result['class']; |
132
|
|
|
$class = strtr($class, ',', ' '); |
133
|
|
|
$image['element']['attributes']['class'] = $class; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $image; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|