1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Obblm\Core\Helper; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Intervention\Image\ImageManager; |
|
|
|
|
7
|
|
|
use Obblm\Core\Entity\Rule; |
8
|
|
|
use Obblm\Core\Entity\Team; |
9
|
|
|
use Obblm\Core\Service\FileTeamUploader; |
10
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
11
|
|
|
use Symfony\Component\Finder\Finder; |
12
|
|
|
use \SplFileInfo; |
13
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
14
|
|
|
|
15
|
|
|
class ImageHelper |
16
|
|
|
{ |
17
|
|
|
const RULE_PATH = '/rule'; |
18
|
|
|
const TEAM_PATH = '/team'; |
19
|
|
|
|
20
|
|
|
private $imageManager; |
21
|
|
|
private $ruleHelper; |
22
|
|
|
private $publicDirectory; |
23
|
|
|
private $cacheDirectory; |
24
|
|
|
private $ruleCacheDirectory; |
25
|
|
|
private $teamCacheDirectory; |
26
|
|
|
private $filesystem; |
27
|
|
|
private $fileTeamUploader; |
28
|
|
|
|
29
|
|
|
public function __construct(RuleHelper $ruleHelper, FileTeamUploader $fileTeamUploader, string $kernetProjectDir, string $cacheDirectory) |
30
|
|
|
{ |
31
|
|
|
$this->imageManager = new ImageManager(['driver' => 'gd']); |
32
|
|
|
$this->ruleHelper = $ruleHelper; |
33
|
|
|
$this->publicDirectory = $kernetProjectDir; |
34
|
|
|
$this->cacheDirectory = $cacheDirectory; |
35
|
|
|
$this->ruleCacheDirectory = $this->cacheDirectory . self::RULE_PATH ; |
36
|
|
|
$this->teamCacheDirectory = $this->cacheDirectory . self::TEAM_PATH ; |
37
|
|
|
$this->filesystem = new Filesystem(); |
38
|
|
|
$this->fileTeamUploader = $fileTeamUploader; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getTeamLogo(Team $team, int $width = null, int $height = null): ?string |
42
|
|
|
{ |
43
|
|
|
if ($team->getLogoFilename()) { |
44
|
|
|
$this->fileTeamUploader->setObjectSubDirectory($team->getId()); |
45
|
|
|
$filePath = $this->fileTeamUploader->getObjectDirectory() . '/' . $team->getLogoFilename(); |
46
|
|
|
if ($width || $height) { |
|
|
|
|
47
|
|
|
$newFilePath = $this->teamCacheDirectory . '/' . $team->getId(); |
48
|
|
|
$file = new File($filePath); |
49
|
|
|
return $this->returnCachedAsset($file, $newFilePath, $width, $height); |
50
|
|
|
} |
51
|
|
|
// full sized |
52
|
|
|
return $this->sanitizeFilePath($filePath); |
53
|
|
|
} |
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getTeamCover(Team $team, int $width = null, int $height = null): ?string |
58
|
|
|
{ |
59
|
|
|
if ($team->getCoverFilename()) { |
60
|
|
|
$this->fileTeamUploader->setObjectSubDirectory($team->getId()); |
61
|
|
|
$filePath = $this->fileTeamUploader->getObjectDirectory() . '/' . $team->getCoverFilename(); |
62
|
|
|
if ($width || $height) { |
|
|
|
|
63
|
|
|
$newFilePath = $this->teamCacheDirectory . '/' . $team->getId(); |
64
|
|
|
$file = new File($filePath); |
65
|
|
|
return $this->returnCachedAsset($file, $newFilePath, $width, $height); |
66
|
|
|
} |
67
|
|
|
// full sized |
68
|
|
|
return $this->sanitizeFilePath($filePath); |
69
|
|
|
} |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getRosterImage(Rule $rule, string $roster, int $width = null, int $height = null): ?string |
74
|
|
|
{ |
75
|
|
|
$helper = $this->ruleHelper->getHelper($rule); |
76
|
|
|
if ($helper->getRosters()->get($roster) && $rule->getRuleDirectory()) { |
77
|
|
|
$finder = new Finder(); |
78
|
|
|
$directory = dirname(__DIR__) . $rule->getRuleDirectory() . '/assets/'; |
79
|
|
|
$finder->files() |
80
|
|
|
->name($roster . '.*') |
81
|
|
|
->in($directory); |
82
|
|
|
if ($finder->hasResults()) { |
83
|
|
|
/** @var SplFileInfo[]|ArrayCollection $results */ |
84
|
|
|
$results = new ArrayCollection(); |
85
|
|
|
foreach ($finder as $file) { |
86
|
|
|
$results->add($file); |
87
|
|
|
} |
88
|
|
|
// We have to resize |
89
|
|
|
$newFilePath = $this->ruleCacheDirectory . '/' . $rule->getId(); |
90
|
|
|
$file = $results->first(); |
91
|
|
|
return $this->returnCachedAsset($file, $newFilePath, $width, $height); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function returnCachedAsset(SplFileInfo $file, $path = '', $width = null, $height = null) |
98
|
|
|
{ |
99
|
|
|
if ($width) { |
100
|
|
|
$path .= '/w' . $width; |
101
|
|
|
} |
102
|
|
|
if ($height) { |
103
|
|
|
$path .= '/h' . $height; |
104
|
|
|
} |
105
|
|
|
$newCacheDirectory = $path; |
106
|
|
|
$path .= '/' . $file->getFilename(); |
107
|
|
|
if (!$this->filesystem->exists($path)) { |
108
|
|
|
$img = $this->imageManager->make($file); |
109
|
|
|
$this->filesystem->mkdir($newCacheDirectory); |
110
|
|
|
$img->resize($width, $height, function ($constraint) { |
111
|
|
|
$constraint->aspectRatio(); |
112
|
|
|
}); |
113
|
|
|
$img->save($path); |
114
|
|
|
} |
115
|
|
|
return $this->sanitizeFilePath($path); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function sanitizeFilePath($filePath) |
119
|
|
|
{ |
120
|
|
|
return str_replace($this->publicDirectory, '', $filePath); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths