1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\MediaBundle\Helper\Image; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\MediaBundle\Entity\Media; |
6
|
|
|
use Kunstmaan\MediaBundle\Helper\ExtensionGuesserFactoryInterface; |
7
|
|
|
use Kunstmaan\MediaBundle\Helper\File\FileHandler; |
8
|
|
|
use Kunstmaan\MediaBundle\Helper\MimeTypeGuesserFactoryInterface; |
9
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* FileHandler |
13
|
|
|
*/ |
14
|
|
|
class ImageHandler extends FileHandler |
15
|
|
|
{ |
16
|
|
|
protected $aviaryApiKey; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param int $priority |
20
|
|
|
* @param MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory |
21
|
|
|
* @param ExtensionGuesserFactoryInterface $extensionGuesserFactoryInterface |
22
|
|
|
* @param string $aviaryApiKey The aviary key |
23
|
|
|
*/ |
24
|
1 |
|
public function __construct($priority, MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory, ExtensionGuesserFactoryInterface $extensionGuesserFactoryInterface, $aviaryApiKey) |
25
|
|
|
{ |
26
|
1 |
|
parent::__construct($priority, $mimeTypeGuesserFactory, $extensionGuesserFactoryInterface); |
27
|
1 |
|
$this->aviaryApiKey = $aviaryApiKey; |
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function getAviaryApiKey() |
34
|
|
|
{ |
35
|
|
|
return $this->aviaryApiKey; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function getName() |
42
|
|
|
{ |
43
|
|
|
return 'Image Handler'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getType() |
50
|
|
|
{ |
51
|
|
|
return 'image'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param mixed $object |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function canHandle($object) |
60
|
|
|
{ |
61
|
|
|
if (parent::canHandle($object) && ($object instanceof File || strncmp($object->getContentType(), 'image', 5) === 0)) { |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function getShowTemplate(Media $media) |
72
|
|
|
{ |
73
|
|
|
return '@KunstmaanMedia/Media/Image/show.html.twig'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Media $media The media entity |
78
|
|
|
* @param string $basepath The base path |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getImageUrl(Media $media, $basepath) |
83
|
|
|
{ |
84
|
|
|
return $basepath . $media->getUrl(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param Media $media |
89
|
|
|
*/ |
90
|
1 |
|
public function prepareMedia(Media $media) |
91
|
|
|
{ |
92
|
1 |
|
parent::prepareMedia($media); |
93
|
|
|
|
94
|
1 |
|
if ($media->getContent()) { |
95
|
1 |
|
$imageInfo = getimagesize($media->getContent()); |
96
|
|
|
|
97
|
1 |
|
$width = $height = null; |
98
|
1 |
|
if (false !== $imageInfo) { |
99
|
|
|
list($width, $height) = $imageInfo; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$media |
103
|
1 |
|
->setMetadataValue('original_width', $width) |
104
|
1 |
|
->setMetadataValue('original_height', $height); |
105
|
|
|
} |
106
|
1 |
|
} |
107
|
|
|
} |
108
|
|
|
|