Avif::save()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_images\Graphics\Format;
4
5
6
use kalanis\kw_images\ImagesException;
7
use kalanis\kw_images\Interfaces\IIMTranslations;
8
9
10
/**
11
 * Class Avif
12
 * AV1 Image File Format
13
 * @package kalanis\kw_images\Graphics\Format
14
 * @codeCoverageIgnore too much hassle to run tests on this one
15
 *
16
 * phpunit says:
17
1) ContentTests\FormatTest::testContentAvif
18
imageavif(): AVIF image support has been disabled
19
 */
20
class Avif extends AFormat
21
{
22
    /**
23
     * @param IIMTranslations|null $lang
24
     * @throws ImagesException
25
     */
26
    public function __construct(?IIMTranslations $lang = null)
27
    {
28
        $this->setImLang($lang);
29
        if (!function_exists('imagecreatefromavif') || !function_exists('imageavif')) {
30
            // @codeCoverageIgnoreStart
31
            throw new ImagesException($this->getImLang()->imImageMagicLibNotPresent(), ImagesException::FORMAT_NO_LIBRARY);
32
        }
33
        // @codeCoverageIgnoreEnd
34
    }
35
36
    public function load(string $path)
37
    {
38
        $result = @imagecreatefromavif($path);
39
        if (false === $result) {
40
            // @codeCoverageIgnoreStart
41
            throw new ImagesException($this->getImLang()->imCannotCreateFromResource(), ImagesException::FORMAT_AVIF_CANNOT_LOAD);
42
        }
43
        // @codeCoverageIgnoreEnd
44
        return $result;
45
    }
46
47
    public function save(?string $path, $resource): void
48
    {
49
        if (!@imageavif($resource, $path)) {
50
            // @codeCoverageIgnoreStart
51
            throw new ImagesException($this->getImLang()->imCannotSaveResource(), ImagesException::FORMAT_AVIF_CANNOT_SAVE);
52
        }
53
        // @codeCoverageIgnoreEnd
54
    }
55
}
56