Get vars for animated gif frames setup

@param LayersInterface|array $layers
@param array                 $transform

@return array
188387
private function getFramesVars($layers, $transform): array
    {
        $startFrame = 0;
        $endFrame = \count($layers) - 1;
        $interval = 1;

        if (isset($transform['frames'])) {
            $framesIntArr = explode('@', $transform['frames']);

            if (\count($framesIntArr) > 1) {
                $interval = $framesIntArr[1];
            }

            $framesArr = explode('-', $framesIntArr[0]);

            if (\count($framesArr) > 1) {
                $startFrame = $framesArr[0];
                if ($framesArr[1] !== '*') {
                    $endFrame = $framesArr[1];
                }
            } else {
                $startFrame = $endFrame = $framesArr[0];
            }

            if ($endFrame > \count($layers) - 1) {
                $endFrame = \count($layers) - 1;
            }
        }

        return [$startFrame, $endFrame, $interval];
    }
1218722
public static function normalizeAssetTransformToObject($assetTransform): array
    {
        $transformArray = $assetTransform->toArray();
        $validParams = ['width', 'height', 'format', 'mode', 'position', 'interlace', 'quality'];

        $r = [];

        foreach ($validParams as $param) {
            if (isset($transformArray[$param])) {
                $r[$param] = $transformArray[$param];
            }
        }

        return $r;
    }
139495
public function generateRouterData(array $routes, ?callable $transform = null): array
    {
        $static = [];
        $dynamic = [];
        
        foreach ($routes as $route) {
            $compiled = $this->compileRoute($route);
            
            if ($compiled[self::KEY_REGEX] === '') {
                $prefix = $compiled[self::KEY_PREFIX];
                $route = $compiled[self::KEY_ROUTE];
                
                if (isset($static[$prefix])) {
                    if (\is_array($static[$prefix])) {
                        $static[$prefix][] = $route;
                    } else {
                        $static[$prefix] = [
                            $static[$prefix],
                            $route
                        ];
                    }
                } else {
                    $static[$prefix] = $route;
                }
            } else {
                $dynamic[] = $compiled;
            }
        }
        
        \usort($dynamic, static function (array $a, array $b): int {
            $p = ($b[self::KEY_PRIORITY] <=> $a[self::KEY_PRIORITY]);
            
            if ($p !== 0) {
                return $p;
            }
            
            $p = \strcmp($a[self::KEY_PREFIX], $b[self::KEY_PREFIX]);
            
            if ($p !== 0) {
                return $p;
            }
            
            return \strcmp($a[self::KEY_REGEX], $b[self::KEY_REGEX]);
        });
        
        $matches = \array_column($dynamic, 0);
        $grouped = [];
        
        $regex = "'^(?";
        
        for ($len = \count($dynamic), $i = 0; $i < $len; $i++) {
            if (isset($dynamic[$i + 1]) && $dynamic[$i + 1][self::KEY_PREFIX] == $dynamic[$i][self::KEY_PREFIX]) {
                $regex .= '|' . \preg_quote($dynamic[$i][self::KEY_PREFIX], "'") . '(?';
                
                $tmp = [
                    $dynamic[$i][self::KEY_REGEX] => [
                        $i => $dynamic[$i][self::KEY_PRIORITY]
                    ]
                ];
                
                do {
                    $i++;
                    
                    $tmp[$dynamic[$i][self::KEY_REGEX]][$i] = $dynamic[$i][self::KEY_PRIORITY];
                } while (isset($dynamic[$i + 1]) && $dynamic[$i + 1][self::KEY_PREFIX] == $dynamic[$i][self::KEY_PREFIX]);
                
                foreach ($tmp as & $t) {
                    \arsort($t, \SORT_NUMERIC);
                }
                
                foreach ($tmp as $k => $v) {
                    $regex .= '|' . $k . '(*MARK:G' . \count($grouped) . ')';
                    $grouped[] = \count($v);
                    
                    if ($transform) {
                        $grouped = \array_merge($grouped, \array_map(static function (int $i) use ($dynamic, $transform) {
                            return $transform($dynamic[$i][self::KEY_ROUTE], $i);
                        }, \array_keys($v)));
                    } else {
                        $grouped = \array_merge($grouped, \array_keys($v));
                    }
                }
                
                $regex .= ')';
            } else {
                $regex .= '|' . \preg_quote($dynamic[$i][self::KEY_PREFIX], "'") . $dynamic[$i][self::KEY_REGEX] . '(*MARK:R' . $i . ')';
            }
        }
        
        $regex .= ")$'i";
        
        return [
            $static,
            $regex,
            $matches,
            $grouped,
            $routes
        ];
    }
