Completed
Push — feature-responsive-images ( c31264...3dc583 )
by Arnaud
17:53 queued 12:13
created

Parsedown::inlineImage()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 47
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 6
eloc 30
c 3
b 1
f 0
nc 6
nop 1
dl 0
loc 47
rs 8.8177
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)),
0 ignored issues
show
Bug introduced by
ceil($width / 2) of type double is incompatible with the type integer expected by parameter $size of Cecil\Assets\Image::resize(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
                (new Image($this->builder))->resize($image['element']['attributes']['src'], /** @scrutinizer ignore-type */ ceil($width / 2)),
Loading history...
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