Back to Image class

Method sanitizeWidth

protected int
sanitizeWidth
(mixed $width, mixed $height)
Method to sanitize a width value.
Parameters
  • mixed $width The input width value to sanitize.
  • mixed $height The input height value for reference.
Returns
  • int
Since
  • 2.5.0
Class: Image
Project: Joomla

Method sanitizeWidth - Source code

/**
 * Method to sanitize a width value.
 *
 * @param   mixed  $width   The input width value to sanitize.
 * @param   mixed  $height  The input height value for reference.
 *
 * @return  integer
 *
 * @since   2.5.0
 */
protected function sanitizeWidth($width, $height)
{
    // If no width was given we will assume it is a square and use the height.
    $width = $width === null ? $height : $width;
    // If we were given a percentage, calculate the integer value.
    if (preg_match('/^[0-9]+(\\.[0-9]+)?\\%$/', $width)) {
        $width = (int) round($this->getWidth() * (float) str_replace('%', '', $width) / 100);
    } else {
        // Else do some rounding so we come out with a sane integer value.
        $width = (int) round((float) $width);
    }
    return $width;
}