Completed
Push — feature-assets ( f88b70...496ec5 )
by Arnaud
02:09
created

Parsedown::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Util;
13
use Intervention\Image\ImageManagerStatic as Image;
14
use ParsedownExtra;
15
16
class Parsedown extends ParsedownExtra
17
{
18
    const TMP_DIR = '.cecil';
19
    const PATTERN = '(.*)(\?|\&)([^=]+)\=([^&]+)'; // https://regex101.com/r/EhIh5N/2
20
    private $config;
21
22
    public function __construct(Config $config = null)
23
    {
24
        $this->config = $config;
25
    }
26
27
    protected function inlineImage($excerpt)
28
    {
29
        $save = true;
30
        $external = false;
31
32
        $image = parent::inlineImage($excerpt);
33
34
        if (!isset($image)) {
35
            return null;
36
        }
37
38
        if (preg_match('~^(?:f|ht)tps?://~i', $image['element']['attributes']['src'])) {
39
            $external = true;
40
        }
41
42
        preg_match('/'.self::PATTERN.'/s', $image['element']['attributes']['src'], $matches);
43
        if (empty($matches)) {
44
            return $image;
45
        }
46
        $image['element']['attributes']['src'] = $matches[1];
47
48
        if ($this->config === null) {
49
            return $image;
50
        }
51
52
        if (array_key_exists(3, $matches) && $matches[3] == 'resize') {
53
            $resize = $matches[4];
54
55
            $image['element']['attributes']['width'] = $resize;
56
57
            if (extension_loaded('gd')) {
58
                if ($external) {
59
                    $img = Image::make($image['element']['attributes']['src']);
60
                } else {
61
                    $img = Image::make($this->config->getStaticPath().'/'.$image['element']['attributes']['src']);
62
                }
63
                $img->resize($resize, null, function ($constraint) {
64
                        $constraint->aspectRatio();
65
                        $constraint->upsize();
66
                    });
67
                if ($save) {
68
                    if ($external) {
69
                        $imgPath = (string) $img->encode('data-url');
70
                    } else {
71
                        $imgPath = '/'.self::TMP_DIR.'/images/thumbs/'.$resize.$image['element']['attributes']['src'];
72
                        $dir = Util::getFS()->makePathRelative(
73
                            dirname($imgPath),
74
                            '/'.self::TMP_DIR.'/images/thumbs/'.$resize
75
                        );
76
                        Util::getFS()->mkdir($this->config->getDestinationDir().'/'.self::TMP_DIR.'/images/thumbs/'.$resize.'/'.$dir);
77
                        $img->save($this->config->getDestinationDir().$imgPath);
78
                        $imgPath = '/images/thumbs/'.$resize.$image['element']['attributes']['src'];
79
                    }
80
                    $image['element']['attributes']['src'] = $imgPath;
81
                }
82
            }
83
        }
84
85
        return $image;
86
    }
87
}
88