Completed
Push — master ( c85726...8dd839 )
by Freek
02:02
created

Webp::requirementsAreInstalled()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 0
1
<?php
2
3
namespace Spatie\MediaLibrary\ImageGenerators\FileTypes;
4
5
use Illuminate\Support\Collection;
6
use Spatie\MediaLibrary\Conversion\Conversion;
7
use Spatie\MediaLibrary\ImageGenerators\BaseGenerator;
8
9
class Webp extends BaseGenerator
10
{
11
    public function convert(string $file, Conversion $conversion = null) : string
12
    {
13
        $pathToImageFile = pathinfo($file, PATHINFO_DIRNAME).'/'.pathinfo($file, PATHINFO_FILENAME).'.png';
14
15
        $image = imagecreatefromwebp($file);
16
17
        imagepng($image, $pathToImageFile, 9);
18
19
        imagedestroy($image);
20
21
        return $pathToImageFile;
22
    }
23
24
    public function requirementsAreInstalled() : bool
25
    {
26
        if (! function_exists('imagecreatefromwebp')) {
27
            return false;
28
        }
29
30
        if (! function_exists('imagepng')) {
31
            return false;
32
        }
33
34
        if (! function_exists('imagedestroy')) {
35
            return false;
36
        }
37
38
        return true;
39
    }
40
41
    public function supportedExtensions() : Collection
42
    {
43
        return collect(['webp']);
44
    }
45
46
    public function supportedMimeTypes() : Collection
47
    {
48
        return collect(['image/webp']);
49
    }
50
}
51