|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Service; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\Image; |
|
6
|
|
|
use App\Repository\WanderRepository; |
|
7
|
|
|
use App\Utils\ExifHelper; |
|
8
|
|
|
use Deployer\Logger\Logger; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
|
11
|
|
|
use PHPExif\Adapter\Exiftool; |
|
12
|
|
|
use PHPExif\Reader\Reader; |
|
13
|
|
|
use Psr\Log\LoggerInterface; |
|
14
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
15
|
|
|
use Symfony\Component\Security\Core\Security; |
|
16
|
|
|
use Vich\UploaderBundle\Templating\Helper\UploaderHelper; |
|
17
|
|
|
use App\Utils\ExifHelperInterface; |
|
18
|
|
|
|
|
19
|
|
|
class ImageService { |
|
20
|
|
|
|
|
21
|
|
|
/** @var UploaderHelper */ |
|
22
|
|
|
private $uploaderHelper; |
|
23
|
|
|
|
|
24
|
|
|
/** @var CacheManager */ |
|
25
|
|
|
private $imagineCacheManager; |
|
26
|
|
|
|
|
27
|
|
|
/** @var UrlGeneratorInterface */ |
|
28
|
|
|
private $router; |
|
29
|
|
|
|
|
30
|
|
|
/** @var LoggerInterface */ |
|
31
|
|
|
private $logger; |
|
32
|
|
|
|
|
33
|
|
|
/** @var WanderRepository */ |
|
34
|
|
|
private $wanderRepository; |
|
35
|
|
|
|
|
36
|
|
|
/** @var Reader */ |
|
37
|
|
|
private $reader; |
|
38
|
|
|
|
|
39
|
|
|
/** @var string */ |
|
40
|
|
|
private $imagesDirectory; |
|
41
|
|
|
|
|
42
|
|
|
/** @var string */ |
|
43
|
|
|
private $exiftoolPath; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct( |
|
46
|
|
|
UploaderHelper $uploaderHelper, |
|
47
|
|
|
CacheManager $imagineCacheManager, |
|
48
|
|
|
UrlGeneratorInterface $router, |
|
49
|
|
|
LoggerInterface $logger, |
|
50
|
|
|
WanderRepository $wanderRepository, |
|
51
|
|
|
string $imagesDirectory, |
|
52
|
|
|
?string $exiftoolPath) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->uploaderHelper = $uploaderHelper; |
|
55
|
|
|
$this->imagineCacheManager = $imagineCacheManager; |
|
56
|
|
|
$this->router = $router; |
|
57
|
|
|
$this->logger = $logger; |
|
58
|
|
|
$this->wanderRepository = $wanderRepository; |
|
59
|
|
|
$this->imagesDirectory = $imagesDirectory; |
|
60
|
|
|
$this->exiftoolPath = $exiftoolPath; |
|
61
|
|
|
|
|
62
|
|
|
if ($exiftoolPath !== null) { |
|
63
|
|
|
// Will throw if path is wrong |
|
64
|
|
|
$adapter = new Exiftool([ |
|
65
|
|
|
'toolpath' => $exiftoolPath |
|
66
|
|
|
]); |
|
67
|
|
|
$this->reader = new Reader($adapter); |
|
68
|
|
|
} else { |
|
69
|
|
|
$this->reader = Reader::factory(Reader::TYPE_EXIFTOOL); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function setCalculatedImageUris(Image $image): void |
|
74
|
|
|
{ |
|
75
|
|
|
$image_asset_path = $this->uploaderHelper->asset($image); |
|
76
|
|
|
$image->setImageUri($image_asset_path); |
|
77
|
|
|
$image->setMarkerImageUri($this->imagineCacheManager->getBrowserPath($image_asset_path, 'marker_thumb')); |
|
78
|
|
|
$image->setMediumImageUri($this->imagineCacheManager->getBrowserPath($image_asset_path, 'map_popup_image')); |
|
79
|
|
|
$image->setImageShowUri($this->router->generate('image_show', ['id' => $image->getId()])); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function setPropertiesFromEXIF( |
|
83
|
|
|
Image $image, |
|
84
|
|
|
bool $updateRelatedWanders = true |
|
85
|
|
|
): void |
|
86
|
|
|
{ |
|
87
|
|
|
if ($image->getMimeType() == 'image/jpeg') { |
|
88
|
|
|
try { |
|
89
|
|
|
$exif = $this->reader->read($this->imagesDirectory . '/' . $image->getName()); |
|
90
|
|
|
/** @var ExifHelperInterface */ |
|
91
|
|
|
$exifHelper = new ExifHelper($exif); |
|
92
|
|
|
|
|
93
|
|
|
$image->setTitle($exifHelper->getTitle()); |
|
94
|
|
|
$image->setDescription($exifHelper->getDescription()); |
|
95
|
|
|
$image->setLatlng($exifHelper->getGPS()); |
|
|
|
|
|
|
96
|
|
|
$image->setKeywords($exifHelper->getKeywords()); |
|
97
|
|
|
$image->setRating($exifHelper->getRating()); |
|
98
|
|
|
|
|
99
|
|
|
$capturedAt = $exifHelper->getCreationDate(); |
|
100
|
|
|
$image->setCapturedAt($capturedAt); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
if ($capturedAt instanceof \DateTime && $updateRelatedWanders) { |
|
103
|
|
|
// Try and find associated wander(s) by looking for |
|
104
|
|
|
// wanders whose timespan includes this image. |
|
105
|
|
|
$wanders = $this->wanderRepository->findWhereIncludesDate($capturedAt); |
|
106
|
|
|
foreach ($wanders as $wander) { |
|
107
|
|
|
$image->addWander($wander); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
catch(Exception $e) { |
|
112
|
|
|
// We've started to rely on the information gathered here, so I think |
|
113
|
|
|
// this should be a proper error now. |
|
114
|
|
|
throw new Exception('Error getting image Exif information: ' . $e->getMessage(), $e->getCode(), $e); |
|
115
|
|
|
} |
|
116
|
|
|
} else { |
|
117
|
|
|
$this->logger->info('Ignoring non-JPEG file when trying to set properties from EXIT.'); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.