1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_images\Content; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\FilesException; |
7
|
|
|
use kalanis\kw_images\ImagesException; |
8
|
|
|
use kalanis\kw_images\Sources; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Images |
13
|
|
|
* Process images parts |
14
|
|
|
* @package kalanis\kw_images\Content |
15
|
|
|
*/ |
16
|
|
|
class Images |
17
|
|
|
{ |
18
|
|
|
/** @var ImageSize */ |
19
|
|
|
protected $libSizes = null; |
20
|
|
|
/** @var Sources\Image */ |
21
|
|
|
protected $libImage = null; |
22
|
|
|
/** @var Sources\Thumb */ |
23
|
|
|
protected $libThumb = null; |
24
|
|
|
/** @var Sources\Desc */ |
25
|
|
|
protected $libDesc = null; |
26
|
|
|
|
27
|
5 |
|
public function __construct(ImageSize $sizes, Sources\Image $image, Sources\Thumb $thumb, Sources\Desc $desc) |
28
|
|
|
{ |
29
|
5 |
|
$this->libSizes = $sizes; |
30
|
5 |
|
$this->libImage = $image; |
31
|
5 |
|
$this->libThumb = $thumb; |
32
|
5 |
|
$this->libDesc = $desc; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string[] $wantedPath where we want to store the file |
37
|
|
|
* @return resource|string |
38
|
|
|
*/ |
39
|
1 |
|
public function get(array $wantedPath) |
40
|
|
|
{ |
41
|
|
|
try { |
42
|
1 |
|
return $this->libImage->get($wantedPath); |
43
|
1 |
|
} catch (FilesException $ex) { |
44
|
1 |
|
return ''; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string[] $wantedPath where we want to store the file |
50
|
|
|
* @param string|resource $content what we want to store as the file |
51
|
|
|
* @throws FilesException |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
1 |
|
public function set(array $wantedPath, $content): bool |
55
|
|
|
{ |
56
|
1 |
|
return $this->libImage->set($wantedPath, $content); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string[] $wantedPath where we want to store the file |
61
|
|
|
* @throws FilesException |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
1 |
|
public function remove(array $wantedPath): bool |
65
|
|
|
{ |
66
|
1 |
|
$fileName = strval(array_pop($wantedPath)); |
67
|
1 |
|
return $this->libImage->delete($wantedPath, $fileName); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string[] $wantedPath where we want to store the file |
72
|
|
|
* @return resource|string |
73
|
|
|
*/ |
74
|
1 |
|
public function getThumb(array $wantedPath) |
75
|
|
|
{ |
76
|
|
|
try { |
77
|
1 |
|
return $this->libThumb->get($wantedPath); |
78
|
1 |
|
} catch (FilesException $ex) { |
79
|
1 |
|
return ''; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string[] $wantedPath where we want to store the file |
85
|
|
|
* @throws FilesException |
86
|
|
|
* @throws ImagesException |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
2 |
|
public function updateThumb(array $wantedPath): bool |
90
|
|
|
{ |
91
|
2 |
|
return $this->libSizes->process( |
92
|
2 |
|
$this->libImage->getPath($wantedPath), |
93
|
2 |
|
$this->libThumb->getPath($wantedPath) |
94
|
2 |
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string[] $wantedPath where we want to store the file |
99
|
|
|
* @throws FilesException |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
2 |
|
public function removeThumb(array $wantedPath): bool |
103
|
|
|
{ |
104
|
2 |
|
$fileName = strval(array_pop($wantedPath)); |
105
|
2 |
|
return $this->libThumb->delete($wantedPath, $fileName); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string[] $path |
110
|
|
|
* @throws FilesException |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
1 |
|
public function getDescription(array $path): string |
114
|
|
|
{ |
115
|
1 |
|
return $this->libDesc->get($path, false); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string[] $wantedPath where we want to store the file |
120
|
|
|
* @param string $description |
121
|
|
|
* @throws FilesException |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
2 |
|
public function updateDescription(array $wantedPath, string $description = ''): bool |
125
|
|
|
{ |
126
|
2 |
|
if (!empty($description)) { |
127
|
2 |
|
return $this->libDesc->set($wantedPath, $description); |
128
|
|
|
} else { |
129
|
1 |
|
$fileName = strval(array_pop($wantedPath)); |
130
|
1 |
|
return $this->libDesc->delete($wantedPath, $fileName); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|