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 2017 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 2017 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Consumer; |
18
|
|
|
|
19
|
|
|
use BadFunctionCallException; |
20
|
|
|
use function imagewebp; |
21
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
22
|
|
|
use Exception; |
23
|
|
|
use JMS\Serializer\Exception\RuntimeException; |
24
|
|
|
use JMS\Serializer\SerializerInterface; |
25
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface; |
26
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
27
|
|
|
use Psr\Log\LoggerInterface; |
28
|
|
|
use SWP\Bundle\ContentBundle\Manager\MediaManagerInterface; |
29
|
|
|
use SWP\Bundle\ContentBundle\Model\FileInterface; |
30
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageRendition; |
31
|
|
|
use SWP\Bundle\CoreBundle\Model\ImageInterface; |
32
|
|
|
use SWP\Bundle\CoreBundle\Model\Tenant; |
33
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
34
|
|
|
use SWP\Component\MultiTenancy\Model\TenantInterface; |
35
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
36
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
37
|
|
|
|
38
|
|
|
class ImageConversionConsumer implements ConsumerInterface |
39
|
|
|
{ |
40
|
|
|
protected $serializer; |
41
|
|
|
|
42
|
|
|
protected $logger; |
43
|
|
|
|
44
|
|
|
protected $mediaManager; |
45
|
|
|
|
46
|
|
|
protected $tenantContext; |
47
|
|
|
|
48
|
|
|
protected $entityManager; |
49
|
|
|
|
50
|
|
|
public function __construct( |
51
|
|
|
SerializerInterface $serializer, |
52
|
|
|
LoggerInterface $logger, |
53
|
|
|
MediaManagerInterface $mediaManager, |
54
|
|
|
TenantContextInterface $tenantContext, |
55
|
|
|
EntityManagerInterface $entityManager |
56
|
|
|
) { |
57
|
|
|
$this->serializer = $serializer; |
58
|
|
|
$this->logger = $logger; |
59
|
|
|
$this->mediaManager = $mediaManager; |
60
|
|
|
$this->tenantContext = $tenantContext; |
61
|
|
|
$this->entityManager = $entityManager; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function execute(AMQPMessage $message): int |
65
|
|
|
{ |
66
|
|
|
try { |
67
|
|
|
['renditionId' => $imageRenditionId, 'tenantId' => $tenantId] = unserialize($message->body, [false]); |
|
|
|
|
68
|
|
|
if (($tenant = $this->entityManager->find(Tenant::class, $tenantId)) instanceof TenantInterface) { |
69
|
|
|
$this->tenantContext->setTenant($tenant); |
70
|
|
|
} |
71
|
|
|
} catch (RuntimeException $e) { |
72
|
|
|
$this->logger->error('Message REJECTED: '.$e->getMessage(), ['exception' => $e->getTraceAsString()]); |
73
|
|
|
|
74
|
|
|
return ConsumerInterface::MSG_REJECT; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$imageRendition = $this->entityManager->find(ImageRendition::class, $imageRenditionId); |
78
|
|
|
if (null !== $imageRendition) { |
79
|
|
|
$mediaId = $imageRendition->getImage()->getAssetId(); |
80
|
|
|
$tempLocation = rtrim(sys_get_temp_dir(), '/').DIRECTORY_SEPARATOR.sha1($mediaId); |
81
|
|
|
|
82
|
|
|
try { |
83
|
|
|
if (!function_exists('imagewebp')) { |
84
|
|
|
throw new BadFunctionCallException('"imagewebp" function is missing. Looks like GD was compiled without webp support'); |
85
|
|
|
} |
86
|
|
|
imagewebp($this->getImageAsResource($imageRendition->getImage()), $tempLocation); |
87
|
|
|
$uploadedFile = new UploadedFile($tempLocation, $mediaId, 'image/webp', strlen($tempLocation), null, true); |
88
|
|
|
$this->mediaManager->saveFile($uploadedFile, $mediaId); |
89
|
|
|
|
90
|
|
|
$this->logger->info(sprintf('File "%s" converted successfully to WEBP', $mediaId)); |
91
|
|
|
|
92
|
|
|
$imageRendition->getImage()->addVariant(ImageInterface::VARIANT_WEBP); |
93
|
|
|
$this->entityManager->flush(); |
94
|
|
|
} catch (Exception $e) { |
95
|
|
|
$this->logger->error('File NOT converted '.$e->getMessage(), ['exception' => $e->getTraceAsString()]); |
96
|
|
|
|
97
|
|
|
return ConsumerInterface::MSG_REJECT; |
98
|
|
|
} finally { |
99
|
|
|
$filesystem = new Filesystem(); |
100
|
|
|
if ($filesystem->exists($tempLocation)) { |
101
|
|
|
$filesystem->remove($tempLocation); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return ConsumerInterface::MSG_ACK; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function getImageAsResource(FileInterface $asset) |
110
|
|
|
{ |
111
|
|
|
$filesystem = new Filesystem(); |
112
|
|
|
$tempLocation = rtrim(sys_get_temp_dir(), '/').DIRECTORY_SEPARATOR.sha1($asset->getAssetId()); |
113
|
|
|
$filesystem->dumpFile($tempLocation, $this->mediaManager->getFile($asset)); |
114
|
|
|
|
115
|
|
|
$resource = null; |
116
|
|
|
$size = getimagesize($tempLocation); |
117
|
|
|
switch ($size['mime']) { |
118
|
|
|
case 'image/jpeg': |
119
|
|
|
$resource = imagecreatefromjpeg($tempLocation); //jpeg file |
120
|
|
|
break; |
121
|
|
|
case 'image/gif': |
122
|
|
|
$resource = imagecreatefromgif($tempLocation); //gif file |
123
|
|
|
break; |
124
|
|
|
case 'image/png': |
125
|
|
|
$resource = imagecreatefrompng($tempLocation); //png file |
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
$filesystem->remove($tempLocation); |
129
|
|
|
|
130
|
|
|
return $resource; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.