1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentBundle\Factory\File; |
15
|
|
|
|
16
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
17
|
|
|
use Liip\ImagineBundle\Service\FilterService; |
18
|
|
|
use Silverback\ApiComponentBundle\Dto\File\ImageMetadata; |
19
|
|
|
use Silverback\ApiComponentBundle\Dto\File\ImagineMetadata; |
20
|
|
|
use Silverback\ApiComponentBundle\Entity\Utility\FileInterface; |
21
|
|
|
use Silverback\ApiComponentBundle\Imagine\PathResolver; |
22
|
|
|
use Symfony\Component\HttpFoundation\UrlHelper; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Daniel West <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ImagineMetadataFactory |
28
|
|
|
{ |
29
|
|
|
private CacheManager $cacheManager; |
30
|
|
|
private PathResolver $pathResolver; |
31
|
|
|
private string $projectDirectory; |
32
|
|
|
private FilterService $filterService; |
33
|
|
|
private UrlHelper $urlHelper; |
34
|
|
|
|
35
|
|
|
public function __construct(CacheManager $cacheManager, PathResolver $pathResolver, string $projectDirectory, FilterService $filterService, UrlHelper $urlHelper) |
36
|
|
|
{ |
37
|
|
|
$this->cacheManager = $cacheManager; |
38
|
|
|
$this->pathResolver = $pathResolver; |
39
|
|
|
$this->projectDirectory = $projectDirectory; |
40
|
|
|
$this->filterService = $filterService; |
41
|
|
|
$this->urlHelper = $urlHelper; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function isImagineFilePath(?string $filePath): bool |
45
|
|
|
{ |
46
|
|
|
if (!$filePath || !file_exists($filePath)) { |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
try { |
50
|
|
|
$imageType = exif_imagetype($filePath); |
51
|
|
|
} catch (\Exception $e) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return \in_array($imageType, [IMAGETYPE_JPEG, IMAGETYPE_JPEG2000, IMAGETYPE_PNG, IMAGETYPE_GIF], true); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function create(FileInterface $file): ?ImagineMetadata |
59
|
|
|
{ |
60
|
|
|
$filePath = $file->getFilePath(); |
61
|
|
|
if (!class_exists(CacheManager::class) || !self::isImagineFilePath($filePath)) { |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
$imagineMetadata = new ImagineMetadata(); |
65
|
|
|
foreach ($file::getImagineFilters() as $returnKey => $filter) { |
66
|
|
|
$resolvedPath = $this->pathResolver->resolve($filePath); |
67
|
|
|
|
68
|
|
|
// Getting the URL will create the filtered image if it does not exist |
69
|
|
|
$this->filterService->getUrlOfFilteredImage($resolvedPath, $filter); |
70
|
|
|
|
71
|
|
|
$imagineBrowserPath = $this->cacheManager->getBrowserPath($resolvedPath, $filter); |
72
|
|
|
$imagineFilePath = urldecode(ltrim( |
73
|
|
|
parse_url( |
74
|
|
|
$imagineBrowserPath, |
75
|
|
|
PHP_URL_PATH |
76
|
|
|
), |
77
|
|
|
'/' |
78
|
|
|
)); |
79
|
|
|
$realPath = sprintf('%s/public/%s', $this->projectDirectory, $imagineFilePath); |
80
|
|
|
|
81
|
|
|
$imagineMetadata->addFilter($returnKey, new ImageMetadata($realPath, $this->urlHelper->getAbsoluteUrl($imagineFilePath))); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $imagineMetadata; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|