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 string $aviaryApiKey The aviary key |
21
|
|
|
*/ |
22
|
1 |
|
public function __construct($priority, MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory, ExtensionGuesserFactoryInterface $extensionGuesserFactoryInterface, $aviaryApiKey) |
23
|
|
|
{ |
24
|
1 |
|
parent::__construct($priority, $mimeTypeGuesserFactory, $extensionGuesserFactoryInterface); |
25
|
1 |
|
$this->aviaryApiKey = $aviaryApiKey; |
26
|
1 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function getAviaryApiKey() |
32
|
|
|
{ |
33
|
|
|
return $this->aviaryApiKey; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public function getName() |
40
|
|
|
{ |
41
|
|
|
return 'Image Handler'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function getType() |
48
|
|
|
{ |
49
|
|
|
return 'image'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param mixed $object |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function canHandle($object) |
58
|
|
|
{ |
59
|
|
|
if (parent::canHandle($object) && ($object instanceof File || strncmp($object->getContentType(), 'image', 5) === 0)) { |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function getShowTemplate(Media $media) |
70
|
|
|
{ |
71
|
|
|
return '@KunstmaanMedia/Media/Image/show.html.twig'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Media $media The media entity |
76
|
|
|
* @param string $basepath The base path |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function getImageUrl(Media $media, $basepath) |
81
|
|
|
{ |
82
|
|
|
return $basepath . $media->getUrl(); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function prepareMedia(Media $media) |
86
|
|
|
{ |
87
|
1 |
|
parent::prepareMedia($media); |
88
|
|
|
|
89
|
1 |
|
if ($media->getContent()) { |
90
|
1 |
|
$imageInfo = getimagesize($media->getContent()); |
91
|
|
|
|
92
|
1 |
|
$width = $height = null; |
93
|
1 |
|
if (false !== $imageInfo) { |
94
|
|
|
list($width, $height) = $imageInfo; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$media |
98
|
1 |
|
->setMetadataValue('original_width', $width) |
99
|
1 |
|
->setMetadataValue('original_height', $height); |
100
|
|
|
} |
101
|
1 |
|
} |
102
|
|
|
} |
103
|
|
|
|