|
1
|
|
|
<?php |
|
2
|
|
|
namespace nstdio\svg\output; |
|
3
|
|
|
|
|
4
|
|
|
use Hoa\Mime\Mime; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class ImageOutput |
|
8
|
|
|
* |
|
9
|
|
|
* @package nstdio\svg\output |
|
10
|
|
|
* @author Edgar Asatryan <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class ImageOutput implements ImageOutputInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @inheritdoc |
|
16
|
|
|
*/ |
|
17
|
|
|
public function imageFile($content, $name, $format, $override) |
|
18
|
|
|
{ |
|
19
|
|
|
$imagick = $this->createImagick($content, $format); |
|
20
|
|
|
|
|
21
|
|
|
$fileWriter = new FileOutput(); |
|
22
|
|
|
|
|
23
|
|
|
return $fileWriter->file($name, $imagick->getImageBlob(), $override); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param $content |
|
28
|
|
|
* @param $format |
|
29
|
|
|
* |
|
30
|
|
|
* @return \Imagick |
|
31
|
|
|
*/ |
|
32
|
|
|
private function createImagick($content, $format) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->checkExtensionLoaded(); |
|
35
|
|
|
|
|
36
|
|
|
$imagick = new \Imagick(); |
|
37
|
|
|
|
|
38
|
|
|
$imagick->readImageBlob($content); |
|
39
|
|
|
try { |
|
40
|
|
|
$imagick->setImageFormat($format); |
|
41
|
|
|
} catch (\ImagickException $e) { |
|
42
|
|
|
throw new \InvalidArgumentException(sprintf("Invalid image format %s.", $format), 0, $e); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $imagick; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function checkExtensionLoaded() |
|
49
|
|
|
{ |
|
50
|
|
|
if (!extension_loaded('imagick')) { |
|
51
|
|
|
throw new \RuntimeException('Imagick extension is not loaded. If you access to php.ini try uncomment or add extension=php_imagick.so.'); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritdoc |
|
57
|
|
|
*/ |
|
58
|
|
|
public function image($content, $format, $sendHeader) |
|
59
|
|
|
{ |
|
60
|
|
|
$imagick = $this->createImagick($content, $format); |
|
61
|
|
|
|
|
62
|
|
|
if ($sendHeader) { |
|
63
|
|
|
$this->trySendHeader($format); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $imagick; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function trySendHeader($format) |
|
70
|
|
|
{ |
|
71
|
|
|
if (!headers_sent()) { |
|
72
|
|
|
header($this->getContentType($format)); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function getContentType($format) |
|
77
|
|
|
{ |
|
78
|
|
|
return sprintf("Content-Type: %s", Mime::getMimeFromExtension($format)); |
|
79
|
|
|
} |
|
80
|
|
|
} |