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(); |
|
|
|
|
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; |
|
|
|
|
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) { |
|
|
|
|
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
|
|
|
|