false, 'before' => $before, 'after' => $before]; if (!extension_loaded('gd') || !is_file($absolutePath)) { return $noop; } $info = @getimagesize($absolutePath); if (!$info) { return $noop; } [$width, $height, $type] = $info; $image = match ($type) { IMAGETYPE_JPEG => @imagecreatefromjpeg($absolutePath), IMAGETYPE_PNG => @imagecreatefrompng($absolutePath), IMAGETYPE_WEBP => function_exists('imagecreatefromwebp') ? @imagecreatefromwebp($absolutePath) : false, default => false, }; if (!$image) { return $noop; } // Nur verkleinern, nie vergroessern. $scale = min(1, self::MAX_DIMENSION / max($width, $height)); if ($scale < 1) { $newWidth = max(1, (int) round($width * $scale)); $newHeight = max(1, (int) round($height * $scale)); $resized = imagecreatetruecolor($newWidth, $newHeight); if ($type === IMAGETYPE_PNG || $type === IMAGETYPE_WEBP) { imagealphablending($resized, false); imagesavealpha($resized, true); } imagecopyresampled($resized, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); imagedestroy($image); $image = $resized; } $saved = match ($type) { IMAGETYPE_JPEG => imagejpeg($image, $absolutePath, self::JPEG_QUALITY), IMAGETYPE_PNG => imagepng($image, $absolutePath, self::PNG_COMPRESSION), IMAGETYPE_WEBP => function_exists('imagewebp') ? imagewebp($image, $absolutePath, self::WEBP_QUALITY) : false, default => false, }; imagedestroy($image); if (!$saved) { return $noop; } clearstatcache(true, $absolutePath); $after = @filesize($absolutePath) ?: $before; return ['changed' => true, 'before' => $before, 'after' => $after]; } }