Completed
Push — master ( f4ebcb...ba1f71 )
by Henry
02:12
created

PDF::__classLoaded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the Divergence package.
4
 *
5
 * @author Henry Paradiz <[email protected]>
6
 * @copyright 2018 Henry Paradiz <[email protected]>
7
 * @license MIT For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
8
 *
9
 * @since 1.1
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
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->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->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)
57
    {
58
        if (!isset($sourceFile)) {
59
            $sourceFile = $this->FilesystemPath ? $this->FilesystemPath : $this->BlankPath;
0 ignored issues
show
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...
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...
60
        }
61
62
        $cmd = sprintf(static::$extractPageCommand, $sourceFile, static::$extractPageIndex);
63
        $fileImage = imagecreatefromstring(shell_exec($cmd));
64
65
        return $fileImage;
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) {
0 ignored issues
show
introduced by
$pageIm is of type resource, thus it always evaluated to false.
Loading history...
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