Completed
Push — master ( f8ff86...ec22b1 )
by Henry
03:57
created

PDF   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A analyzeFile() 0 13 2
A getImage() 0 10 3
A __classLoaded() 0 8 1
A getValue() 0 22 6
1
<?php
2
namespace Divergence\Models\Media;
3
4
use Exception;
5
6
class PDF extends Media
7
{
8
    // configurables
9
    public static $extractPageCommand = 'convert \'%1$s[%2$u]\' JPEG:- 2>/dev/null'; // 1=pdf path, 2=page
10
    public static $extractPageIndex = 0;
11
12
13
    // magic methods
14
    public static function __classLoaded()
15
    {
16
        $className = get_called_class();
17
18
        Media::$mimeHandlers['application/pdf'] = $className;
19
        Media::$mimeHandlers['application/postscript'] = $className;
20
        Media::$mimeHandlers['image/svg+xml'] = $className;
21
        parent::__classLoaded();
0 ignored issues
show
introduced by
The method __classLoaded() does not exist on Divergence\Models\Media\Media. Maybe you want to declare this class abstract? ( Ignorable by Annotation )

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

21
        parent::/** @scrutinizer ignore-call */ 
22
                __classLoaded();
Loading history...
22
    }
23
24
25
    public function getValue($name)
26
    {
27
        switch ($name) {
28
            case 'ThumbnailMIMEType':
29
                return 'image/png';
30
31
            case 'Extension':
32
33
                switch ($this->MIMEType) {
34
                    case 'application/pdf':
35
                        return 'pdf';
36
                    case 'application/postscript':
37
                        return 'eps';
38
                    case 'image/svg+xml':
39
                        return 'svg';
40
                    default:
41
                        throw new Exception('Unable to find document extension for mime-type: '.$this->MIMEType);
42
                }
43
44
                // no break
45
            default:
46
                return parent::getValue($name);
47
        }
48
    }
49
50
51
    // public methods
52
    public function getImage($sourceFile = null)
53
    {
54
        if (!isset($sourceFile)) {
55
            $sourceFile = $this->FilesystemPath ? $this->FilesystemPath : $this->BlankPath;
0 ignored issues
show
Bug Best Practice introduced by
The property BlankPath does not exist on Divergence\Models\Media\PDF. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property FilesystemPath does not exist on Divergence\Models\Media\PDF. Since you implemented __get, consider adding a @property annotation.
Loading history...
56
        }
57
58
        $cmd = sprintf(static::$extractPageCommand, $sourceFile, static::$extractPageIndex);
59
        $fileImage = imagecreatefromstring(shell_exec($cmd));
60
61
        return $fileImage;
62
    }
63
64
    // static methods
65
    public static function analyzeFile($filename, $mediaInfo = [])
66
    {
67
        $cmd = sprintf(static::$extractPageCommand, $filename, static::$extractPageIndex);
68
        $pageIm = @imagecreatefromstring(shell_exec($cmd));
69
70
        if (!$pageIm) {
0 ignored issues
show
introduced by
$pageIm is of type resource, thus it always evaluated to false.
Loading history...
71
            throw new Exception('Unable to convert PDF, ensure that imagemagick is installed on the server');
72
        }
73
74
        $mediaInfo['width'] = imagesx($pageIm);
75
        $mediaInfo['height'] = imagesy($pageIm);
76
77
        return $mediaInfo;
78
    }
79
}
80