203735
public function prepareTransform($transform)
    {
        // it can not be  empty
        if (!$transform) {
            return null;
        }

        // sections must be like scale(...)
        if (!preg_match_all('/([a-z]+)\(([^\)]*)\)/isU', $transform, $match)) {
            return null;
        }

        // prepare the list of the actions
        $actions = array();

        // for actions
        $amountMatches = count($match[0]);
        for ($k=0; $k < $amountMatches; $k++) {
            // get the name of the action
            $name = strtolower($match[1][$k]);

            // get the parameters of the action
            $values = [];
            $string = trim($match[2][$k]);
            if ($string !== '') {
                $values = explode(',', $string);
            }
            foreach ($values as $key => $value) {
                $value = trim($value);
                if ($value === '') {
                    unset($values[$key]);
                    continue;
                }

                $values[$key] = $value;
            }

            // prepare the matrix, depending on the action
            switch ($name) {
                case 'scale':
                    if (!array_key_exists(0, $values)) {
                        $values[0] = 1.;
                    }

                    if (!array_key_exists(1, $values)) {
                        $values[1] = $values[0];
                    }

                    $values[0] = floatval($values[0]);
                    $values[1] = floatval($values[1]);

                    $actions[] = array($values[0],0.,0.,$values[1],0.,0.);
                    break;

                case 'translate':
                    if (!array_key_exists(0, $values)) {
                        $values[0] = 0.;
                    }

                    if (!array_key_exists(1, $values)) {
                        $values[1] = 0.;
                    }

                    $values[0] = $this->cssConverter->convertToMM($values[0], $this->getProperty('w'));
                    $values[1] = $this->cssConverter->convertToMM($values[1], $this->getProperty('h'));

                    $actions[] = array(1.,0.,0.,1.,$values[0],$values[1]);
                    break;

                case 'rotate':
                    if (!array_key_exists(0, $values)) {
                        $values[0] = 0.;
                    }
                    if (!array_key_exists(1, $values)) {
                        $values[1] = 0.;
                    }
                    if (!array_key_exists(2, $values)) {
                        $values[2] = 0.;
                    }

                    $values[0] = $values[0]*M_PI/180.;
                    $values[1] = $this->cssConverter->convertToMM($values[1], $this->getProperty('w'));
                    $values[2] = $this->cssConverter->convertToMM($values[2], $this->getProperty('h'));

                    if ($values[1] || $values[2]) {
                        $actions[] = array(1.,0.,0.,1.,-$values[1],-$values[2]);
                    }

                    $actions[] = array(cos($values[0]),sin($values[0]),-sin($values[0]),cos($values[0]),0.,0.);

                    if ($values[1] || $values[2]) {
                        $actions[] = array(1.,0.,0.,1.,$values[1],$values[2]);
                    }
                    break;

                case 'skewx':
                    if (!array_key_exists(0, $values)) {
                        $values[0] = 0.;
                    }

                    $values[0] = $values[0]*M_PI/180.;

                    $actions[] = array(1.,0.,tan($values[0]),1.,0.,0.);
                    break;

                case 'skewy':
                    if (!array_key_exists(0, $values)) {
                        $values[0] = 0.;
                    }

                    $values[0] = $values[0]*M_PI/180.;

                    $actions[] = array(1.,tan($values[0]),0.,1.,0.,0.);
                    break;

                case 'matrix':
                    if (!array_key_exists(0, $values)) {
                        $values[0] = 0.;
                    }

                    if (!array_key_exists(1, $values)) {
                        $values[1] = 0.;
                    }

                    if (!array_key_exists(2, $values)) {
                        $values[2] = 0.;
                    }

                    if (!array_key_exists(3, $values)) {
                        $values[3] = 0.;
                    }

                    if (!array_key_exists(4, $values)) {
                        $values[4] = 0.;
                    }

                    if (!array_key_exists(5, $values)) {
                        $values[5] = 0.;
                    }

                    $values[0] = floatval($values[0]);
                    $values[1] = floatval($values[1]);
                    $values[2] = floatval($values[2]);
                    $values[3] = floatval($values[3]);
                    $values[4] = $this->cssConverter->convertToMM($values[4], $this->getProperty('w'));
                    $values[5] = $this->cssConverter->convertToMM($values[5], $this->getProperty('h'));

                    $actions[] = $values;
                    break;
            }
        }

        // if there are no actions => return
        if (!$actions) {
            return null;
        }

        // get the first matrix
        $m = $actions[0];
        unset($actions[0]);

        // foreach matrix => multiply to the last matrix
        foreach ($actions as $n) {
            $m = array(
                $m[0]*$n[0]+$m[2]*$n[1],
                $m[1]*$n[0]+$m[3]*$n[1],
                $m[0]*$n[2]+$m[2]*$n[3],
                $m[1]*$n[2]+$m[3]*$n[3],
                $m[0]*$n[4]+$m[2]*$n[5]+$m[4],
                $m[1]*$n[4]+$m[3]*$n[5]+$m[5]
            );
        }

        // return the matrix
        return $m;
    }
445708
private function encodeLayersInformation($layers, Encoder $encoder)
    {
        $layers = array_map(
            function ($layer) use ($encoder) {
                return $encoder->encodeReference($layer);
            },
            $layers
        );

        return '[' . implode(', ', $layers) . ']';
    }
