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