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

Webp   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 42
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A convert() 0 12 1
A requirementsAreInstalled() 0 16 4
A supportedExtensions() 0 4 1
A supportedMimeTypes() 0 4 1
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