summaryrefslogtreecommitdiff
path: root/src/images.php
blob: 94626410525c98168a583754f81ad2da546c3cd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
function resize_image(string $src_path, string $dst_path, int $max_width, int $max_height, bool $set_format = true, bool $stretch = false): int|null
{
    if ($src_path == "") {
        return -2;
    }

    $image = getimagesize($src_path);

    if ($image == false) {
        return -1;
    }

    $format = $set_format ? ".webp" : "";

    $width = $image[0];
    $height = $image[1];
    $ratio = min($max_width / $width, $max_height / $height);
    $new_width = $stretch ? $max_width : (int) ($width * $ratio);
    $new_height = $stretch ? $max_height : (int) ($height * $ratio);

    $input_path = escapeshellarg($src_path);
    $output_path = escapeshellarg("$dst_path$format");

    $result_code = null;

    exec(command: "magick convert $input_path -coalesce -resize {$new_width}x{$new_height}! -loop 0 $output_path", result_code: $result_code);

    return $result_code;
}

function create_image_bundle(string $src_path, string $dst_path, int $max_width, int $max_height, bool $set_format = true, bool $stretch = false): int|null
{
    if (!is_dir($dst_path)) {
        mkdir($dst_path, 0777, true);
    }

    if ($err = resize_image($src_path, "$dst_path/3x", $max_width, $max_height, $set_format, $stretch)) {
        array_map("unlink", glob("$dst_path/*.*"));
        rmdir($dst_path);
        return $err;
    }

    if ($err = resize_image($src_path, "$dst_path/2x", round($max_width / 2), round($max_height / 2), $set_format, $stretch)) {
        array_map("unlink", glob("$dst_path/*.*"));
        rmdir($dst_path);
        return $err;
    }

    if ($err = resize_image($src_path, "$dst_path/1x", round($max_width / 4), round($max_height / 4), $set_format, $stretch)) {
        array_map("unlink", glob("$dst_path/*.*"));
        rmdir($dst_path);
        return $err;
    }

    return null;
}

function get_file_extension(string $path): string|null
{
    if ($file = getimagesize($path)) {
        return image_type_to_extension(intval($file[2]), false);
    }

    return null;
}

function does_file_meet_requirements(string $path, int $max_width, int $max_height): array
{
    $file = getimagesize($path);
    if (!$file) {
        return [false, null];
    }

    return [$file[0] <= $max_width && $file[1] <= $max_height, image_type_to_extension(intval($file[2]), false)];
}