|
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\Config; |
|
12
|
|
|
use Cecil\Exception\Exception; |
|
13
|
|
|
use Cecil\Util; |
|
14
|
|
|
use Intervention\Image\Exception\NotReadableException; |
|
15
|
|
|
use Intervention\Image\ImageManagerStatic as Image; |
|
16
|
|
|
use ParsedownExtra; |
|
17
|
|
|
|
|
18
|
|
|
class Parsedown extends ParsedownExtra |
|
19
|
|
|
{ |
|
20
|
|
|
const TMP_DIR = '.cecil'; |
|
21
|
|
|
const PATTERN = '(.*)(\?|\&)([^=]+)\=([^&]+)'; // https://regex101.com/r/EhIh5N/2 |
|
22
|
|
|
private $config; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(Config $config = null) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct(); |
|
30
|
|
|
$this->config = $config; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function inlineImage($excerpt) |
|
37
|
|
|
{ |
|
38
|
|
|
$image = parent::inlineImage($excerpt); |
|
39
|
|
|
|
|
40
|
|
|
if (!isset($image)) { |
|
41
|
|
|
return null; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
preg_match('/'.self::PATTERN.'/s', $image['element']['attributes']['src'], $matches); |
|
45
|
|
|
// nothing to do |
|
46
|
|
|
if (empty($matches)) { |
|
47
|
|
|
return $image; |
|
48
|
|
|
} |
|
49
|
|
|
$image['element']['attributes']['src'] = $matches[1]; // URL without query string |
|
50
|
|
|
|
|
51
|
|
|
// no config or no GD, can't process |
|
52
|
|
|
if ($this->config === null || !extension_loaded('gd')) { |
|
53
|
|
|
return $image; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// has resize value |
|
57
|
|
|
if (array_key_exists(3, $matches) && $matches[3] == 'resize') { |
|
58
|
|
|
$resize = (int) $matches[4]; |
|
59
|
|
|
$image['element']['attributes']['width'] = $resize; |
|
60
|
|
|
// external image? return data URL |
|
61
|
|
|
if (preg_match('~^(?:f|ht)tps?://~i', $image['element']['attributes']['src'])) { |
|
62
|
|
|
try { |
|
63
|
|
|
$img = Image::make($image['element']['attributes']['src']); |
|
64
|
|
|
} catch (NotReadableException $e) { |
|
65
|
|
|
throw new Exception(sprintf('Cannot get image "%s"', $image['element']['attributes']['src'])); |
|
66
|
|
|
} |
|
67
|
|
|
$imgPath = (string) $img->encode('data-url'); |
|
68
|
|
|
$image['element']['attributes']['src'] = $imgPath; |
|
69
|
|
|
|
|
70
|
|
|
return $image; |
|
71
|
|
|
} |
|
72
|
|
|
// save thumb file |
|
73
|
|
|
$img = Image::make($this->config->getStaticPath().'/'.$image['element']['attributes']['src']); |
|
74
|
|
|
$img->resize($resize, null, function ($constraint) { |
|
75
|
|
|
$constraint->aspectRatio(); |
|
76
|
|
|
$constraint->upsize(); |
|
77
|
|
|
}); |
|
78
|
|
|
$imgThumbPath = '/'.self::TMP_DIR.'/images/thumbs/'.$resize; |
|
79
|
|
|
$imgPath = $imgThumbPath.$image['element']['attributes']['src']; |
|
80
|
|
|
$imgSubdir = Util::getFS()->makePathRelative( |
|
81
|
|
|
dirname($imgPath), |
|
82
|
|
|
$imgThumbPath |
|
83
|
|
|
); |
|
84
|
|
|
Util::getFS()->mkdir($this->config->getDestinationDir().$imgThumbPath.'/'.$imgSubdir); |
|
85
|
|
|
$img->save($this->config->getDestinationDir().$imgPath); |
|
86
|
|
|
$imgPath = '/images/thumbs/'.$resize.$image['element']['attributes']['src']; |
|
87
|
|
|
$image['element']['attributes']['src'] = $imgPath; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $image; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|