1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2020 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2020 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\MessageHandler; |
18
|
|
|
|
19
|
|
|
use BadFunctionCallException; |
20
|
|
|
use DateTime; |
21
|
|
|
use SWP\Bundle\CoreBundle\MessageHandler\Message\ConvertImageMessage; |
22
|
|
|
use SWP\Bundle\CoreBundle\Model\Image; |
23
|
|
|
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
24
|
|
|
use function imagewebp; |
25
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
26
|
|
|
use Exception; |
27
|
|
|
use InvalidArgumentException; |
28
|
|
|
use JMS\Serializer\SerializerInterface; |
29
|
|
|
use Psr\Log\LoggerInterface; |
30
|
|
|
use SWP\Bundle\ContentBundle\Manager\MediaManagerInterface; |
31
|
|
|
use SWP\Bundle\ContentBundle\Model\FileInterface; |
32
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageRenditionInterface; |
33
|
|
|
use SWP\Bundle\CoreBundle\Model\ImageInterface; |
34
|
|
|
use SWP\Bundle\CoreBundle\Model\Tenant; |
35
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
36
|
|
|
use SWP\Component\Storage\Repository\RepositoryInterface; |
37
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
38
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
39
|
|
|
|
40
|
|
|
class ImageConversionHandler implements MessageHandlerInterface |
41
|
|
|
{ |
42
|
|
|
protected $serializer; |
43
|
|
|
|
44
|
|
|
protected $logger; |
45
|
|
|
|
46
|
|
|
protected $imageRenditionRepository; |
47
|
|
|
|
48
|
|
|
protected $mediaManager; |
49
|
|
|
|
50
|
|
|
protected $tenantContext; |
51
|
|
|
|
52
|
|
|
protected $entityManager; |
53
|
|
|
|
54
|
|
|
public function __construct( |
55
|
|
|
SerializerInterface $serializer, |
56
|
|
|
LoggerInterface $logger, |
57
|
|
|
RepositoryInterface $imageRenditionRepository, |
58
|
|
|
MediaManagerInterface $mediaManager, |
59
|
|
|
TenantContextInterface $tenantContext, |
60
|
|
|
EntityManagerInterface $entityManager |
61
|
|
|
) { |
62
|
|
|
$this->serializer = $serializer; |
63
|
|
|
$this->logger = $logger; |
64
|
|
|
$this->imageRenditionRepository = $imageRenditionRepository; |
65
|
|
|
$this->mediaManager = $mediaManager; |
66
|
|
|
$this->tenantContext = $tenantContext; |
67
|
|
|
$this->entityManager = $entityManager; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function __invoke(ConvertImageMessage $message) |
71
|
|
|
{ |
72
|
|
|
$tenant = $this->entityManager->find(Tenant::class, $message->getTenantId()); |
73
|
|
|
if (null === $tenant) { |
74
|
|
|
throw new InvalidArgumentException('Tenant not found'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->tenantContext->setTenant($tenant); |
78
|
|
|
|
79
|
|
|
$image = $this->entityManager->find(Image::class, $message->getImageId()); |
80
|
|
|
if (null === $image) { |
81
|
|
|
throw new InvalidArgumentException('Missing image data'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** @var ImageInterface $image */ |
85
|
|
|
$image = $this->entityManager->merge($image); |
|
|
|
|
86
|
|
|
$mediaId = $image->getAssetId(); |
87
|
|
|
$tempLocation = rtrim(sys_get_temp_dir(), '/').DIRECTORY_SEPARATOR.sha1($mediaId); |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
if (!function_exists('imagewebp')) { |
91
|
|
|
throw new BadFunctionCallException('"imagewebp" function is missing. Looks like GD was compiled without webp support'); |
92
|
|
|
} |
93
|
|
|
imagewebp($this->getImageAsResource($image), $tempLocation); |
94
|
|
|
$uploadedFile = new UploadedFile($tempLocation, $mediaId, 'image/webp', strlen($tempLocation), null, true); |
|
|
|
|
95
|
|
|
$this->mediaManager->saveFile($uploadedFile, $mediaId); |
96
|
|
|
|
97
|
|
|
$this->logger->info(sprintf('File "%s" converted successfully to WEBP', $mediaId)); |
98
|
|
|
|
99
|
|
|
$image->addVariant(ImageInterface::VARIANT_WEBP); |
100
|
|
|
$this->markArticlesMediaAsUpdated($image); |
101
|
|
|
|
102
|
|
|
$this->entityManager->flush(); |
103
|
|
|
} catch (Exception $e) { |
104
|
|
|
$this->logger->error('File NOT converted '.$e->getMessage(), ['exception' => $e->getTraceAsString()]); |
105
|
|
|
|
106
|
|
|
return; |
107
|
|
|
} finally { |
108
|
|
|
$filesystem = new Filesystem(); |
109
|
|
|
if ($filesystem->exists($tempLocation)) { |
110
|
|
|
$filesystem->remove($tempLocation); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function markArticlesMediaAsUpdated($image) |
116
|
|
|
{ |
117
|
|
|
/** @var ImageRenditionInterface[] $articleMedia */ |
118
|
|
|
$articleMedia = $this->imageRenditionRepository->findBy(['image' => $image]); |
119
|
|
|
foreach ($articleMedia as $media) { |
120
|
|
|
$media->getMedia()->getArticle()->setMediaUpdatedAt(new DateTime()); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function getImageAsResource(FileInterface $asset) |
125
|
|
|
{ |
126
|
|
|
$filesystem = new Filesystem(); |
127
|
|
|
$tempLocation = rtrim(sys_get_temp_dir(), '/').DIRECTORY_SEPARATOR.sha1($asset->getAssetId()); |
128
|
|
|
$filesystem->dumpFile($tempLocation, $this->mediaManager->getFile($asset)); |
129
|
|
|
|
130
|
|
|
$resource = null; |
131
|
|
|
$size = getimagesize($tempLocation); |
132
|
|
|
switch ($size['mime']) { |
133
|
|
|
case 'image/jpeg': |
134
|
|
|
$resource = imagecreatefromjpeg($tempLocation); //jpeg file |
135
|
|
|
break; |
136
|
|
|
case 'image/gif': |
137
|
|
|
$resource = imagecreatefromgif($tempLocation); //gif file |
138
|
|
|
break; |
139
|
|
|
case 'image/png': |
140
|
|
|
$resource = imagecreatefrompng($tempLocation); //png file |
141
|
|
|
break; |
142
|
|
|
} |
143
|
|
|
$filesystem->remove($tempLocation); |
144
|
|
|
|
145
|
|
|
return $resource; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.