Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Bytesize Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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:

  1. README.md — the usage examples show the exact format() signature and constants for the version you pulled.
  2. src/ByteSize.php — the format() method and BASE_DECIMAL / BASE_BINARY constants are the whole public surface.
  3. 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.

Implementation Patterns

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.

Gotchas and Tips

  • Argument order is easy to misremember. 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.
  • Decimal vs binary labels. 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.
  • Old code on modern PHP. Last tagged 2014, so run it under the PHP version you actually deploy. If you see deprecation notices from loose comparisons or dynamic properties, consider vendoring the single class into your own namespace rather than fighting the dependency.
  • Static methods are hard to test/mock. Wrap as above; don't scatter ByteSize::format() calls across the codebase.
  • Edge inputs. Verify behavior for 0, negative numbers, and very large integers (32-bit PHP overflows around 2 GB). Add explicit tests; the package won't guard you.
  • Rounding surprises. With $precision = 2, 1024 under decimal shows as 1.02 kB, not 1 kB — fine, but expect QA tickets. Use precision = 0 for compact UI.
  • Extension points. If 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.
  • Consider the trade-off. The package is 0-dependents, unmaintained, and does one thing. If you already need locale-aware formatting, Symfony's NumberFormatter-backed helpers or a maintained package may serve you better; keep this only if the tiny, dependency-free footprint is the actual value.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
diephp/sequences
users-au/socialite-provider
twidpay/twid-logger
mrnewport/laravel-plaid
mrnewport/laravel-repo
mrnewport/laravel-docsign
mrnewport/laravel-stow
mrnewport/laravel-flow
mrnewport/laravel-priceable
mrnewport/laravel-nestedterms
fredmeng/laravel-apidoc-generator
itstructure/laravel-multilingual-tools
pdustdar/elasticsearch
markupai/php-sdk
rlacerda83/lumem-microservice-shipment
uni-method/symfony-lib
tacticmedia/scim-server-bundle
neika-ai/symfony-bundle
mattleads/agent-publishing-bundle
glavweb/framework-bundle