Imbo\EventListener\ImageVariations::getMaxWidth PHP Метод

getMaxWidth() публичный Метод

Fetch the maximum width present in the set of transformations
public getMaxWidth ( integer $width, integer $height, array $transformations ) : array | null
$width integer The width of the existing image
$height integer The height of the existing image
$transformations array Transformations from the URL
Результат array | null Returns an array with a single element where the index is the index of the transformation that has the maximum width, and the value of the width
    public function getMaxWidth($width, $height, array $transformations)
    {
        // Possible widths to use
        $widths = [];
        // Extracts from the image
        $extracts = [];
        // Calculate the aspect ratio in case some transformations only specify height
        $ratio = $width / $height;
        foreach ($transformations as $i => $transformation) {
            $name = $transformation['name'];
            $params = $transformation['params'];
            if ($name === 'maxSize') {
                // MaxSize transformation
                if (isset($params['width'])) {
                    // width detected
                    $widths[$i] = (int) $params['width'];
                } else {
                    if (isset($params['height'])) {
                        // height detected, calculate ratio
                        $widths[$i] = (int) $params['height'] * $ratio;
                    }
                }
            } else {
                if ($name === 'resize') {
                    // Resize transformation
                    if (isset($params['width'])) {
                        // width detected
                        $widths[$i] = (int) $params['width'];
                    } else {
                        if (isset($params['height'])) {
                            // height detected, calculate ratio
                            $widths[$i] = (int) $params['height'] * $ratio;
                        }
                    }
                } else {
                    if ($name === 'thumbnail') {
                        // Thumbnail transformation
                        if (isset($params['width'])) {
                            // Width have been specified
                            $widths[$i] = (int) $params['width'];
                        } else {
                            if (isset($params['height']) && isset($params['fit']) && $params['fit'] === 'inset') {
                                // Height have been specified, and the fit mode is inset, calculate width
                                $widths[$i] = (int) $params['height'] * $ratio;
                            } else {
                                // No width or height/inset fit combo. Use default width for thumbnails
                                $widths[$i] = 50;
                            }
                        }
                    } else {
                        if ($name === 'crop' && empty($widths)) {
                            // Crop transformation
                            $extracts[$i] = $params;
                        }
                    }
                }
            }
        }
        if ($widths && !empty($extracts)) {
            // If we are fetching extracts, we need a larger version of the image
            $extract = reset($extracts);
            // Find the correct scaling factor for the extract
            $extractFactor = $width / $extract['width'];
            $maxWidth = max($widths);
            // Find the new max width
            $maxWidth = $maxWidth * $extractFactor;
            return [key($extracts) => $maxWidth];
        }
        if ($widths) {
            // Find the max width in the set, and return it along with the index of the
            // transformation that first referenced it
            $maxWidth = max($widths);
            return [array_search($maxWidth, $widths) => $maxWidth];
        }
        return null;
    }

Usage Example

Пример #1
0
 /**
  * @dataProvider getTransformations
  */
 public function testCanGetTheMinWidthFromASetOfTransformations($width, $height, array $transformations, $maxWidth)
 {
     $calculatedMax = $this->listener->getMaxWidth($width, $height, $transformations);
     $this->assertSame(is_null($maxWidth) ? null : array_map('intval', $maxWidth), is_null($calculatedMax) ? null : array_map('intval', $calculatedMax), 'Could not figure out the minimum width');
 }