- How do I install zenstruck/bytes in a Laravel project?
- Run `composer require zenstruck/bytes` in your project root. The package has no Laravel-specific dependencies and works out-of-the-box with PHP 8.1+ and Laravel 10+. No additional configuration is needed for basic usage.
- Can I parse human-readable strings like '1.5 MB' into bytes?
- Yes. Use `Bytes::parse('1.5 MB')` to convert strings like '1.5 MB', '1024 KiB', or '2.3 GB' into integer byte values. The package supports both binary (KiB, MiB) and decimal (KB, MB) units.
- How do I format bytes into human-readable strings (e.g., '1.0 MB')?
- Cast the `Bytes` object to a string: `(string) Bytes::parse(1048576)` returns '1.0 MB'. For custom formatting, use `->format('%.2n B')` to control precision and units (e.g., '1.00 MB').
- Does zenstruck/bytes work with Laravel’s Storage facade for file sizes?
- Absolutely. Use it to humanize file sizes from `Storage::disk()->filesize('path/to/file')`. For example, `Bytes::parse(Storage::disk('s3')->size('file.jpg'))->human()` returns '1.2 MB' for Blade or API responses.
- What Laravel versions and PHP versions are supported?
- The package is officially tested with Laravel 10+ and PHP 8.1+. It also works with PHP 7.4+ and older Laravel versions (9.x) if you’re constrained by legacy requirements. Check the [GitHub repo](https://github.com/zenstruck/bytes) for version-specific notes.
- How do I ensure consistent byte formatting across my app (e.g., always '1.00 MB')?
- Use the `format()` method with ICU-style syntax: `Bytes::parse(1048576)->format('%.2f B')` forces two decimal places. For global consistency, extend Laravel’s `AppServiceProvider` to wrap `Bytes::parse()` calls with your preferred format.
- Can I compare byte values (e.g., check if a file exceeds 100MB)?
- Yes. Use comparison methods like `Bytes::parse('100MB')->isLessThan($fileSize)` or `Bytes::parse('500MB')->gt(Bytes::parse('1GB'))` for boolean checks. These methods support both `Bytes` objects and raw values.
- Will this package work with non-English locales (e.g., comma vs. dot decimals)?
- Yes, but it requires PHP’s `intl` extension. Humanized output respects PHP’s locale settings (e.g., '1,0 MB' in German). Test with `setlocale(LC_ALL, 'de_DE')` before deployment if targeting specific locales.
- Are there performance concerns for high-traffic APIs using this package?
- No. Parsing and formatting bytes add negligible overhead (~1–2ms per call). Humanization is the most expensive operation, but it’s still efficient for most use cases. Benchmark in your staging environment if processing millions of requests.
- What alternatives exist for byte manipulation in Laravel, and why choose zenstruck/bytes?
- Alternatives include PHP’s built-in `number_format()` or third-party packages like `voku/portable-ascii`. zenstruck/bytes stands out for its immutable value object design, support for both binary/decimal units, and Laravel-specific integrations (e.g., Storage, API responses). It’s also actively maintained with no critical issues.