Skip to content

Commit e77ba26

Browse files
Size helper in Medias
1 parent 4a41e44 commit e77ba26

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/Eloquent/Media.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,17 @@ public function download(): ?string
3939
->forMedia($this)
4040
->download();
4141
}
42+
43+
public function size($precision = 2): ?string
44+
{
45+
if ($this->size > 0) {
46+
$size = (int) $this->size;
47+
$base = log($size) / log(1024);
48+
$suffixes = array(' bytes', ' KB', ' MB', ' GB', ' TB');
49+
50+
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
51+
} else {
52+
return $this->size;
53+
}
54+
}
4255
}

0 commit comments

Comments
 (0)