[,...] [qualität=78] * → ziel-basis-.jpg je Breite * * Crop (exaktes Format, mittig beschnitten, z.B. OG-Image): * php bin/img-resize.php --crop x [qualität=78] */ if (PHP_SAPI !== 'cli') { exit(1); } $args = array_slice($argv, 1); $crop = false; if (($args[0] ?? '') === '--crop') { $crop = true; array_shift($args); } if (count($args) < 3) { fwrite(STDERR, "Nutzung: siehe Datei-Kommentar\n"); exit(1); } [$src, $dest, $spec] = $args; $quality = (int) ($args[3] ?? 78); $image = match (strtolower(pathinfo($src, PATHINFO_EXTENSION))) { 'jpg', 'jpeg' => imagecreatefromjpeg($src), 'png' => imagecreatefrompng($src), 'webp' => imagecreatefromwebp($src), default => null, }; if (!$image) { fwrite(STDERR, "Kann Quelle nicht lesen: {$src}\n"); exit(1); } $srcW = imagesx($image); $srcH = imagesy($image); if ($crop) { [$w, $h] = array_map('intval', explode('x', $spec)); $srcRatio = $srcW / $srcH; $dstRatio = $w / $h; if ($srcRatio > $dstRatio) { $cropH = $srcH; $cropW = (int) round($srcH * $dstRatio); } else { $cropW = $srcW; $cropH = (int) round($srcW / $dstRatio); } $x = (int) (($srcW - $cropW) / 2); $y = (int) (($srcH - $cropH) / 2); $out = imagecreatetruecolor($w, $h); imagecopyresampled($out, $image, 0, 0, $x, $y, $w, $h, $cropW, $cropH); imagejpeg($out, $dest, $quality); echo "{$dest} ({$w}x{$h})\n"; exit(0); } foreach (explode(',', $spec) as $width) { $w = min((int) $width, $srcW); // nie hochskalieren $h = (int) round($srcH * $w / $srcW); $out = imagecreatetruecolor($w, $h); imagecopyresampled($out, $image, 0, 0, 0, 0, $w, $h, $srcW, $srcH); $file = "{$dest}-{$w}.jpg"; imagejpeg($out, $file, $quality); echo "{$file} ({$w}x{$h}, " . round(filesize($file) / 1024) . " KB)\n"; }