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 2019 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 2019 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Command; |
18
|
|
|
|
19
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
20
|
|
|
use League\Flysystem\FileNotFoundException; |
21
|
|
|
use League\Flysystem\Filesystem; |
22
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageRendition; |
23
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageRenditionInterface; |
24
|
|
|
use SWP\Bundle\CoreBundle\Resolver\AssetLocationResolver; |
25
|
|
|
use Symfony\Component\Console\Command\Command; |
26
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
27
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
28
|
|
|
|
29
|
|
|
class FixIncompleteImagesDataCommand extends Command |
30
|
|
|
{ |
31
|
|
|
protected static $defaultName = 'swp:migration:fix-renditions-width-height'; |
32
|
|
|
|
33
|
|
|
private $entityManager; |
34
|
|
|
|
35
|
|
|
private $filesystem; |
36
|
|
|
|
37
|
|
|
private $assetLocationResolver; |
38
|
|
|
|
39
|
|
|
public function __construct( |
40
|
|
|
EntityManagerInterface $entityManager, |
41
|
|
|
Filesystem $filesystem, |
42
|
|
|
AssetLocationResolver $assetLocationResolver |
43
|
|
|
) { |
44
|
|
|
parent::__construct(); |
45
|
|
|
|
46
|
|
|
$this->entityManager = $entityManager; |
47
|
|
|
$this->filesystem = $filesystem; |
48
|
|
|
$this->assetLocationResolver = $assetLocationResolver; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function configure(): void |
52
|
|
|
{ |
53
|
|
|
$this |
54
|
|
|
->setName(self::$defaultName) |
55
|
|
|
->setDescription('Finds image renditions with width and height set to 0 and sets if from file.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
59
|
|
|
{ |
60
|
|
|
$query = $this->entityManager->createQuery(' |
61
|
|
|
SELECT |
62
|
|
|
ir.id, i.assetId, i.fileExtension |
63
|
|
|
FROM |
64
|
|
|
SWP\Bundle\ContentBundle\Model\ImageRendition ir |
65
|
|
|
LEFT JOIN |
66
|
|
|
SWP\Bundle\CoreBundle\Model\Image i WITH ir.image = i.id |
67
|
|
|
WHERE |
68
|
|
|
ir.width = 0 |
69
|
|
|
OR |
70
|
|
|
ir.height = 0 |
71
|
|
|
'); |
72
|
|
|
|
73
|
|
|
$brokenImages = $query->getResult(); |
74
|
|
|
$counter = 0; |
75
|
|
|
foreach ($brokenImages as $brokenImage) { |
76
|
|
|
/** @var ImageRenditionInterface $imageReference */ |
77
|
|
|
$imageReference = $this->entityManager->getReference(ImageRendition::class, $brokenImage['id']); |
78
|
|
|
$filePath = $this->assetLocationResolver->getMediaBasePath().'/'.$brokenImage['assetId'].'.'.$brokenImage['fileExtension']; |
79
|
|
|
|
80
|
|
|
try { |
81
|
|
|
$file = $this->filesystem->read($filePath); |
82
|
|
|
$imageResource = imagecreatefromstring($file); |
83
|
|
|
$imageReference->setWidth(imagesx($imageResource)); |
84
|
|
|
$imageReference->setHeight(imagesy($imageResource)); |
85
|
|
|
++$counter; |
86
|
|
|
} catch (FileNotFoundException $e) { |
87
|
|
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($counter > 4) { |
91
|
|
|
$output->writeln('<bg=green;options=bold>In progress... Processed '.($counter - 1).' renditions.</>'); |
92
|
|
|
$this->entityManager->flush(); |
93
|
|
|
$this->entityManager->clear(); |
94
|
|
|
$counter = 0; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
$this->entityManager->flush(); |
98
|
|
|
|
99
|
|
|
$output->writeln('<bg=green;options=bold>Done. In total processed '.\count($brokenImages).' renditions.</>'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|