Пример #1
0
 /**
  * Gets color palette for image
  *
  * @param AssetFileModel|string $image
  * @param $colorCount
  * @param $quality
  * @param $colorValue
  * @return array
  * @throws Exception
  */
 public function getColorPalette($image, $colorCount, $quality, $colorValue)
 {
     $pathsModel = new Imager_ImagePathsModel($image);
     if (!IOHelper::getRealPath($pathsModel->sourcePath)) {
         throw new Exception(Craft::t('Source folder “{sourcePath}” does not exist', array('sourcePath' => $pathsModel->sourcePath)));
     }
     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)));
     }
     $palette = ColorThief::getPalette($pathsModel->sourcePath . $pathsModel->sourceFilename, $colorCount, $quality);
     return $colorValue == 'hex' ? $this->_paletteToHex($palette) : $palette;
 }
Пример #2
0
 /**
  * Save temporary file and return filename
  *
  * @param $imageInstance
  * @param $sourceExtension
  * @return string
  */
 private function _saveTemporaryFile($imageInstance, $sourceExtension)
 {
     $tempPath = craft()->path->getRuntimePath() . 'imager/temp/';
     // check if the path exists
     if (!IOHelper::getRealPath($tempPath)) {
         IOHelper::createFolder($tempPath, craft()->config->get('defaultFolderPermissions'), true);
         if (!IOHelper::getRealPath($tempPath)) {
             throw new Exception(Craft::t('Temp folder “{tempPath}” does not exist and could not be created', array('tempPath' => $tempPath)));
         }
     }
     $targetFilePath = $tempPath . md5(time()) . '.' . $sourceExtension;
     $saveOptions = array('jpeg_quality' => 100, 'png_compression_level' => 1, 'flatten' => true);
     $imageInstance->save($targetFilePath, $saveOptions);
     return $targetFilePath;
 }
 /**
  * Get the file system path for upload source.
  *
  * @param $sourceType
  *
  * @return string
  */
 public function getSourceFileSystemPath($sourceType = null)
 {
     $path = is_null($sourceType) ? $this->getBasePath($this->source->settings) : $sourceType->getBasePath($this->source->settings);
     $path = IOHelper::getRealPath($path);
     return $path;
 }
Пример #4
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;
 }
Пример #5
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;
 }