1189396
function transformLayer(ctx, iCanvas, layer) {
    var m = layer.transform.matrix();

    ctx.translate(iCanvas.width / 2, iCanvas.height / 2);
    ctx.transform(m[0], m[1], m[3], m[4], m[6], m[7]);
    if (layer.flip_h || layer.flip_v) {
        ctx.scale(layer.flip_h ? -1 : 1, layer.flip_v ? -1 : 1);
    }
    ctx.translate(-layer.img.width / 2, -layer.img.height / 2);
}
388250
private function getFilterMethod($transform): string
    {
        /** @var ConfigModel $settings */
        $config = ImagerService::getConfig();

        return ImagerService::$imageDriver === 'imagick' ? ImagerService::$filterKeyTranslate[(string)$config->getSetting('resizeFilter', $transform)] : ImageInterface::FILTER_UNDEFINED;
    }
884974
private function getTweetsVars($tweets, $vars)
    {
        /** @var string $firstTweetId */
        $firstTweetId = $tweets[0]->getId();

        $vars['previous'] = $this->tweetRepository
            ->getPreviousTweetId($firstTweetId);
        $vars['next'] = $this->tweetRepository
            ->getNextTweetId($firstTweetId);

        // Only update the cookie if the last Tweet Id is bigger than
        //  the one in the cookie
        if ($firstTweetId > $vars['cookieId']) {
            $vars['cookie'] = $this->createCookie($firstTweetId);
            $vars['cookieId'] = $firstTweetId;
        }

        $vars['number'] = $this->tweetRepository
            ->countPendingTweets($vars['cookieId']);

        $vars['first'] = $firstTweetId;

        return $vars;
    }
1078845
protected function getVariables($matches)
    {
        $variables = array();
        
        if (!is_array($matches)) {
            return $variables;
        }
        
        array_shift($matches);
        
        foreach ($matches as $path) {
            $variables = array_merge($variables, explode('/', $path));
        }
        
        foreach ($variables as $i => $variable) {
            if (!$variable) {
                unset($variables[$i]);
            }
        }
        
        return array_values($variables);
    }
981736
public static function getVariables($matches, &$vars = null)
    {
        if (!$matches) {
            return [];
        }

        if ($vars === null) {
            $vars = [];
        }

        foreach ($matches as $i => $var) {
            if (is_numeric($i)) {
                continue;
            }

            if (strpos((string) $var, '/') !== false) {
                $var = explode('/', (string) $var);
            }

            $vars[$i] = $var;
        }

        return $vars;
    }
36084
public function transformToResourceIdentifier(ResourceTransformation $transformation): ?array
    {
        if ($transformation->object === null || $transformation->resource === null) {
            return null;
        }

        $transformation->resource->initializeTransformation(
            $transformation->request,
            $transformation->object,
            $transformation->exceptionFactory
        );

        $this->transformResourceIdentifier($transformation);

        $transformation->resource->clearTransformation();

        return $transformation->result;
    }
733158
public function transformImage ($asset = null, $transforms = null, $defaultOptions = [])
    {
        return Imgix::$plugin->imgixService->transformImage($asset, $transforms, $defaultOptions);
    }
234496
public function overlayOnMp4($source, $layer, $target)
    {
        if (!is_array($layer)) {
            $layer = array($layer);
        }
        $overlay = '';
        $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
       
        $ffmpeg = $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
            ->addPreInputCommand('-y')
            ->addCommand('-i', $source, true);

        foreach ($layer as $k => $l) {
            $ffmpeg->addCommand('-i', $l, true);
            $overlay .= 'overlay=format=rgb';
            if ($k<count($layer)-1) {
                $overlay .= ',';
            }
        }

        $ffmpeg->addCommand('-filter_complex', $overlay.'[out]')
            ->addCommand('-map', '[out]')
            ->addCommand('-vcodec', 'qtrle')
            ->setOutputPath($target)
            ->execute();

        return $target;
    }
353354
private function getArtifactPath(Transformation $transformation)
    {
        return $transformation->getArtifact()
            ? $transformation->getTransformer()->getTarget() . DIRECTORY_SEPARATOR . $transformation->getArtifact()
            : null;
    }
1025938
private function ensurePreviewVars(array $vars, array $uriVars)
    {
        if (! is_user_logged_in()) {
            return $vars;
        }

        foreach (['preview', 'preview_id', 'preview_nonce'] as $var) {
            if (! isset($vars[$var]) && isset($uriVars[$var])) {
                $vars[$var] = $uriVars[$var];
            }
        }

        return $vars;
    }
645769
private function buildTransform($filename, $transform)
    {
        $parameters = $this->translateAttributes($transform);

        return $this->builder->createURL($filename, $parameters);
    }
1031772
public function resolveBindings(array $vars)
    {
        // First check if the route $vars as a whole matches any registered composite binding
        if (count($vars) > 1 && !empty($this->compositeBindings)) {
            if ($r = $this->resolveCompositeBinding($vars)) {
                return $r;
            }
        }

        // If no composite binding found, check for explicit and implicit bindings
        if (!empty($this->implicitBindings) || !empty($this->bindings)) {
            foreach ($vars as $var => $value) {
                $vars[$var] = $this->resolveBinding($var, $value);
            }
        }

        return $vars;
    }