Пример #1
0
 /**
  * Loads an image from a file system path, do transform, return transformed image as an Imager_ImageModel
  *
  * @param Imager_ImagePathsModel $paths
  * @param Array $transform
  *
  * @throws Exception
  * @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;
 }
Пример #2
0
 /**
  * Loads an image from a file system path, do transform, return transformed image as an Imager_ImageModel
  *
  * @param Imager_ImagePathsModel $paths
  * @param Array $transform
  *
  * @throws Exception
  * @return Imager_ImageModel
  */
 private function _getTransformedImage($paths, $transform)
 {
     // break up the image filename to get extension and actual filename
     $pathParts = pathinfo($paths->targetFilename);
     $sourceExtension = $targetExtension = $pathParts['extension'];
     $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);
     // 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) || IOHelper::getLastTimeModified($targetFilePath)->format('U') + $this->getSetting('cacheDuration') < 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, false, $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($transform);
         }
         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']);
             }
         }
         if (isset($transform['watermark'])) {
             $this->_applyWatermark($this->imageInstance, $transform['watermark']);
         }
         if ($sourceExtension != $targetExtension && $sourceExtension != 'jpg' && $targetExtension == 'jpg' && $this->getSetting('bgColor', $transform) != '') {
             $this->_applyBackgroundColor($this->imageInstance, $this->getSetting('bgColor', $transform));
         }
         $this->imageInstance->save($targetFilePath, $saveOptions);
         // if file was created, check if optimization should be done
         if (IOHelper::fileExists($targetFilePath)) {
             // todo : move these into tasks
             if ($targetExtension == 'jpg' || $targetExtension == 'jpeg') {
                 if ($this->getSetting('jpegoptimEnabled', $transform)) {
                     $this->runJpegoptim($targetFilePath, $transform);
                 }
                 if ($this->getSetting('jpegtranEnabled', $transform)) {
                     $this->runJpegtran($targetFilePath, $transform);
                 }
             }
             if ($targetExtension == 'png' && $this->getSetting('optipngEnabled', $transform)) {
                 $this->runOptipng($targetFilePath, $transform);
             }
             if ($this->getSetting('tinyPngEnabled', $transform)) {
                 $this->runTinyPng($targetFilePath, $transform);
             }
             // todo : make sure this is done after task has been run too
             if ($this->getSetting('awsEnabled')) {
                 $this->_uploadToAWS($targetFilePath);
             }
         }
     }
     $imageInfo = @getimagesize($targetFilePath);
     $imagerImage = new Imager_ImageModel();
     $imagerImage->url = $targetFileUrl;
     $imagerImage->width = $imageInfo[0];
     $imagerImage->height = $imageInfo[1];
     return $imagerImage;
 }
Пример #3
0
 /**
  * Loads an image from a file system path, do transform, return transformed image as an Imager_ImageModel
  *
  * @param AssetFileModel $image
  * @param string $sourcePath
  * @param string $targetPath
  * @param string $targetUrl
  * @param Array $transform
  *
  * @throws Exception
  * @return Imager_ImageModel
  */
 private function _getTransformedImage($imageFilename, $sourcePath, $targetPath, $targetUrl, $transform)
 {
     // break up the image filename to get extension and actual filename
     $pathParts = pathinfo($imageFilename);
     $targetExtension = $pathParts['extension'];
     $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);
     // create target filename, path and url
     $targetFilename = $this->_createTargetFilename($filename, $targetExtension, $transform);
     $targetFilePath = $targetPath . $targetFilename;
     $targetFileUrl = $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) || IOHelper::getLastTimeModified($targetFilePath)->format('U') + $this->getSetting('cacheDuration') < 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($sourcePath . $imageFilename);
         }
         // Apply any pre resize filters
         if (isset($transform['preEffects'])) {
             $this->_applyImageEffects($this->imageInstance, $transform['preEffects']);
         }
         $originalSize = $this->imageInstance->getSize();
         $cropSize = $this->_getCropSize($originalSize, $transform);
         $resizeSize = $this->_getResizeSize($originalSize, $transform);
         $saveOptions = $this->_getSaveOptions($targetExtension, $transform);
         $filterMethod = $this->_getFilterMethod($transform);
         if (!isset($transform['mode']) || mb_strtolower($transform['mode']) == 'crop' || mb_strtolower($transform['mode']) == 'croponly') {
             $cropPoint = $this->_getCropPoint($resizeSize, $cropSize, $transform);
             $this->imageInstance->resize($resizeSize, $filterMethod)->crop($cropPoint, $cropSize);
         } else {
             $this->imageInstance->resize($resizeSize, $filterMethod);
         }
         // Apply post resize filters
         if (isset($transform['effects'])) {
             $this->_applyImageEffects($this->imageInstance, $transform['effects']);
         }
         $this->imageInstance->save($targetFilePath, $saveOptions);
         // if file was created, check if optimization should be done
         if (IOHelper::fileExists($targetFilePath)) {
             if (($targetExtension == 'jpg' || $targetExtension == 'jpeg') && $this->getSetting('jpegoptimEnabled', $transform)) {
                 $this->runJpegoptim($targetFilePath, $transform);
             }
             if ($targetExtension == 'png' && $this->getSetting('optipngEnabled', $transform)) {
                 $this->runOptipng($targetFilePath, $transform);
             }
             if ($this->getSetting('tinyPngEnabled', $transform)) {
                 $this->runTinyPng($targetFilePath, $transform);
             }
         }
     }
     $imageInfo = @getimagesize($targetFilePath);
     $imagerImage = new Imager_ImageModel();
     $imagerImage->url = $targetFileUrl;
     $imagerImage->width = $imageInfo[0];
     $imagerImage->height = $imageInfo[1];
     return $imagerImage;
 }
 protected function updateAsset($asset, $image, $path)
 {
     // Update our model
     $asset->size = IOHelper::getFileSize($path);
     $asset->width = $image->getWidth();
     $asset->height = $image->getHeight();
     // Then, make sure we update the asset info as stored in the database
     $fileRecord = AssetFileRecord::model()->findById($asset->id);
     $fileRecord->size = $asset->size;
     $fileRecord->width = $asset->width;
     $fileRecord->height = $asset->height;
     $fileRecord->dateModified = IOHelper::getLastTimeModified($path);
     $fileRecord->save(false);
 }