Skip to content

Commit

Permalink
Method replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
jonom committed Feb 15, 2016
1 parent ef1fd14 commit 504b198
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,21 @@ Use just like existing cropping functions but swap out the names:
- $CropWidth --> $FocusCropWidth
- $CropHeight --> $FocusCropHeight

Or use the existing names... see 'Method replacement' below.

## Advanced usage

### Method replacement

You can swap out the `Image` class using the [injector](https://docs.silverstripe.org/en/developer_guides/extending/injector/) like this:

```yml
Injector:
Image:
class: FPImage
```
This will automatically upgrade the built-in cropping methods so that they give you focused output. Caveat: this doesn't apply to chained image methods unfortunately due to a limitation with injector support in the `Image_cached` class.

### Method chaining

Expand Down
5 changes: 4 additions & 1 deletion _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ After: 'framework/*','cms/*'
---
Image:
extensions:
- FocusPointImage
- FocusPointImage
# Injector:
# Image:
# class: FPImage
45 changes: 45 additions & 0 deletions code/FPImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

class FPImage extends Image {

public function Fill($width, $height) {
return $this->FocusFill($width, $height);
}

public function FillMax($width, $height) {
return $this->FocusFillMax($width, $height);
}

public function CropWidth($width) {
return $this->FocusCropWidth($width);
}

public function CropHeight($height) {
return $this->FocusCropHeight($height);
}
}

/*
// This is required to ensure manipulated images get their methods overidden too,
// but it doesn't work because Image_cached isn't compatible with the injector.
// Not having this in place means chained method chaining doesn't work properly
// e.g. `$Image.ScaleHeight(200).CropWidth(200)` will not use $FocusCropWidth.
class FPImage_cached extends Image_cached {
public function Fill($width, $height) {
return $this->FocusFill($width, $height);
}
public function FillMax($width, $height) {
return $this->FocusFillMax($width, $height);
}
public function CropWidth($width) {
return $this->FocusCropWidth($width);
}
public function CropHeight($height) {
return $this->FocusCropHeight($height);
}
}
*/
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"homepage": "http://jonathonmenz.com"
}],
"require": {
"silverstripe/framework": "^3.2"
"silverstripe/framework": "^3.3"
}
}
}

0 comments on commit 504b198

Please sign in to comment.