|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Deployer; |
| 6 | + |
| 7 | +use Deployer\Exception\RunException; |
| 8 | + |
| 9 | +task('requirements:check:media_support', function (): void { |
| 10 | + if (!get('requirements_check_media_support_enabled')) { |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + checkGdFormatSupport(); |
| 15 | + checkImageToolFormatSupport(); |
| 16 | + checkBrotliExtension(); |
| 17 | +})->hidden(); |
| 18 | + |
| 19 | +function checkGdFormatSupport(): void |
| 20 | +{ |
| 21 | + $formats = ['AVIF' => 'AVIF Support', 'WebP' => 'WebP Support']; |
| 22 | + |
| 23 | + try { |
| 24 | + $gdJson = run('php -r "echo function_exists(\'gd_info\') ? json_encode(gd_info()) : \'null\';" 2>/dev/null'); |
| 25 | + } catch (RunException) { |
| 26 | + foreach ($formats as $label => $key) { |
| 27 | + addRequirementRow("GD: $label Support", REQUIREMENT_SKIP, 'Could not query GD'); |
| 28 | + } |
| 29 | + |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + $gdInfo = json_decode($gdJson, true); |
| 34 | + |
| 35 | + if (!is_array($gdInfo)) { |
| 36 | + foreach ($formats as $label => $key) { |
| 37 | + addRequirementRow("GD: $label Support", REQUIREMENT_SKIP, 'GD not available'); |
| 38 | + } |
| 39 | + |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + foreach ($formats as $label => $key) { |
| 44 | + $supported = !empty($gdInfo[$key]); |
| 45 | + addRequirementRow( |
| 46 | + "GD: $label Support", |
| 47 | + $supported ? REQUIREMENT_OK : REQUIREMENT_WARN, |
| 48 | + $supported ? 'Supported' : 'Not compiled into GD' |
| 49 | + ); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function checkImageToolFormatSupport(): void |
| 54 | +{ |
| 55 | + $formats = ['AVIF' => 'AVIF', 'WEBP' => 'WebP']; |
| 56 | + $formatList = null; |
| 57 | + $toolLabel = null; |
| 58 | + |
| 59 | + // Try GraphicsMagick first |
| 60 | + try { |
| 61 | + $output = run('gm convert -list format 2>/dev/null'); |
| 62 | + |
| 63 | + if ('' !== trim($output)) { |
| 64 | + $formatList = $output; |
| 65 | + $toolLabel = 'GraphicsMagick'; |
| 66 | + } |
| 67 | + } catch (RunException) { |
| 68 | + // not available |
| 69 | + } |
| 70 | + |
| 71 | + // Fallback to ImageMagick |
| 72 | + if (null === $formatList) { |
| 73 | + foreach (['magick', 'convert'] as $imCommand) { |
| 74 | + try { |
| 75 | + $output = run("$imCommand -list format 2>/dev/null"); |
| 76 | + |
| 77 | + if ('' !== trim($output)) { |
| 78 | + $formatList = $output; |
| 79 | + $toolLabel = 'ImageMagick'; |
| 80 | + |
| 81 | + break; |
| 82 | + } |
| 83 | + } catch (RunException) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (null === $formatList || null === $toolLabel) { |
| 90 | + foreach ($formats as $label) { |
| 91 | + addRequirementRow("IM/GM: $label", REQUIREMENT_SKIP, 'No image processing tool found'); |
| 92 | + } |
| 93 | + |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + foreach ($formats as $formatKey => $label) { |
| 98 | + $supported = (bool) preg_match('/^\s*' . $formatKey . '\b/mi', $formatList); |
| 99 | + addRequirementRow( |
| 100 | + "IM/GM: $label", |
| 101 | + $supported ? REQUIREMENT_OK : REQUIREMENT_WARN, |
| 102 | + $supported ? "Supported ($toolLabel)" : "Format not supported ($toolLabel)" |
| 103 | + ); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function checkBrotliExtension(): void |
| 108 | +{ |
| 109 | + try { |
| 110 | + $modules = strtolower(run('php -m 2>/dev/null')); |
| 111 | + } catch (RunException) { |
| 112 | + addRequirementRow('PHP ext: brotli', REQUIREMENT_SKIP, 'Could not retrieve PHP modules'); |
| 113 | + |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + $loaded = in_array('brotli', array_map('trim', explode("\n", $modules)), true); |
| 118 | + addRequirementRow( |
| 119 | + 'PHP ext: brotli', |
| 120 | + $loaded ? REQUIREMENT_OK : REQUIREMENT_WARN, |
| 121 | + $loaded ? 'Loaded' : 'Not loaded' |
| 122 | + ); |
| 123 | +} |
0 commit comments