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\Traits\TLang; |
13
|
|
|
use kalanis\kw_mime\MimeException; |
14
|
|
|
use kalanis\kw_paths\PathsException; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ImageRotate |
19
|
|
|
* Rotate image against the passed data |
20
|
|
|
* @package kalanis\kw_images\Content |
21
|
|
|
*/ |
22
|
|
|
class ImageRotate |
23
|
|
|
{ |
24
|
|
|
use TLang; |
25
|
|
|
|
26
|
|
|
protected Sources\Image $libImage; |
27
|
|
|
protected Graphics $libGraphics; |
28
|
|
|
protected ISizes $config; |
29
|
|
|
|
30
|
11 |
|
public function __construct(Graphics $graphics, ISizes $config, Sources\Image $image, ?IIMTranslations $lang = null) |
31
|
|
|
{ |
32
|
11 |
|
$this->setImLang($lang); |
33
|
11 |
|
$this->libImage = $image; |
34
|
11 |
|
$this->libGraphics = $graphics; |
35
|
11 |
|
$this->config = $config; |
36
|
11 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string[] $sourcePath |
40
|
|
|
* @param float|null $rotateAngle target angle |
41
|
|
|
* @param int|null $flipMode as defined by function imageflip [IMG_FLIP_HORIZONTAL, IMG_FLIP_VERTICAL, IMG_FLIP_BOTH] |
42
|
|
|
* @param string[]|null $targetPath mainly for tests |
43
|
|
|
* @throws FilesException |
44
|
|
|
* @throws ImagesException |
45
|
|
|
* @throws MimeException |
46
|
|
|
* @throws PathsException |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
4 |
|
public function process(array $sourcePath, ?float $rotateAngle, ?int $flipMode = null, ?array $targetPath = null): bool |
50
|
|
|
{ |
51
|
4 |
|
$sourceFull = array_values($sourcePath); |
52
|
4 |
|
$targetFull = $targetPath ? array_values($targetPath) : $sourceFull; |
53
|
|
|
|
54
|
4 |
|
$tempPath = strval(tempnam(sys_get_temp_dir(), $this->config->getTempPrefix())); |
55
|
|
|
|
56
|
|
|
// get from the storage |
57
|
4 |
|
$resource = $this->libImage->get($sourceFull); |
58
|
4 |
|
if (empty($resource)) { |
59
|
1 |
|
@unlink($tempPath); |
|
|
|
|
60
|
1 |
|
throw new FilesException($this->getImLang()->imThumbCannotGetBaseImage()); |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
if (false === @file_put_contents($tempPath, $resource)) { |
64
|
|
|
// @codeCoverageIgnoreStart |
65
|
|
|
@unlink($tempPath); |
66
|
|
|
throw new FilesException($this->getImLang()->imThumbCannotStoreTemporaryImage()); |
67
|
|
|
} |
68
|
|
|
// @codeCoverageIgnoreEnd |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
// now process image locally |
72
|
3 |
|
$this->libGraphics->rotate($rotateAngle, $flipMode, $tempPath, $sourceFull, $targetFull); |
73
|
1 |
|
} catch (ImagesException $ex) { |
74
|
|
|
// clear when fails |
75
|
1 |
|
@unlink($tempPath); |
76
|
1 |
|
throw $ex; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// return result to the storage as new file |
80
|
2 |
|
$result = @file_get_contents($tempPath); |
81
|
2 |
|
if (false === $result) { |
82
|
|
|
// @codeCoverageIgnoreStart |
83
|
|
|
@unlink($tempPath); |
84
|
|
|
throw new FilesException($this->getImLang()->imThumbCannotLoadTemporaryImage()); |
85
|
|
|
} |
86
|
|
|
// @codeCoverageIgnoreEnd |
87
|
|
|
|
88
|
2 |
|
$set = $this->libImage->set($targetFull, $result); |
89
|
2 |
|
@unlink($tempPath); |
90
|
2 |
|
return $set; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getImage(): Sources\Image |
94
|
|
|
{ |
95
|
|
|
return $this->libImage; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: