1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_images\Content; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\FilesException; |
7
|
|
|
use kalanis\kw_images\Graphics; |
8
|
|
|
use kalanis\kw_images\ImagesException; |
9
|
|
|
use kalanis\kw_images\Interfaces\IIMTranslations; |
10
|
|
|
use kalanis\kw_images\Interfaces\ISizes; |
11
|
|
|
use kalanis\kw_images\Sources; |
12
|
|
|
use kalanis\kw_images\TLang; |
13
|
|
|
use kalanis\kw_mime\MimeException; |
14
|
|
|
use kalanis\kw_paths\PathsException; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ImageSize |
19
|
|
|
* Process image from source |
20
|
|
|
* @package kalanis\kw_images\Content |
21
|
|
|
*/ |
22
|
|
|
class ImageSize |
23
|
|
|
{ |
24
|
|
|
use TLang; |
25
|
|
|
|
26
|
|
|
/** @var Sources\Image */ |
27
|
|
|
protected $libImage = null; |
28
|
|
|
/** @var Graphics */ |
29
|
|
|
protected $libGraphics = null; |
30
|
|
|
/** @var ISizes */ |
31
|
|
|
protected $config = null; |
32
|
|
|
|
33
|
12 |
|
public function __construct(Graphics $graphics, ISizes $config, Sources\Image $image, ?IIMTranslations $lang = null) |
34
|
|
|
{ |
35
|
12 |
|
$this->setLang($lang); |
36
|
12 |
|
$this->libImage = $image; |
37
|
12 |
|
$this->libGraphics = $graphics; |
38
|
12 |
|
$this->config = $config; |
39
|
12 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string[] $sourcePath |
43
|
|
|
* @param string[] $targetPath |
44
|
|
|
* @throws FilesException |
45
|
|
|
* @throws ImagesException |
46
|
|
|
* @throws MimeException |
47
|
|
|
* @throws PathsException |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
6 |
|
public function process(array $sourcePath, array $targetPath): bool |
51
|
|
|
{ |
52
|
6 |
|
$sourceFull = array_values($sourcePath); |
53
|
6 |
|
$targetFull = array_values($targetPath); |
54
|
|
|
|
55
|
6 |
|
$tempPath = strval(tempnam(sys_get_temp_dir(), $this->config->getTempPrefix())); |
56
|
|
|
|
57
|
|
|
// get from the storage |
58
|
6 |
|
$resource = $this->libImage->get($sourceFull); |
59
|
6 |
|
if (empty($resource)) { |
60
|
1 |
|
throw new FilesException($this->getLang()->imThumbCannotGetBaseImage()); |
61
|
|
|
} |
62
|
|
|
|
63
|
5 |
|
if (false === @file_put_contents($tempPath, $resource)) { |
64
|
|
|
// @codeCoverageIgnoreStart |
65
|
|
|
throw new FilesException($this->getLang()->imThumbCannotStoreTemporaryImage()); |
66
|
|
|
} |
67
|
|
|
// @codeCoverageIgnoreEnd |
68
|
|
|
|
69
|
|
|
// now process image locally |
70
|
5 |
|
$this->libGraphics->setSizes($this->config)->resize($tempPath, $sourceFull, $targetFull); |
71
|
|
|
|
72
|
|
|
// return result to the storage as new file |
73
|
5 |
|
$result = @file_get_contents($tempPath); |
74
|
5 |
|
if (false === $result) { |
75
|
|
|
// @codeCoverageIgnoreStart |
76
|
|
|
throw new FilesException($this->getLang()->imThumbCannotLoadTemporaryImage()); |
77
|
|
|
} |
78
|
|
|
// @codeCoverageIgnoreEnd |
79
|
|
|
|
80
|
5 |
|
return $this->libImage->set($targetFull, $result); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
public function getImage(): Sources\Image |
84
|
|
|
{ |
85
|
3 |
|
return $this->libImage; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|