Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Image class > Wrapper for GD filter functionality


Reply


773 Views

Avatar
dutton

Community Member, 1 Post

18 February 2011 at 1:08am

Edited: 18/02/2011 2:17am

Hi,

We are working on a few sites that require b&w version of the images (i.e. colour on rollover, b&w when not). I'd like to avoid telling my client they have to upload 2 versions of the image so have been looking into potential solutions.

One potential was the following wrapper to the built in GD functions (http://www.php.net/manual/en/function.imagefilter.php), the most generic way I can see is as follows (Silverstripe 2.4.5):

* Add function 'AddFilter' to Image, something like:

function AddFilter ($filtertype, int $arg1, int $arg2, int $arg3, int $arg4) {
//some sensible defaults
//add to queued filters (private array)
$this->filters[] = $filter;//a filter is like: type => array args
return $this;
}

This would be accessible from a template like <% control Image.Setwidth(100) %> $AddFilter(filtername, 123, 456) <% end_control %>

* Alter 'cacheFilename' to create a filename that includes the filters and arguments.

* Alter 'getFormattedImage' to pass all the arguments needed to cacheFilename

* In 'generateFormattedImage', look at the filters that have been added and apply each in turn before returning the image.

If I should be looking somewhere else, please let me know - I can't see how this could be built as an extension without modifying the core (or re-writing chunks of it into a module).

The above looks straightforward enough, but one area concerned me greatly; this cached file name- specifically its length as we're adding dimensions and filtername and arguments here (I'm not sure of the restriction on input filename). I can't see any measure taken to obey a specific OS's rules on filename/path length, but maybe this is buried in the core somewhere?

Lastly, I'm looking to make this a generic wrapper for use in templates to get at the GD filter functions... is it useful to anyone? I would like to send back my changes, so I don't have to patch every new core with the change.

Many thanks in advance,
Alex


EDIT: just realised that the chaining thing doesn't work (not sure where I'd gained that impression). Perhaps this is easy to fix though? Otherwise, it would mean altering each of the functions already present to allow passing of filters from the template. Perhaps this solution won't work atall...


EDIT2: Looked at the chaining again, can be achieved using nested controls, ie:

<% control Logo.SetWidth(100) %>
   <% control AddFilter(filter-a) %>
      $AddFilter(filter-b)
   <% end_control %>
<% end_control %>