Permalink
Browse files

allows for use of plugin without defining a profile - for one-time im…

…age resizing
  • Loading branch information...
1 parent d6c6943 commit 9611780f3a9def4284a7829cc011117db61b7216 @bshaffer committed Jan 10, 2011
View
@@ -5,8 +5,8 @@ Installation
------------
This plugin requires that you install either `sfThumbnailPlugin` or
-`sfImageTransformPlugin` for image transformation. `sfImageTransformPlugin`
-is used by default, but you can change this to `sfThumbnailPlugin` in `app.yml`
+`sfImageTransformPlugin` for image transformation. sfImageTransformPlugin
+is used by default, but you can change this to sfThumbnailPlugin in `app.yml`
all:
imagecache:
View
@@ -1,3 +1,7 @@
-imagecache_get:
+imagecache_get_profile:
url: /sfImageCache/:cache_name/get
+ params: { module: imagecache, action: get }
+
+imagecache_get:
+ url: /sfImageCache/get
params: { module: imagecache, action: get }
@@ -18,7 +18,12 @@ function imagecache_path($source, $cacheName, $absolute = false)
if (isset($options['asynchronous']) && $options['asynchronous'])
{
- return url_for('@imagecache_get?' . http_build_query(array('cache_name' => $cacheName, 'source' => $source)));
+ if (get_imagecache_service()->isImageCacheProfileName($cacheName))
+ {
+ return url_for('@imagecache_get_profile?' . http_build_query(array('cache_name' => $cacheName, 'source' => $source)));
+ }
+
+ return url_for('@imagecache_get?' . http_build_query(array('options' => $options, 'source' => $source)));
}
get_imagecache_service()->createCachedImage($sourcePath, $cachePath, $options['width'], $options['height']);
@@ -59,12 +59,64 @@ protected function createCachedImageWithSfThumbnailPlugin($imagePath, $savePath,
public function getImageCacheSystemPath($source, $cacheName)
{
- return sprintf('%s/%s-%s', $this->getImageCacheWebDir(), $cacheName, basename($source));
+ if ($this->isImageCacheProfileName($cacheName))
+ {
+ return sprintf('%s/%s-%s', $this->getImageCacheWebDir(), $cacheName, basename($source));
+ }
+
+ $suffix = array();
+ $info = pathinfo($source);
+ $options = $this->getImageCacheOptions($cacheName);
+
+ // Unset options that do not effect the cache
+ unset($options['asynchronous']);
+
+ // Cause who doesn't like a nice-looking filename?
+ if (isset($options['width']) && isset($options['height']))
+ {
+ $suffix[] = $options['width'] . 'x' . $options['height'];
+ unset($options['width'], $options['height']);
+ }
+
+ // append addl options
+ foreach ($options as $key => $value)
+ {
+ $suffix[] = "$key-$value";
+ }
+
+ return sprintf('%s/%s_%s.%s', $this->getImageCacheWebDir(), $info['filename'], implode('_', $suffix), $info['extension']);
}
public function getImageCacheOptions($cacheName)
{
- return array_merge(array('width' => '', 'height' => ''), sfConfig::get('app_imagecache_'.$cacheName));
+ $baseOptions = array_merge(array('width' => '', 'height' => ''));
+
+ // imagecache profile name
+ if ($this->isImageCacheProfileName($cacheName))
+ {
+ $options = sfConfig::get('app_imagecache_'.$cacheName);
+
+ }
+ // array of options passed
+ elseif (is_array($cacheName))
+ {
+ if (isset($cacheName['cache_name']))
+ {
+ $options = array_merge($cacheName, sfConfig::get('app_imagecache_'.$cacheName['cache_name']));
+ }
+ else
+ {
+ $options = $cacheName;
+ }
+ }
+ // array string passed
+ elseif(!$options = sfToolkit::stringToArray($cacheName))
+ {
+ throw new sfException('Unable to parse cache options. Please pass an imagecache profile name or imagecache options in string or array format');
+ }
+
+ // profile name passed
+ return array_merge($baseOptions, $options);
}
public function getCacheSymlinkDir()
@@ -92,6 +144,11 @@ public function getSourcePath($source)
return $this->canonicalize_path(sprintf('%s/%s', sfConfig::get('sf_web_dir'), $source));
}
+ public function isImageCacheProfileName($cacheName)
+ {
+ return is_string($cacheName) && sfConfig::has('app_imagecache_'.$cacheName);
+ }
+
protected function canonicalize_path($path)
{
if (empty($path))
@@ -13,7 +13,7 @@ public function executeGet(sfWebRequest $request)
$service = get_imagecache_service();
$source = $request->getParameter('source');
- $cacheName = $request->getParameter('cache_name');
+ $cacheName = $request->getParameter('cache_name', $request->getParameter('options'));
$cachePath = get_imagecache_service()->getImageCacheSystemPath($source, $cacheName);
$webPath = sprintf('%s/%s', sfConfig::get('app_imagecache_web_path'), basename($cachePath));

0 comments on commit 9611780

Please sign in to comment.