From 504b19893c512a42ecde578ee75eda43be34f6a2 Mon Sep 17 00:00:00 2001 From: Jonathon Menz Date: Sun, 13 Dec 2015 12:12:03 -0800 Subject: [PATCH] Method replacement --- README.md | 13 +++++++++++++ _config/config.yml | 5 ++++- code/FPImage.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ composer.json | 4 ++-- 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100755 code/FPImage.php diff --git a/README.md b/README.md index 554972d..069bb72 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/_config/config.yml b/_config/config.yml index 53eafe3..dc6f8ff 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -4,4 +4,7 @@ After: 'framework/*','cms/*' --- Image: extensions: - - FocusPointImage \ No newline at end of file + - FocusPointImage +# Injector: +# Image: +# class: FPImage diff --git a/code/FPImage.php b/code/FPImage.php new file mode 100755 index 0000000..1004ec9 --- /dev/null +++ b/code/FPImage.php @@ -0,0 +1,45 @@ +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); + } +} +*/ diff --git a/composer.json b/composer.json index f71ebce..e01d74c 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,6 @@ "homepage": "http://jonathonmenz.com" }], "require": { - "silverstripe/framework": "^3.2" + "silverstripe/framework": "^3.3" } -} \ No newline at end of file +}