blob: 9a2c2a5b8710a15e004d89fb2a1feb33ad53cca0 (
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
|
<?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 -layers optimize -loop 0 $output_path", result_code: $result_code);
return $result_code;
}
|