すべてのモディファイア呼び出しは、カスタムのイメージモディファイアクラスに組み合わせとして保存できます。このモディファイアは、どのコマンドを画像インスタンスに、どの順序で、どの引数で適用するかを定義します。これにより、複雑な変換を単一の再利用可能な呼び出しにまとめることが簡単になります。
Intervention Image はシンプルな Intervention\Image\Interfaces\ModifierInterface を提供しており、すべてのモディファイアはこれを実装する必要があります。
自分のモディファイアを作成したら、modify() メソッドで適用できます。
#カスタムモディファイアの実装
以下の非常に簡単な例は、グレースケールとピクセル化の効果を組み合わせたカスタムモディファイアクラスを示します。
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Interfaces\ImageInterface;
class MyCustomModifier implements ModifierInterface
{
protected $size;
public function __construct(int $size)
{
$this->size = $size;
}
public function apply(ImageInterface $image): ImageInterface
{
$image->pixelate($this->size);
$image->greyscale();
return $image;
}
}
#カスタムモディファイアの適用
カスタムモディファイアを実装すれば、画像インスタンスに簡単に適用できます。
public Image::modify(ModifierInterface $modifier): ImageInterface
#パラメータ
| Name | Type | Description |
|---|---|---|
| modifier | Intervention\Image\Interfaces\ModifierInterface | モディファイアオブジェクト |
#例
use Intervention\Image\ImageManager;
// 新しい画像インスタンスを作成
$image = ImageManager::imagick()->read('images/example.jpg');
// モディファイアを適用
$image->modify(new MyCustomModifier(25));