3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace SimonEq; interface ImageInterface { function getFileName(); } class Image implements ImageInterface { private $fileName; public function __construct($fileName) { $this->fileName = $fileName; } public function getFileName() { return $this->fileName; } } interface TransformationInterface { function transform(ImageInterface $image); } class TransformationAggregate implements TransformationInterface { private $transformationArray = []; public function __construct($transformationArray) { $this->transformationArray = $transformationArray; } public function transform(ImageInterface $image) { foreach ($this->transformationArray as $transformation) { $transformation->transform($image); } } } class CropTransformation implements TransformationInterface { private $width; private $height; public function __construct($width, $height) { $this->width = (int) $width; $this->height = (int) $height; } public function transform(ImageInterface $image) { printf('Image "%s" transformed with %s' . PHP_EOL, $image->getFileName(), print_r($this, true)); } } class ResizeTransformation implements TransformationInterface { private $width; private $height; private $resample; public function __construct($width, $height, $resample) { $this->width = (int) $width; $this->height = (int) $height; $this->resample = (bool) $resample; } public function transform(ImageInterface $image) { printf('Image "%s" transformed with %s' . PHP_EOL, $image->getFileName(), print_r($this, true)); } } class WatermarkTransformation implements TransformationInterface { private $image; public function __construct(ImageInterface $image) { $this->image = $image; } public function transform(ImageInterface $image) { printf('Image "%s" transformed with %s' . PHP_EOL, $image->getFileName(), print_r($this, true)); } } $image = new Image('FluffyCat.jpg'); $transformation = new TransformationAggregate([ new CropTransformation(200, 400), new WatermarkTransformation(new Image('MyWaterMark.png')), new ResizeTransformation(100, 200, true), ]); $transformation->transform($image);

preferences:
41.14 ms | 402 KiB | 5 Q