/** * Normalizes the given dimensions. If width or height is set to 'AUTO', we calculate the missing dimension. * * @param int|string $width * @param int|string $height * * @throws Exception */ private function _normalizeDimensions(&$width, &$height = null) { if (preg_match('/^(?P<width>[0-9]+|AUTO)x(?P<height>[0-9]+|AUTO)/', $width, $matches)) { $width = $matches['width'] != 'AUTO' ? $matches['width'] : null; $height = $matches['height'] != 'AUTO' ? $matches['height'] : null; } if (!$height || !$width) { list($width, $height) = ImageHelper::calculateMissingDimension($width, $height, $this->getWidth(), $this->getHeight()); } }
/** * Return a dimension of the image. * * @param $dimension 'height' or 'width' * @param $transform * * @return null|float|mixed */ private function _getDimension($dimension, $transform) { if ($this->kind != 'image') { return null; } if ($transform === null && isset($this->_transform)) { $transform = $this->_transform; } if (!$transform) { return parent::getAttribute($dimension); } $transform = craft()->assetTransforms->normalizeTransform($transform); $dimensions = array('width' => $transform->width, 'height' => $transform->height); if (!$transform->width || !$transform->height) { // Fill in the blank $dimensionArray = ImageHelper::calculateMissingDimension($dimensions['width'], $dimensions['height'], $this->_getWidth(), $this->_getHeight()); $dimensions['width'] = (int) $dimensionArray[0]; $dimensions['height'] = (int) $dimensionArray[1]; } // Special case for 'fit' since that's the only one whose dimensions vary from the transform dimensions if ($transform->mode == 'fit') { $factor = max($this->_getWidth() / $dimensions['width'], $this->_getHeight() / $dimensions['height']); $dimensions['width'] = (int) round($this->_getWidth() / $factor); $dimensions['height'] = (int) round($this->_getHeight() / $factor); } return $dimensions[$dimension]; }
private function _resizeImage(&$image, $width, $height) { // Calculate the missing width/height for the asset - ensure aspect ratio is maintained $dimensions = ImageHelper::calculateMissingDimension($width, $height, $image->getWidth(), $image->getHeight()); $image->resize($dimensions[0], $dimensions[1]); }