/** * Update the asset transforms for the FileModel. * * @param AssetFileModel $fileModel * @param array|object|string $transformsToUpdate * @return bool */ public function updateTransforms(AssetFileModel $fileModel, $transformsToUpdate) { if (!in_array(IOHelper::getExtension($fileModel->filename), ImageHelper::getAcceptedExtensions())) { return true; } $sourceType = craft()->assetSources->getSourceTypeById($fileModel->sourceId); $imageSource = $sourceType->getImageSourcePath($fileModel); if (!IOHelper::fileExists($imageSource)) { return false; } if (!is_array($transformsToUpdate)) { $transformsToUpdate = array($transformsToUpdate); } foreach ($transformsToUpdate as $transform) { $transform = $this->normalizeTransform($transform); $transformLocation = $this->_getTransformLocation($transform); $timeModified = $sourceType->getTimeTransformModified($fileModel, $transformLocation); // Create the transform if the file doesn't exist, or if it was created before the image was last updated // or if the transform dimensions have changed since it was last created if (!$timeModified || $timeModified < $fileModel->dateModified || $timeModified < $transform->dimensionChangeTime) { $targetFile = AssetsHelper::getTempFilePath(IOHelper::getExtension($fileModel->filename)); switch ($transform->mode) { case 'fit': craft()->images->loadImage($imageSource)->scaleToFit($transform->width, $transform->height)->saveAs($targetFile); break; case 'stretch': craft()->images->loadImage($imageSource)->resize($transform->width, $transform->height)->saveAs($targetFile); break; default: craft()->images->loadImage($imageSource)->scaleAndCrop($transform->width, $transform->height, true, $transform->position)->saveAs($targetFile); break; } clearstatcache(true, $targetFile); $sourceType->putImageTransform($fileModel, $transformLocation, $targetFile); IOHelper::deleteFile($targetFile); } } return true; }
/** * Get URL for a file. * * @param AssetFileModel $file * @param $transform * @return string */ public function getUrlForFile(AssetFileModel $file, $transform = null) { $returnPlaceholder = false; if (!$transform || !in_array(IOHelper::getExtension($file->filename), ImageHelper::getAcceptedExtensions())) { $sourceType = craft()->assetSources->getSourceTypeById($file->sourceId); $base = $sourceType->getBaseUrl(); return $base . $file->getFolder()->fullPath . $file->filename; } // Get the transform index model $existingTransformData = craft()->assetTransforms->getTransformIndex($file, $transform); // Does the file actually exist? if ($existingTransformData->fileExists) { return craft()->assetTransforms->getUrlforTransformByFile($file, $transform); } else { // File doesn't exist yet - load the TransformLoader and set the placeholder URL flag $placeholderUrl = UrlHelper::getResourceUrl('images/blank.gif'); if (!$this->_includedTransformLoader) { $entityPlaceholderUrl = htmlspecialchars($placeholderUrl, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); $spinnerUrl = UrlHelper::getResourceurl('images/spinner_transform.gif'); $actionUrl = UrlHelper::getActionUrl('assets/generateTransform'); craft()->templates->includeJsResource('js/TransformLoader.js'); craft()->templates->includeJs('new TransformLoader(' . JsonHelper::encode($placeholderUrl) . ', ' . JsonHelper::encode($entityPlaceholderUrl) . ', ' . JsonHelper::encode($spinnerUrl) . ', ' . JsonHelper::encode($actionUrl) . ');'); $this->_includedTransformLoader = true; } return $placeholderUrl . '#' . $existingTransformData->id; } }