rych/bytesize
A lightweight PHP package for parsing and formatting byte sizes. Convert human-readable values such as 1.5 MB into bytes and back again, making it useful for file-size validation, storage limits, and user-friendly display.
Install the package via Composer (it is framework-agnostic, so no Laravel service provider is shipped):
composer require rych/bytesize
The entry point is the Rych\ByteSize\ByteSize class. The fastest first use case is turning a raw byte count into a human-readable string for a file listing, upload limit, or CLI message:
use Rych\ByteSize\ByteSize;
echo ByteSize::format(1024); // "1.02 kB" (decimal, base 1000)
echo ByteSize::format(1024, ByteSize::BASE_BINARY); // "1 KiB" (binary, base 1024)
echo ByteSize::format(1048576, ByteSize::BASE_BINARY, 0); // "1 MiB"
Where to look first:
README.md — the usage examples show the exact format() signature and constants for the version you pulled.src/ByteSize.php — the format() method and BASE_DECIMAL / BASE_BINARY constants are the whole public surface.src/Formatter/ — if the version ships formatter classes, this is where unit labels and suffixes are defined.Because the last release was 2014, install it once, then immediately write a couple of tests around the boundary values (1023 / 1024 / 1024*1024) before building on top of it.
1. Thin helper wrapper (recommended). Because the API is a static call, wrap it so tests can swap it and so you have one place to enforce unit conventions:
// app/Support/HumanBytes.php
namespace App\Support;
use Rych\ByteSize\ByteSize;
class HumanBytes
{
public static function decimal(int $bytes, int $precision = 2): string
{
return ByteSize::format($bytes, ByteSize::BASE_DECIMAL, $precision);
}
public static function binary(int $bytes, int $precision = 2): string
{
return ByteSize::format($bytes, ByteSize::BASE_BINARY, $precision);
}
}
2. Blade directive for views. This is where byte formatting lives most often:
Blade::directive('bytesize', function ($expression) {
return "<?php echo App\\Support\\HumanBytes::decimal($expression); ?>";
});
<p>Attachment: @bytesize($file->size)</p>
3. Model accessor. Keep the raw integer column and expose the formatted value separately so you can still sort/filter on the raw size:
public function getSizeHumanAttribute(): string
{
return \App\Support\HumanBytes::binary($this->size);
}
4. Container binding + injection. If a formatter object is used in your version, bind a configured instance in a service provider and inject it rather than calling statics from controllers:
$this->app->singleton('bytesize.binary', fn () => new ByteSize(new Formatter\Binary()));
5. Consistency rule. Pick one base per UI surface and encode that decision in the helper, not at each call site. Mixing "MB" and "MiB" in the same table is the most common day-to-day defect this package will expose.
format($bytes, $base, $precision) — passing a precision where the base is expected produces plausible-looking but wrong output. Confirm against src/ByteSize.php for the version you installed, and rely on the constants rather than literals.BASE_DECIMAL (1000) yields kB/MB/GB; BASE_BINARY (1024) yields KiB/MiB/GiB. Decide deliberately — disk vendors and OSes disagree, and users will notice.ByteSize::format() calls across the codebase.0, negative numbers, and very large integers (32-bit PHP overflows around 2 GB). Add explicit tests; the package won't guard you.$precision = 2, 1024 under decimal shows as 1.02 kB, not 1 kB — fine, but expect QA tickets. Use precision = 0 for compact UI.Formatter classes are present, implement the formatter interface (or subclass Decimal/Binary) to change suffixes, spacing, or locale-specific decimal separators instead of post-processing the returned string.NumberFormatter-backed helpers or a maintained package may serve you better; keep this only if the tiny, dependency-free footprint is the actual value.How can I help you explore Laravel packages today?