Skip to content

Commit 3871b0a

Browse files
committed
Add pngcrush helper
1 parent fe17347 commit 3871b0a

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Laravel Essentials
22

3-
Growing library for simple macros, facades and other helpers in the Laravel cosmos .
3+
Growing library for simple macros, facades and other helpers in the Laravel cosmos.
44

55
## Installation
66

bootstrap/helpers.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,22 @@ function decode_json(string $value): array
1313
return \Illuminate\Support\Str::toJson($value);
1414
}
1515
}
16+
17+
if (! function_exists('crush_png')) {
18+
function crush_png(string $image): bool
19+
{
20+
if ($pngcrush = exec('which pngcrush')) {
21+
$format = '%s -d %s -q -rem alla %s > /dev/null 2>&1';
22+
$dir = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR);
23+
$name = basename($image);
24+
25+
$command = sprintf($format, $pngcrush, $dir, escapeshellarg($image));
26+
27+
if (exec($command) !== false) {
28+
return rename($dir.DIRECTORY_SEPARATOR.$name, $image);
29+
}
30+
}
31+
32+
return false;
33+
}
34+
}

tests/Feature/HelpersTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,15 @@
2424
->toContain('greeting');
2525

2626
})->covers('encode_json');
27+
28+
it('can crush png', function () {
29+
$version = null;
30+
31+
if ($pngcrush = exec('which pngcrush')) {
32+
$version = exec($pngcrush.' --version');
33+
}
34+
35+
expect($version)->toBeString();
36+
// ->toContain('pngcrush');
37+
38+
})->covers('crush_png');

0 commit comments

Comments
 (0)