PDF::getValue()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 22
ccs 0
cts 13
cp 0
crap 42
rs 9.2222
1
<?php
2
/**
3
 * This file is part of the Divergence package.
4
 *
5
 * (c) Henry Paradiz <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Divergence\Models\Media;
12
13
use Exception;
14
15
/**
16
 * PDF Media Model
17
 *
18
 * @author Henry Paradiz <[email protected]>
19
 * @author Chris Alfano <[email protected]>
20
 *
21
 * {@inheritDoc}
22
 */
23
class PDF extends Media
0 ignored issues
show
Bug introduced by
The type Divergence\Models\Media\Media was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
{
25
    // configurables
26
    public static $extractPageCommand = 'convert \'%1$s[%2$u]\' JPEG:- 2>/dev/null'; // 1=pdf path, 2=page
27
    public static $extractPageIndex = 0;
28
29
    public function getValue($name)
30
    {
31
        switch ($name) {
32
            case 'ThumbnailMIMEType':
33
                return 'image/png';
34
35
            case 'Extension':
36
37
                switch ($this->getValue('MIMEType')) {
38
                    case 'application/pdf':
39
                        return 'pdf';
40
                    case 'application/postscript':
41
                        return 'eps';
42
                    case 'image/svg+xml':
43
                        return 'svg';
44
                    default:
45
                        throw new Exception('Unable to find document extension for mime-type: '.$this->getValue('MIMEType'));
46
                }
47
48
                // no break
49
            default:
50
                return parent::getValue($name);
51
        }
52
    }
53
54
55
    // public methods
56
    public function getImage($sourceFile = null): false|\GdImage
57
    {
58
        if (!isset($sourceFile)) {
59
            $sourceFile = $this->FilesystemPath ? $this->FilesystemPath : $this->BlankPath;
60
        }
61
62
        $cmd = sprintf(static::$extractPageCommand, $sourceFile, static::$extractPageIndex);
63
        $fileImage = imagecreatefromstring(shell_exec($cmd));
64
65
        return $fileImage;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $fileImage could return the type resource which is incompatible with the type-hinted return GdImage|false. Consider adding an additional type-check to rule them out.
Loading history...
66
    }
67
68
    // static methods
69
    public static function analyzeFile($filename, $mediaInfo = [])
70
    {
71
        $cmd = sprintf(static::$extractPageCommand, $filename, static::$extractPageIndex);
72
        $pageIm = @imagecreatefromstring(shell_exec($cmd));
73
74
        if (!$pageIm) {
75
            throw new Exception('Unable to convert PDF, ensure that imagemagick is installed on the server');
76
        }
77
78
        $mediaInfo['width'] = imagesx($pageIm);
79
        $mediaInfo['height'] = imagesy($pageIm);
80
81
        return $mediaInfo;
82
    }
83
}
84