/** * 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; }
/** * Imports the Craft datamodel. * * @param string $file yml file containing the schema definition * @param string $override_file yml file containing the override values * @param bool $force if set to true items not in the import will be deleted * * @return int */ public function actionIndex($file = 'craft/config/schema.yml', $override_file = 'craft/config/override.yml', $force = false) { if (!IOHelper::fileExists($file)) { $this->usageError(Craft::t('File not found.')); } $result = Craft::app()->schematic->importFromYaml($file, $override_file, $force); if (!$result->hasErrors()) { Craft::log(Craft::t('Loaded schema from {file}', ['file' => $file])); return 0; } Craft::log(Craft::t('There was an error loading schema from {file}', ['file' => $file])); print_r($result->getErrors()); return 1; }
/** * Saves image as webp * * @param $imageInstance * @param $path * @param $sourceExtension * @param $saveOptions * @throws Exception */ private function _saveAsWebp($imageInstance, $path, $sourceExtension, $saveOptions) { if ($this->getSetting('useCwebp')) { // save temp file $tempFile = $this->_saveTemporaryFile($imageInstance, $sourceExtension); // convert to webp with cwebp $command = escapeshellcmd($this->getSetting('cwebpPath') . ' ' . $this->getSetting('cwebpOptions') . ' -q ' . $saveOptions['webp_quality'] . ' ' . $tempFile . ' -o ' . $path); $r = shell_exec($command); if (!IOHelper::fileExists($path)) { throw new Exception(Craft::t('Save operation failed')); } // delete temp file IOHelper::deleteFile($tempFile); } else { if ($this->imageDriver === 'gd') { $instance = $imageInstance->getGdResource(); if (false === \imagewebp($instance, $path, $saveOptions['webp_quality'])) { throw new Exception(Craft::t('Save operation failed')); } // Fix for corrupt file bug (http://stackoverflow.com/questions/30078090/imagewebp-php-creates-corrupted-webp-files) if (filesize($path) % 2 == 1) { file_put_contents($path, "", FILE_APPEND); } } if ($this->imageDriver === 'imagick') { $instance = $imageInstance->getImagick(); $instance->setImageFormat('webp'); $instance->setImageAlphaChannel(\Imagick::ALPHACHANNEL_ACTIVATE); $instance->setBackgroundColor(new \ImagickPixel('transparent')); $instance->setImageCompressionQuality($saveOptions['webp_quality']); $imagickOptions = $saveOptions['webp_imagick_options']; if ($imagickOptions && count($imagickOptions) > 0) { foreach ($imagickOptions as $key => $val) { $instance->setOption('webp:' . $key, $val); } } $instance->writeImage($path); } } }
/** * 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; }
/** * Renders the settings form to configure Minimee * @return String */ public function getSettingsHtml() { $filesystemConfigPath = craft()->path->getConfigPath() . 'minimee.php'; return craft()->templates->render('minimee/settings', array('settings' => $this->getSettings(), 'filesystemConfigExists' => (bool) IOHelper::fileExists($filesystemConfigPath))); }
/** * 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; }
public function getProviderSource($providerClass) { // Get the full class name $class = $providerClass . 'OAuthProviderSource'; $nsClass = 'OAuthProviderSources\\' . $class; // Skip the autoloader if (!class_exists($nsClass, false)) { $path = CRAFT_PLUGINS_PATH . 'oauth/providers/' . $class . '.php'; if (($path = IOHelper::fileExists($path, false)) !== false) { require_once $path; } else { return null; } } if (!class_exists($nsClass, false)) { return null; } $providerSource = new $nsClass(); if (!$providerSource instanceof \OAuthProviderSources\BaseOAuthProviderSource) { die("this provider doesn't implement BaseOAuthProviderSource abstract class"); } return $providerSource; }