Back to Image class

Method prepareDimensions

protected \stdClass
prepareDimensions
(mixed $width, mixed $height, mixed $scaleMethod)
Method to get the new dimensions for a resized image.
Parameters
  • int $width The width of the resized image in pixels.
  • int $height The height of the resized image in pixels.
  • int $scaleMethod The method to use for scaling
Returns
  • \stdClass
Since
  • 2.5.0
-
  • \InvalidArgumentException If width, height or both given as zero
Class: Image
Project: Joomla

Method prepareDimensions - Source code

/**
 * Method to get the new dimensions for a resized image.
 *
 * @param   integer  $width        The width of the resized image in pixels.
 * @param   integer  $height       The height of the resized image in pixels.
 * @param   integer  $scaleMethod  The method to use for scaling
 *
 * @return  \stdClass
 *
 * @since   2.5.0
 * @throws  \InvalidArgumentException  If width, height or both given as zero
 */
protected function prepareDimensions($width, $height, $scaleMethod)
{
    // Instantiate variables.
    $dimensions = new \stdClass();
    switch ($scaleMethod) {
        case self::SCALE_FILL:
            $dimensions->width = (int) round($width);
            $dimensions->height = (int) round($height);
            break;
        case self::SCALE_INSIDE:
        case self::SCALE_OUTSIDE:
        case self::SCALE_FIT:
            $rx = $width > 0 ? $this->getWidth() / $width : 0;
            $ry = $height > 0 ? $this->getHeight() / $height : 0;
            if ($scaleMethod != self::SCALE_OUTSIDE) {
                $ratio = max($rx, $ry);
            } else {
                $ratio = min($rx, $ry);
            }
            $dimensions->width = (int) round($this->getWidth() / $ratio);
            $dimensions->height = (int) round($this->getHeight() / $ratio);
            break;
        default:
            throw new \InvalidArgumentException('Invalid scale method.');
    }
    return $dimensions;
}