alex-kalanis /
kw_images
| 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 | 12 | public function __construct(Graphics $graphics, ISizes $config, Sources\Image $image, ?IIMTranslations $lang = null) |
|
| 31 | { |
||
| 32 | 12 | $this->setImLang($lang); |
|
| 33 | 12 | $this->libImage = $image; |
|
| 34 | 12 | $this->libGraphics = $graphics; |
|
| 35 | 12 | $this->config = $config; |
|
| 36 | } |
||
| 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 | 5 | public function process(array $sourcePath, ?float $rotateAngle, ?int $flipMode = null, ?array $targetPath = null): bool |
|
| 50 | { |
||
| 51 | 5 | $sourceFull = array_values($sourcePath); |
|
| 52 | 5 | $targetFull = $targetPath ? array_values($targetPath) : $sourceFull; |
|
| 53 | |||
| 54 | 5 | $tempPath = strval(tempnam(sys_get_temp_dir(), $this->config->getTempPrefix())); |
|
| 55 | |||
| 56 | // get from the storage |
||
| 57 | 5 | $resource = $this->libImage->get($sourceFull); |
|
| 58 | 5 | if (empty($resource)) { |
|
| 59 | 1 | @unlink($tempPath); |
|
|
0 ignored issues
–
show
|
|||
| 60 | 1 | throw new FilesException($this->getImLang()->imThumbCannotGetBaseImage()); |
|
| 61 | } |
||
| 62 | |||
| 63 | 4 | 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 | 4 | $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 | 3 | $result = @file_get_contents($tempPath); |
|
| 81 | 3 | if (false === $result) { |
|
| 82 | // @codeCoverageIgnoreStart |
||
| 83 | @unlink($tempPath); |
||
| 84 | throw new FilesException($this->getImLang()->imThumbCannotLoadTemporaryImage()); |
||
| 85 | } |
||
| 86 | // @codeCoverageIgnoreEnd |
||
| 87 | |||
| 88 | 3 | $set = $this->libImage->set($targetFull, $result); |
|
| 89 | 3 | @unlink($tempPath); |
|
| 90 | 3 | return $set; |
|
| 91 | } |
||
| 92 | |||
| 93 | 1 | public function getImage(): Sources\Image |
|
| 94 | { |
||
| 95 | 1 | return $this->libImage; |
|
| 96 | } |
||
| 97 | } |
||
| 98 |
If you suppress an error, we recommend checking for the error condition explicitly: