image()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 5
nop 5
dl 0
loc 17
rs 9.9666
1
<?php
2
3
/**
4
 * UPLOAD
5
 */
6
define("CONF_UPLOAD_DIR", "uploads");
7
define("CONF_UPLOAD_IMAGE_DIR", "images");
8
9
10
/**
11
 * IMAGES
12
 */
13
define("CONF_IMAGE_CACHE", CONF_UPLOAD_DIR . "/" . CONF_UPLOAD_IMAGE_DIR . "/cache");
14
define("CONF_IMAGE_QUALITY", ["jpg" => 75, "png" => 5]);
15
16
17
/**
18
 * AUTOLOAD
19
 */
20
$baseDir   = dirname(__FILE__);
21
$libDir    = $baseDir . '/vendor';
22
$vendorDir = dirname(dirname($baseDir));
23
$autoload  = '/autoload.php';
24
25
if (!class_exists('whallysson\Thumb')) {
26
    if (file_exists($libDir . $autoload)) {
27
        require_once $libDir . $autoload;
28
    } elseif (file_exists($vendorDir . $autoload)) {
29
        require_once $vendorDir . $autoload;
30
    } else {
31
        require_once $baseDir . '/src/Thumb.php';
32
    }
33
}
34
35
/** Função para gerar imagens */
36
37
/**
38
 * @param string $image
39
 * @param int $width
40
 * @param int|null $height
41
 * @param int|null $zc
42
 * @return string
43
 */
44
function image($image, $width, $height = null, $zc = null, $imageName = null)
45
{
46
    if ($image) {
47
        $newimage = (new \whallysson\Thumb())->make($image, $width, $height, $zc, $imageName);
48
49
        /** WEBP */
50
        $newName = ($imageName ? $imageName : pathinfo(filter_var(mb_strtolower($newimage),
51
            FILTER_SANITIZE_STRIPPED))['filename']);
52
        $destination = "{$newName}-{$width}.webp";
53
54
        $webp = new \CodeBlog\ToWebP\ToWebP(CONF_UPLOAD_DIR, CONF_UPLOAD_IMAGE_DIR . "/cache");
55
        $webp->convert($newimage, $destination);
56
57
        return (!empty($webp->image_webp) ? $webp->image_webp : $webp->image_original);
58
    }
59
60
    return null;
61
}
62