Webp::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 12
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 Webp
12
 * @package kalanis\kw_images\Graphics\Format
13
 * for some strange reason travisci automatic tests cannot use webp format in their GD library
14
 */
15
class Webp extends AFormat
16
{
17
    /**
18
     * @param IIMTranslations|null $lang
19
     * @throws ImagesException
20
     */
21
    public function __construct(?IIMTranslations $lang = null)
22
    {
23
        $this->setImLang($lang);
24
        if (!function_exists('imagecreatefromwebp') || !function_exists('imagewebp')) {
25
            // @codeCoverageIgnoreStart
26
            throw new ImagesException($this->getImLang()->imImageMagicLibNotPresent(),ImagesException::FORMAT_NO_LIBRARY);
27
        }
28
        // @codeCoverageIgnoreEnd
29
    }
30
31
    public function load(string $path)
32
    {
33
        $result = @imagecreatefromwebp($path);
34
        if (false === $result) {
35
            // @codeCoverageIgnoreStart
36
            throw new ImagesException($this->getImLang()->imCannotCreateFromResource(), ImagesException::FORMAT_WEBP_CANNOT_LOAD);
37
        }
38
        // @codeCoverageIgnoreEnd
39
        return $result;
40
    }
41
42
    public function save(?string $path, $resource): void
43
    {
44
        if (!@imagewebp($resource, $path)) {
45
            // @codeCoverageIgnoreStart
46
            throw new ImagesException($this->getImLang()->imCannotSaveResource(), ImagesException::FORMAT_WEBP_CANNOT_SAVE);
47
        }
48
        // @codeCoverageIgnoreEnd
49
    }
50
}
51