Skip to content

Instantly share code, notes, and snippets.

@ursbraem
Created March 19, 2014 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ursbraem/9645542 to your computer and use it in GitHub Desktop.
Save ursbraem/9645542 to your computer and use it in GitHub Desktop.
Viewhelper for showing the size of a file in TYPO3 fluid
<?php
/* *
* This script belongs to the FLOW3 package "Fluid". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation, either version 3 of the License, or (at your *
* option) any later version. *
* *
* This script is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
* TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
* General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with the script. *
* If not, see http://www.gnu.org/licenses/lgpl.html *
* *
* The TYPO3 project - inspiring people to share! *
* */
/**
* View helper for outputting the formatted size of a file
*
* @version $Id$
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
*
*/
class Tx_Myext_ViewHelpers_FilesizeViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
protected $units = array('b', 'k', 'm', 'g', 't', 'e', 'z', 'y');
/**
* Returns the formatted filesize
*
* @param string $path
* @return string The filesize with SI unit appended
* @author Urs Braem <ub@sturmundbraem.ch>, original author of filesize vh: Thomas "Thasmo" Deinhamer <thasmo@gmail.com>
* @api
*/
public function render($path) {
$unit = NULL; // The SI unit suffix to use
$threshold = 0; // The threshold of the final filesize used to force the next SI unit
$decimals = 1; // The maximum number of decimal places to show
$decimalSeparator = '.'; // The symbol used as decimal separator
$thousandsSeparator = '\''; // The symbol used as thousands separator
$size = filesize($path);
# determine the quotient for the calculation
# decimal system only, as KiB is not understood by users
$quotient = 1000;
# determine the valid threshold
$threshold = min(max(0, $threshold), $quotient);
# reset the units
reset($this->units);
# determine the unit and calculate the final size
while(
($size >= $quotient && is_null($unit)) || # if size is still greater than the quotient
(current($this->units) != strtolower($unit) && !is_null($unit)) || # or the SI unit is manually defined and reached
($threshold > 0 && $size > $threshold && is_null($unit)) # or the threshold forces to use the next unit
) {
$size /= $quotient;
next($this->units);
}
# get the SI unit
$unit = current($this->units);
$unit = strtoupper($unit) . ($unit != 'b' ? 'B' : '');
# format the final size
$size = number_format(round($size, $decimals), max(0, $decimals), $decimalSeparator, $thousandsSeparator);
# append the unit to the final size
return implode(' ', array($size, $unit));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment