Formatter for human-readable memory
Only handles time up to a few gigs gracefully

@param double  $size
@param integer $percision
172403
protected function getReadableMemory($size, $percision = 2)
    {
        $unitOptions = array('b', 'k', 'M', 'G');

        $base = log($size, 1024);

        $memory = round(pow(1024, $base - floor($base)), $percision);
        $unit = $unitOptions[floor($base)];
        return "{$memory} {$unit}";
    }
501431
public function setSize($size)
    {
        if (!empty($size)) {
            $this->size = $size;
            $this->pxRatio = round($size / 5);

            return $this;
        }
    }
1115187
public static function readableFilesize($size, $precision = 2)
    {
        $result = $size;
        $index = 0;
        while ($result > 1024 && $index < count(self::$prefixes)) {
            $result = $result / 1024;
            $index++;
        }

        return sprintf('%1.'.$precision.'f %sB', $result, self::$prefixes[$index]);
    }
532796
public static function parseSize($size)
    {
        $unit = preg_replace('/[^bkmgtpezy]/i', '', $size);
        $size = preg_replace('/[^0-9\.]/', '', $size);

        if ($unit) {
            $size = $size * pow(1024, stripos('bkmgtpezy', $unit[0]));
        }

        return (int) abs(round($size));
    }
469268
public function fileSize($size) {
        if (!$size) {
            return '0 ' . $this->_sizes[0];
        }
        return round($size/pow(1024, ($i = floor(log($size, 1024)))), $i > 1 ? 2 : 0) .
            ' ' . $this->_sizes[$i];
    }
594526
public function setSize( $size ){
		if( !is_integer( $size ) )
			throw new InvalidArgumentException( 'Size must be an integer' );
		if( $size < 1 )
			throw new OutOfBoundsException( 'Size must be atleast 1 pixel' );
		if( $size > 512 )
			throw new OutOfBoundsException( 'Size must be atmost 512 pixels' );
		$this->size	= $size;
	}
635142
public function setPagination($offset, $size)
    {
        if (is_int($offset)) {
            $this->query->setFrom($offset);
        }

        if (is_int($size)) {
            $this->query->setSize($size);
        }

        return $this;
    }
472797
public function truncate(int $size): bool
    {
        $this->content->truncate($size);
        $this->lastModified = time();
        return true;
    }
675234
private function formatMemory($size)
    {
        $isNegative = false;
        $unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];

        if (0 > $size) {
            // This is a negative value
            $isNegative = true;
        }

        $return = ($isNegative) ? '-' : '';

        return $return
        .round(
            abs($size) / pow(1024, ($i = floor(log(abs($size), 1024)))), 2
        )
        .' '
        .$unit[$i];
    }
1194636
public function subtract(Size $size): Size
    {
        $result = $this->bit - $size->get('b');

        if (0 > $result) {
            throw new \OutOfBoundsException('Could not subtract, the result would be less than 0.');
        }

        $this->bit = $result;

        return $this;
    }
188899
public function fileSize($size, $decimals = 2)
    {

        switch (true)
        {
            case $size >= ((1 << 50) * 10):
                $postfix = 'PB';
                $size /= (1 << 50);
                break;

            case $size >= ((1 << 40) * 10):
                $postfix = 'TB';
                $size /= (1 << 40);
                break;

            case $size >= ((1 << 30) * 10):
                $postfix = 'GB';
                $size /= (1 << 30);
                break;

            case $size >= ((1 << 20) * 10):
                $postfix = 'MB';
                $size /= (1 << 20);
                break;

            case $size >= ((1 << 10) * 10):
                $postfix = 'KB';
                $size /= (1 << 10);
                break;

            default:
                $postfix = 'B';
        }

        return \round($size, $decimals) . ' ' . $postfix;
    }
3423
protected function getReadableTime($time, $percision = 3)
    {
        $unit = 's';
        if ($time < 1) {
            $time *= 1000;
            $percision = 0;
            $unit = 'ms';
        } elseif ($time > 60) {
            $time /= 60;
            $unit = 'm';
        }
        $time = number_format($time, $percision);
        return "{$time} {$unit}";
    }
1122650
protected static function pad($input, $size)
    {
        $input = str_pad(
            $input,
            Cuid::BASE36,
            '0',
            STR_PAD_LEFT
        );

        return substr($input, strlen($input) - $size);
    }
489675
public static function bytesFormat($size, $precision = 2)
    {
        $base = log($size) / log(1024);
        $suffix = array('', 'KB', 'MB', 'GB', 'TB');

        return round(pow(1024, $base - floor($base)), $precision) . $suffix[floor($base)];
    }
616642
public static function formatBytes($size, $precision = 0, $format = 'decimal')
    {
        if (!array_key_exists($format, self::$formats)) {
            throw new \InvalidArgumentException('Not a valid format');
        }

        $format = self::$formats[$format];
        $precision = (int) $precision;

        /** @var float $base */
        $base = log((float) $size, $format['mod']);
        $key = (int) floor($base);

        $value = round(pow($format['mod'], $base - floor($base)), $precision);

        return sprintf('%.' . $precision . 'f %s', $value, $format['units'][$key]);
    }
1067199
public function setWriteBuffer($size)
    {
        if (!is_resource($this->fp)) {
            return $this->raiseError('not connected');
        }

        $returned = stream_set_write_buffer($this->fp, $size);
        if ($returned === 0) {
            return true;
        }

        return $this->raiseError('Cannot set write buffer.');
    }
696412
function formatSize( $size ) {
		// For small sizes no decimal places necessary
		$round = 0;
		if ( $size > 1024 ) {
			$size = $size / 1024;
			if ( $size > 1024 ) {
				$size = $size / 1024;
				// For MB and bigger two decimal places are smarter
				$round = 2;
				if ( $size > 1024 ) {
					$size = $size / 1024;
					$msg = 'size-gigabytes';
				} else {
					$msg = 'size-megabytes';
				}
			} else {
				$msg = 'size-kilobytes';
			}
		} else {
			$msg = 'size-bytes';
		}
		$size = round( $size, $round );
		$text = $this->getMessageFromDB( $msg );
		return str_replace( '$1', $this->formatNum( $size ), $text );
	}