1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_images\Content; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\FilesException; |
7
|
|
|
use kalanis\kw_images\Configs; |
8
|
|
|
use kalanis\kw_images\Graphics; |
9
|
|
|
use kalanis\kw_images\ImagesException; |
10
|
|
|
use kalanis\kw_images\Interfaces\ISizes; |
11
|
|
|
use kalanis\kw_images\Sources; |
12
|
|
|
use kalanis\kw_mime\MimeException; |
13
|
|
|
use kalanis\kw_paths\Interfaces\IPaths; |
14
|
|
|
use kalanis\kw_paths\PathsException; |
15
|
|
|
use kalanis\kw_paths\Stuff; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ImageUpload |
20
|
|
|
* Process uploaded content |
21
|
|
|
* @package kalanis\kw_images\Content |
22
|
|
|
*/ |
23
|
|
|
class ImageUpload |
24
|
|
|
{ |
25
|
|
|
protected Graphics $graphics; |
26
|
|
|
protected Sources\Image $imageSource; |
27
|
|
|
protected ISizes $config; |
28
|
|
|
protected Images $images; |
29
|
|
|
protected Configs\ProcessorConfig $procConfig; |
30
|
|
|
protected ImageOrientate $orientation; |
31
|
|
|
protected ImageSize $libSizes; |
32
|
|
|
|
33
|
3 |
|
public function __construct( |
34
|
|
|
Graphics $graphics, |
35
|
|
|
Sources\Image $libImage, |
36
|
|
|
ISizes $config, |
37
|
|
|
Images $images, |
38
|
|
|
Configs\ProcessorConfig $procConfig, |
39
|
|
|
ImageOrientate $orientation, |
40
|
|
|
ImageSize $libSizes |
41
|
|
|
) |
42
|
|
|
{ |
43
|
3 |
|
$this->graphics = $graphics; |
44
|
3 |
|
$this->imageSource = $libImage; |
45
|
3 |
|
$this->config = $config; |
46
|
3 |
|
$this->images = $images; |
47
|
3 |
|
$this->procConfig = $procConfig; |
48
|
3 |
|
$this->orientation = $orientation; |
49
|
3 |
|
$this->libSizes = $libSizes; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string[] $wantedPath where we want to store the file |
54
|
|
|
* @param string $name |
55
|
|
|
* @throws FilesException |
56
|
|
|
* @throws PathsException |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
1 |
|
public function findFreeName(array $wantedPath, string $name): string |
60
|
|
|
{ |
61
|
1 |
|
$name = Stuff::canonize($name); |
62
|
1 |
|
$ext = $this->procConfig->canLimitExt() && !empty($this->procConfig->getDefaultExt()) |
63
|
1 |
|
? $this->procConfig->getDefaultExt() |
64
|
1 |
|
: Stuff::fileExt($name); |
65
|
1 |
|
if (0 < mb_strlen($ext)) { |
66
|
1 |
|
$ext = IPaths::SPLITTER_DOT . $ext; |
67
|
|
|
} |
68
|
1 |
|
$fileName = Stuff::fileBase($name); |
69
|
1 |
|
return $this->imageSource->findFreeName($wantedPath, $fileName, $ext); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string[] $wantedPath where we want to store the file |
74
|
|
|
* @param string $tempPath where the file is accessible after upload |
75
|
|
|
* @param string $description |
76
|
|
|
* @param bool $useOrientation |
77
|
|
|
* @throws FilesException |
78
|
|
|
* @throws ImagesException |
79
|
|
|
* @throws MimeException |
80
|
|
|
* @throws PathsException |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
1 |
|
public function process(array $wantedPath, string $tempPath = '', string $description = '', bool $useOrientation = false): bool |
84
|
|
|
{ |
85
|
1 |
|
$fullPath = array_values($wantedPath); |
86
|
|
|
// check file |
87
|
1 |
|
$this->graphics->setSizes($this->config)->check($tempPath, $fullPath); |
88
|
|
|
|
89
|
|
|
// store image |
90
|
1 |
|
$uploaded = strval(@file_get_contents($tempPath)); |
91
|
1 |
|
$this->imageSource->set($fullPath, $uploaded); |
92
|
1 |
|
@unlink($tempPath); |
|
|
|
|
93
|
|
|
|
94
|
|
|
// orientate if set |
95
|
1 |
|
if ($useOrientation) { |
96
|
|
|
try { |
97
|
1 |
|
$this->orientation->process($fullPath); |
98
|
1 |
|
} catch (ImagesException $ex) { |
99
|
|
|
// this failure will be skipped |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// resize if set |
104
|
1 |
|
if ($this->procConfig->canLimitSize() && !empty($this->config->getMaxInWidth()) && !empty($this->config->getMaxInHeight())) { |
105
|
1 |
|
$this->libSizes->process($fullPath, $fullPath); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// thumbs |
109
|
1 |
|
$this->images->removeThumb($fullPath); |
110
|
1 |
|
if ($this->procConfig->hasThumb()) { |
111
|
1 |
|
$this->images->updateThumb($fullPath); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// description |
115
|
1 |
|
$this->images->updateDescription($fullPath, $description); |
116
|
1 |
|
return true; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: