Ejemplo n.º 1
0
 /**
  * Do an image transform
  *
  * @param AssetFileModel|string $image
  * @param AssetFileModel $transform
  *
  * @throws Exception
  * @return Image
  */
 public function transformImage($image, $transform, $configOverrides)
 {
     if (!$image) {
         return null;
         // there's nothing to see here, move along.
     }
     $this->configModel = new Imager_ConfigModel($configOverrides);
     $this->_createImagineInstance();
     if (is_string($image)) {
         // ok, so it's not an AssetFileModel. What is it then?
         $imageString = str_replace($this->getSetting('imagerUrl'), '', $image);
         if (strrpos($imageString, 'http') !== false) {
             // todo : download remote file and proceed
             throw new Exception(Craft::t('External urls are not yet supported.'));
         } else {
             $pathParts = pathinfo($imageString);
             $sourcePath = $this->getSetting('imagerSystemPath') . $pathParts['dirname'] . '/';
             $targetPath = $this->getSetting('imagerSystemPath') . $pathParts['dirname'] . '/';
             $targetUrl = $this->getSetting('imagerUrl') . $pathParts['dirname'] . '/';
             $imageFilename = $pathParts['basename'];
         }
     } else {
         // This is an AssetsFileModel! That's great.
         // todo : But only local sources are supported for now.
         if ($image->getSource()->type != 'Local') {
             throw new Exception(Craft::t('Only local asset sources are supported for now'));
         }
         $sourcePath = craft()->config->parseEnvironmentString($image->getSource()->settings['path']) . $image->getFolder()->path;
         $targetPath = $this->getSetting('imagerSystemPath') . $image->getFolder()->path;
         $targetUrl = $this->getSetting('imagerUrl') . $image->getFolder()->path;
         $imageFilename = $image->filename;
     }
     /**
      * Check all the things that could go wrong(tm)
      */
     if (!IOHelper::getRealPath($sourcePath)) {
         throw new Exception(Craft::t('Source folder “{sourcePath}” does not exist', array('sourcePath' => $sourcePath)));
     }
     if (!IOHelper::getRealPath($targetPath)) {
         IOHelper::createFolder($this->getSetting('imagerSystemPath') . $image->getFolder()->path, craft()->config->get('defaultFolderPermissions'), true);
         $targetPath = IOHelper::getRealPath($this->getSetting('imagerSystemPath') . $image->getFolder()->path);
         if (!IOHelper::getRealPath($targetPath)) {
             throw new Exception(Craft::t('Target folder “{targetPath}” does not exist and could not be created', array('targetPath' => $targetPath)));
         }
     }
     if ($targetPath && !IOHelper::isWritable($targetPath)) {
         throw new Exception(Craft::t('Target folder “{targetPath}” is not writeable', array('targetPath' => $targetPath)));
     }
     if (!IOHelper::fileExists($sourcePath . $imageFilename)) {
         throw new Exception(Craft::t('Requested image “{fileName}” does not exist in path “{sourcePath}”', array('fileName' => $imageFilename, 'sourcePath' => $sourcePath)));
     }
     if (!craft()->images->checkMemoryForImage($sourcePath . $imageFilename)) {
         throw new Exception(Craft::t("Not enough memory available to perform this image operation."));
     }
     /**
      * Transform can be either an array or just an object. 
      * Act accordingly and return the results the same way to the template.
      */
     $r = null;
     if (isset($transform[0])) {
         $transformedImages = array();
         foreach ($transform as $t) {
             $transformedImage = $this->_getTransformedImage($imageFilename, $sourcePath, $targetPath, $targetUrl, $t);
             $transformedImages[] = $transformedImage;
         }
         $r = $transformedImages;
     } else {
         $transformedImage = $this->_getTransformedImage($imageFilename, $sourcePath, $targetPath, $targetUrl, $transform);
         $r = $transformedImage;
     }
     $this->imageInstance = null;
     return $r;
 }
Ejemplo n.º 2
0
 /**
  * Do an image transform
  *
  * @param AssetFileModel|string $image
  * @param Array $transform
  * @param Array $transformDefaults
  * @param Array $configOverrides
  *
  * @throws Exception
  * @return Image
  */
 public function transformImage($image, $transform, $transformDefaults, $configOverrides)
 {
     if (!$image) {
         return null;
         // there's nothing to see here, move along.
     }
     $this->configModel = new Imager_ConfigModel($configOverrides);
     $pathsModel = new Imager_ImagePathsModel($image);
     $this->imagineInstance = $this->_createImagineInstance();
     /**
      * Check all the things that could go wrong(tm)
      */
     if (!IOHelper::getRealPath($pathsModel->sourcePath)) {
         throw new Exception(Craft::t('Source folder “{sourcePath}” does not exist', array('sourcePath' => $pathsModel->sourcePath)));
     }
     if (!IOHelper::getRealPath($pathsModel->targetPath)) {
         IOHelper::createFolder($pathsModel->targetPath, craft()->config->get('defaultFolderPermissions'), true);
         if (!IOHelper::getRealPath($pathsModel->targetPath)) {
             throw new Exception(Craft::t('Target folder “{targetPath}” does not exist and could not be created', array('targetPath' => $pathsModel->targetPath)));
         }
         $pathsModel->targetPath = IOHelper::getRealPath($pathsModel->targetPath);
     }
     if ($pathsModel->targetPath && !IOHelper::isWritable($pathsModel->targetPath)) {
         throw new Exception(Craft::t('Target folder “{targetPath}” is not writeable', array('targetPath' => $pathsModel->targetPath)));
     }
     if (!IOHelper::fileExists($pathsModel->sourcePath . $pathsModel->sourceFilename)) {
         throw new Exception(Craft::t('Requested image “{fileName}” does not exist in path “{sourcePath}”', array('fileName' => $pathsModel->sourceFilename, 'sourcePath' => $pathsModel->sourcePath)));
     }
     if (!craft()->images->checkMemoryForImage($pathsModel->sourcePath . $pathsModel->sourceFilename)) {
         throw new Exception(Craft::t("Not enough memory available to perform this image operation."));
     }
     /**
      * Transform can be either an array or just an object.
      * Act accordingly and return the results the same way to the template.
      */
     $r = null;
     if (isset($transform[0])) {
         $transformedImages = array();
         foreach ($transform as $t) {
             $transformedImage = $this->_getTransformedImage($pathsModel, $transformDefaults != null ? array_merge($transformDefaults, $t) : $t);
             $transformedImages[] = $transformedImage;
         }
         $r = $transformedImages;
     } else {
         $transformedImage = $this->_getTransformedImage($pathsModel, $transformDefaults != null ? array_merge($transformDefaults, (array) $transform) : $transform);
         $r = $transformedImage;
     }
     $this->imageInstance = null;
     /**
      * If this was an ajax request, and optimization tasks were created, trigger them now.
      */
     if (craft()->request->isAjaxRequest() && $this->taskCreated && $this->getSetting('runTasksImmediatelyOnAjaxRequests')) {
         $this->_triggerTasksNow();
     }
     if (count($this->invalidatePaths) > 0) {
         craft()->imager_aws->invalidateCloudfrontPaths($this->invalidatePaths);
         $this->invalidatePaths = array();
     }
     return $r;
 }
Ejemplo n.º 3
0
 /**
  * Do an image transform
  *
  * @param AssetFileModel|string $image
  * @param Array $transform
  * @param Array $configOverrides
  *
  * @throws Exception
  * @return Image
  */
 public function transformImage($image, $transform, $configOverrides)
 {
     if (!$image) {
         return null;
         // there's nothing to see here, move along.
     }
     $this->configModel = new Imager_ConfigModel($configOverrides);
     $pathsModel = new Imager_ImagePathsModel($image);
     $this->imagineInstance = $this->_createImagineInstance();
     /**
      * Check all the things that could go wrong(tm)
      */
     if (!IOHelper::getRealPath($pathsModel->sourcePath)) {
         throw new Exception(Craft::t('Source folder “{sourcePath}” does not exist', array('sourcePath' => $pathsModel->sourcePath)));
     }
     if (!IOHelper::getRealPath($pathsModel->targetPath)) {
         IOHelper::createFolder($pathsModel->targetPath, craft()->config->get('defaultFolderPermissions'), true);
         $pathsModel->targetPath = IOHelper::getRealPath($pathsModel->targetPath);
         if (!IOHelper::getRealPath($pathsModel->targetPath)) {
             throw new Exception(Craft::t('Target folder “{targetPath}” does not exist and could not be created', array('targetPath' => $pathsModel->targetPath)));
         }
     }
     if ($pathsModel->targetPath && !IOHelper::isWritable($pathsModel->targetPath)) {
         throw new Exception(Craft::t('Target folder “{targetPath}” is not writeable', array('targetPath' => $pathsModel->targetPath)));
     }
     if (!IOHelper::fileExists($pathsModel->sourcePath . $pathsModel->sourceFilename)) {
         throw new Exception(Craft::t('Requested image “{fileName}” does not exist in path “{sourcePath}”', array('fileName' => $pathsModel->sourceFilename, 'sourcePath' => $pathsModel->sourcePath)));
     }
     if (!craft()->images->checkMemoryForImage($pathsModel->sourcePath . $pathsModel->sourceFilename)) {
         throw new Exception(Craft::t("Not enough memory available to perform this image operation."));
     }
     /**
      * Transform can be either an array or just an object.
      * Act accordingly and return the results the same way to the template.
      */
     $r = null;
     if (isset($transform[0])) {
         $transformedImages = array();
         foreach ($transform as $t) {
             $transformedImage = $this->_getTransformedImage($pathsModel, $t);
             $transformedImages[] = $transformedImage;
         }
         $r = $transformedImages;
     } else {
         $transformedImage = $this->_getTransformedImage($pathsModel, $transform);
         $r = $transformedImage;
     }
     $this->imageInstance = null;
     return $r;
 }