Completed
Push — dependabot/composer/myclabs/ph... ( 9d5ce4...59e5dd )
by
unknown
01:44
created

Parsedown::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
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\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
    private $config;
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function __construct(Config $config = null)
27
    {
28
        parent::__construct();
29
        $this->config = $config;
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
        $query = parse_url($image['element']['attributes']['src'], PHP_URL_QUERY);
44
45
        // nothing to do
46
        if ($query === null) {
47
            return $image;
48
        }
49
        // URL without query string
50
        $image['element']['attributes']['src'] = strtok($image['element']['attributes']['src'], '?');
51
52
        // has resize value
53
        parse_str($query, $result);
54
        if (array_key_exists('resize', $result)) {
55
            $resize = (int) $result['resize'];
56
            $image['element']['attributes']['width'] = $resize;
57
            // no config or no GD, can't process
58
            if ($this->config === null || !extension_loaded('gd')) {
59
                return $image;
60
            }
61
            // external image? return data URL
62
            if (preg_match('~^(?:f|ht)tps?://~i', $image['element']['attributes']['src'])) {
63
                try {
64
                    $img = Image::make($image['element']['attributes']['src']);
65
                } catch (NotReadableException $e) {
66
                    throw new Exception(sprintf('Cannot get image "%s"', $image['element']['attributes']['src']));
67
                }
68
                $imgPath = (string) $img->encode('data-url');
69
                $image['element']['attributes']['src'] = $imgPath;
70
71
                return $image;
72
            }
73
            // save thumb file
74
            $img = Image::make($this->config->getStaticPath().'/'.$image['element']['attributes']['src']);
75
            $img->resize($resize, null, function ($constraint) {
76
                $constraint->aspectRatio();
77
                $constraint->upsize();
78
            });
79
            $imgThumbPath = '/'.self::TMP_DIR.'/images/thumbs/'.$resize;
80
            $imgPath = $imgThumbPath.$image['element']['attributes']['src'];
81
            $imgSubdir = Util::getFS()->makePathRelative(
82
                dirname($imgPath),
83
                $imgThumbPath
84
            );
85
            Util::getFS()->mkdir($this->config->getDestinationDir().$imgThumbPath.'/'.$imgSubdir);
86
            $img->save($this->config->getDestinationDir().$imgPath);
87
            $imgPath = '/images/thumbs/'.$resize.$image['element']['attributes']['src'];
88
            $image['element']['attributes']['src'] = $imgPath;
89
        }
90
91
        return $image;
92
    }
93
}
94