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
|
|
|
throw new FilesException($this->getImLang()->imThumbCannotGetBaseImage()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (false === @file_put_contents($tempPath, $resource)) { |
61
|
|
|
// @codeCoverageIgnoreStart |
62
|
|
|
throw new FilesException($this->getImLang()->imThumbCannotStoreTemporaryImage()); |
63
|
|
|
} |
64
|
|
|
// @codeCoverageIgnoreEnd |
65
|
|
|
|
66
|
|
|
// now process image locally |
67
|
|
|
$this->libGraphics->setSizes($this->config)->orientate($tempPath, $sourceFull, $targetFull); |
68
|
|
|
|
69
|
|
|
// return result to the storage as new file |
70
|
|
|
$result = @file_get_contents($tempPath); |
71
|
|
|
if (false === $result) { |
72
|
|
|
// @codeCoverageIgnoreStart |
73
|
|
|
throw new FilesException($this->getImLang()->imThumbCannotLoadTemporaryImage()); |
74
|
|
|
} |
75
|
|
|
// @codeCoverageIgnoreEnd |
76
|
|
|
|
77
|
|
|
return $this->libImage->set($targetFull, $result); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getImage(): Sources\Image |
81
|
|
|
{ |
82
|
|
|
return $this->libImage; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|