1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cecil\Converter; |
10
|
|
|
|
11
|
|
|
use Cecil\Assets\Image; |
12
|
|
|
use Cecil\Builder; |
13
|
|
|
use Cecil\Util; |
14
|
|
|
use ParsedownExtra; |
15
|
|
|
|
16
|
|
|
class Parsedown extends ParsedownExtra |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Builder |
20
|
|
|
*/ |
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
|
|
|
// capture query string. ie: "?resize=300&responsive" |
44
|
|
|
$query = parse_url($image['element']['attributes']['src'], PHP_URL_QUERY); |
45
|
|
|
if ($query === null) { |
46
|
|
|
return $image; |
47
|
|
|
} |
48
|
|
|
parse_str($query, $result); |
49
|
|
|
// clean URL |
50
|
|
|
$image['element']['attributes']['src'] = strtok($image['element']['attributes']['src'], '?'); |
51
|
|
|
|
52
|
|
|
// should be responsive? |
53
|
|
|
if (array_key_exists('responsive', $result) && !Util::isExternalUrl($image['element']['attributes']['src'])) { |
54
|
|
|
$path = $this->builder->getConfig()->getStaticPath().'/'.ltrim($image['element']['attributes']['src'], '/'); |
55
|
|
|
list($width) = getimagesize($path); |
56
|
|
|
$image['element']['attributes']['srcset'] = sprintf( |
57
|
|
|
'%s %sw, %s %sw, %s %sw', |
58
|
|
|
(new Image($this->builder))->resize($image['element']['attributes']['src'], ceil($width / 2)), |
|
|
|
|
59
|
|
|
ceil($width / 2), |
60
|
|
|
(new Image($this->builder))->resize($image['element']['attributes']['src'], ceil($width / 1.5)), |
61
|
|
|
ceil($width / 1.5), |
62
|
|
|
(new Image($this->builder))->resize($image['element']['attributes']['src'], $width), |
63
|
|
|
ceil($width) |
64
|
|
|
); |
65
|
|
|
$image['element']['attributes']['sizes'] = sprintf( |
66
|
|
|
'(max-width: %spx) %spx, %spx', |
67
|
|
|
ceil($width / 1.5), |
68
|
|
|
ceil($width / 2), |
69
|
|
|
ceil($width) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// has resize value? |
74
|
|
|
if (array_key_exists('resize', $result)) { |
75
|
|
|
$size = (int) $result['resize']; |
76
|
|
|
$image['element']['attributes']['width'] = $size; |
77
|
|
|
$image['element']['attributes']['src'] = (new Image($this->builder)) |
78
|
|
|
->resize($image['element']['attributes']['src'], $size); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $image; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|