public static function scaleDimensions(array &$dimensions, $width = NULL, $height = NULL, $upscale = FALSE) {
    $aspect = $dimensions['height'] / $dimensions['width'];

    // Calculate one of the dimensions from the other target dimension,
    // ensuring the same aspect ratio as the source dimensions. If one of the
    // target dimensions is missing, that is the one that is calculated. If both
    // are specified then the dimension calculated is the one that would not be
    // calculated to be bigger than its target.
    if (($width && !$height) || ($width && $height && $aspect < $height / $width)) {
      $height = (int) round($width * $aspect);
    }
    else {
      $width = (int) round($height / $aspect);
    }

    // Don't upscale if the option isn't enabled.
    if (!$upscale && ($width >= $dimensions['width'] || $height >= $dimensions['height'])) {
      return FALSE;
    }

    $dimensions['width'] = $width;
    $dimensions['height'] = $height;
    return TRUE;
  }