Craft\ImagerService::_getTransformedImage PHP Method

_getTransformedImage() private method

Loads an image from a file system path, do transform, return transformed image as an Imager_ImageModel
private _getTransformedImage ( Imager_ImagePathsModel $paths, Array $transform ) : Imager_ImageModel
$paths Imager_ImagePathsModel
$transform Array
return Imager_ImageModel
    private function _getTransformedImage($paths, $transform)
    {
        // break up the image filename to get extension and actual filename
        $pathParts = pathinfo($paths->targetFilename);
        if (isset($pathParts['extension'])) {
            $sourceExtension = $targetExtension = $pathParts['extension'];
        } else {
            $sourceExtension = $targetExtension = FileHelper::getExtensionByMimeType(mime_content_type($paths->sourcePath . $paths->sourceFilename));
        }
        $filename = $pathParts['filename'];
        // do we want to output in a certain format?
        if (isset($transform['format'])) {
            $targetExtension = $transform['format'];
        }
        // normalize the transform before doing anything more
        $transform = $this->_normalizeTransform($transform, $paths);
        // create target filename, path and url
        $targetFilename = $this->_createTargetFilename($filename, $targetExtension, $transform);
        $targetFilePath = $paths->targetPath . $targetFilename;
        $targetFileUrl = $paths->targetUrl . $targetFilename;
        /**
         * Check if the image already exists, if caching is turned on or if the cache has expired.
         */
        if (!$this->getSetting('cacheEnabled', $transform) || !IOHelper::fileExists($targetFilePath) || $this->getSetting('cacheDuration', $transform) !== false && IOHelper::getLastTimeModified($targetFilePath)->format('U') + $this->getSetting('cacheDuration', $transform) < time()) {
            // create the imageInstance. only once if reuse is enabled, or always
            if (!$this->getSetting('instanceReuseEnabled', $transform) || $this->imageInstance == null) {
                $this->imageInstance = $this->imagineInstance->open($paths->sourcePath . $paths->sourceFilename);
            }
            // Apply any pre resize filters
            if (isset($transform['preEffects'])) {
                $this->_applyImageEffects($this->imageInstance, $transform['preEffects']);
            }
            // Do the resize
            $originalSize = $this->imageInstance->getSize();
            $cropSize = $this->getCropSize($originalSize, $transform);
            $resizeSize = $this->getResizeSize($originalSize, $transform);
            $saveOptions = $this->_getSaveOptions($targetExtension, $transform);
            $filterMethod = $this->_getFilterMethod($transform);
            if ($this->imageDriver == 'imagick' && $this->getSetting('smartResizeEnabled', $transform) && version_compare(craft()->getVersion(), '2.5', '>=')) {
                $this->imageInstance->smartResize($resizeSize, (bool) craft()->config->get('preserveImageColorProfiles'), $this->getSetting('jpegQuality', $transform));
            } else {
                $this->imageInstance->resize($resizeSize, $filterMethod);
            }
            // If Image Driver is imagick and removeMetadata is true
            // remove Metadata to reduce the image size by a significant amount
            if ($this->imageDriver == 'imagick' && $this->getSetting('removeMetadata', $transform)) {
                $this->imageInstance->strip();
            }
            if (!isset($transform['mode']) || mb_strtolower($transform['mode']) == 'crop' || mb_strtolower($transform['mode']) == 'croponly') {
                $cropPoint = $this->_getCropPoint($resizeSize, $cropSize, $transform);
                $this->imageInstance->crop($cropPoint, $cropSize);
            }
            // letterbox, add padding
            if (isset($transform['mode']) && mb_strtolower($transform['mode']) == 'letterbox') {
                $this->_applyLetterbox($this->imageInstance, $transform);
            }
            // Apply post resize filters
            if (isset($transform['effects'])) {
                $this->_applyImageEffects($this->imageInstance, $transform['effects']);
            }
            // Interlace if true
            if ($this->getSetting('interlace', $transform)) {
                $interlaceVal = $this->getSetting('interlace', $transform);
                if (is_string($interlaceVal)) {
                    $this->imageInstance->interlace(ImagerService::$interlaceKeyTranslate[$interlaceVal]);
                } else {
                    $this->imageInstance->interlace(ImagerService::$interlaceKeyTranslate['line']);
                }
            }
            // apply watermark if enabled
            if (isset($transform['watermark'])) {
                $this->_applyWatermark($this->imageInstance, $transform['watermark']);
            }
            // apply background color if enabled and applicable
            if ($sourceExtension != $targetExtension && $sourceExtension != 'jpg' && $targetExtension == 'jpg' && $this->getSetting('bgColor', $transform) != '') {
                $this->_applyBackgroundColor($this->imageInstance, $this->getSetting('bgColor', $transform));
            }
            // save the transform
            if ($targetExtension === 'webp') {
                if ($this->hasSupportForWebP()) {
                    $this->_saveAsWebp($this->imageInstance, $targetFilePath, $sourceExtension, $saveOptions);
                } else {
                    throw new Exception(Craft::t('This version of {imageDriver} does not support the webp format. You should use “craft.imager.serverSupportsWebp” in your templates to test for it.', array('imageDriver' => $this->imageDriver == 'gd' ? 'GD' : 'Imagick')));
                }
            } else {
                $this->imageInstance->save($targetFilePath, $saveOptions);
            }
            // if file was created, check if optimization should be done
            if (IOHelper::fileExists($targetFilePath)) {
                if ($targetExtension == 'jpg' || $targetExtension == 'jpeg') {
                    if ($this->getSetting('jpegoptimEnabled', $transform)) {
                        $this->postOptimize('jpegoptim', $targetFilePath);
                    }
                    if ($this->getSetting('jpegtranEnabled', $transform)) {
                        $this->postOptimize('jpegtran', $targetFilePath);
                    }
                    if ($this->getSetting('mozjpegEnabled', $transform)) {
                        $this->postOptimize('mozjpeg', $targetFilePath);
                    }
                }
                if ($targetExtension == 'png' && $this->getSetting('optipngEnabled', $transform)) {
                    $this->postOptimize('optipng', $targetFilePath);
                }
                if ($this->getSetting('tinyPngEnabled', $transform)) {
                    $this->postOptimize('tinypng', $targetFilePath);
                }
                // Upload to AWS if enabled
                if ($this->getSetting('awsEnabled')) {
                    craft()->imager_aws->uploadToAWS($targetFilePath);
                    // Invalidate cloudfront distribution if enabled
                    if ($this->getSetting('cloudfrontInvalidateEnabled')) {
                        $parsedUrl = parse_url($targetFileUrl);
                        $this->invalidatePaths[] = $parsedUrl['path'];
                    }
                }
            }
        }
        // create Imager_ImageModel for transformed image
        $imagerImage = new Imager_ImageModel($targetFilePath, $targetFileUrl, $paths, $transform);
        return $imagerImage;
    }