Skip to content

Commit

Permalink
Merge pull request #12 from ushahidi/source-dir-prefix
Browse files Browse the repository at this point in the history
Add support for prefixing the source directory
  • Loading branch information
Bodom78 committed Aug 10, 2014
2 parents 4f44a70 + a3bdcde commit 1ee9f96
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Number of seconds before the browser checks the server for a new version of the
**cache_dir:** 'cache/'
Path to the image cache directory you would like to use, don't forget the trailing slash!

**source_dir:** `''`
Path prefix to the source directory you would like to use, don't forget the trailing slash!

**mimic_source_dir:** TRUE
Mimic the source file folder structure within the cache directory.
Useful if you want to keep track of cached files and folders to perhaps periodically clear some cache folders but not others.
Expand Down
15 changes: 11 additions & 4 deletions classes/ImageFly.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class ImageFly
* or processed sub directories when the "mimic_source_dir" config option id set to TRUE
*/
protected $cache_dir = NULL;

/**
* @var string Stores the path to the source directory (set in the config "source_dir")
*/
protected $source_dir = NULL;

/**
* @var object Kohana image instance
Expand Down Expand Up @@ -64,6 +69,8 @@ public function __construct()

// Set the config
$this->config = Kohana::$config->load('imagefly');

$this->source_dir = $this->config['source_dir'];

// Try to create the cache directory if it does not exist
$this->_create_cache_dir();
Expand All @@ -72,7 +79,7 @@ public function __construct()
$this->_set_params();

// Set the source file modified timestamp
$this->source_modified = filemtime($this->source_file);
$this->source_modified = filemtime($this->source_dir.$this->source_file);

// Try to create the mimic directory structure if required
$this->_create_mimic_cache_dir();
Expand Down Expand Up @@ -155,7 +162,7 @@ private function _set_params()
throw new HTTP_Exception_404('The requested URL :uri was not found on this server.',
array(':uri' => Request::$current->uri()));

$this->image = Image::factory($filepath);
$this->image = Image::factory($this->source_dir.$filepath);

// The parameters are separated by hyphens
$raw_params = explode('-', $params);
Expand Down Expand Up @@ -228,7 +235,7 @@ private function _cached_exists()
*/
private function _cached_required()
{
$image_info = getimagesize($this->source_file);
$image_info = getimagesize($this->source_dir.$this->source_file);

if (($this->url_params['w'] == $image_info[0]) AND ($this->url_params['h'] == $image_info[1]))
{
Expand Down Expand Up @@ -371,7 +378,7 @@ private function _serve_file()
// Set either the source or cache file as our datasource
if ($this->serve_default)
{
$file_data = $this->source_file;
$file_data = $this->source_dir.$this->source_file;
}
else
{
Expand Down
12 changes: 8 additions & 4 deletions config/imagefly.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
* Path to the image cache directory you would like to use, don't forget the trailing slash!
*/
'cache_dir' => 'cache/',
/**
* Path to the image cache directory you would like to use, don't forget the trailing slash!
*/
'source_dir' => '',
/**
* Mimic the source file folder structure within the cache directory.
* Useful if you want to keep track of cached files and folders to perhaps periodically clear some cache folders but not others.
Expand All @@ -28,13 +32,13 @@
/**
* If the image should be scaled up beyond it's original dimensions on resize.
*/
'scale_up' => FALSE,
/**
'scale_up' => FALSE,
/**
* Will only allow param configurations set in the presets.
* Best enabled on production sites to reduce spamming of different sized images on the server.
*/
'enforce_presets' => FALSE,
/**
'enforce_presets' => FALSE,
/**
* Imagefly params that are allowed when enforce_presets is set to TRUE
* Any other param configuration will throw a 404 error.
*/
Expand Down

0 comments on commit 1ee9f96

Please sign in to comment.