有没有办法在Silverstripe 4中像这样扩展图像类?
class MyImage extends Image {
public function generateRotateClockwise(GD $gd) {
return $gd->rotate(90);
}
public function generateRotateCounterClockwise(GD $gd) {
return $gd->rotate(270);
}
我在Silverstripe网页上找不到任何内容。
感谢您的帮助。
答案 0 :(得分:2)
You can do this using the Injector to rewrite the class that is loaded when SilverStripe asks for Image
, but with Image it's not recommended. If you wanted to, you'd do this:
# File: mysite/_config/injector.yml
---
Name: myinjectorconfig
---
SilverStripe\Core\Injector\Injector:
SilverStripe\Assets\Image:
class: MyImage
And your class would look like the example in your question.
Instead if you want to add two new PHP methods you can use an Extension:
use SilverStripe\ORM\DataExtension;
class MyImageExtension extends DataExtension
{
public function generateRotateClockwise(GD $gd)
{
return $gd->rotate(90);
}
public function generateRotateCounterClockwise(GD $gd)
{
return $gd->rotate(270);
}
}
Then apply it to Image with configuration:
# File: mysite/_config/extensions.yml
---
Name: myextensions
---
SilverStripe\Assets\Image:
extensions:
- MyImageExtension
This adds the two public methods to wherever an Image class is used.