diff --git a/.gitignore b/.gitignore index f02a2f8..1e1645c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ +.idea build composer.lock +.phpunit.result.cache docs vendor diff --git a/README.md b/README.md index 39bfc7f..768e883 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,16 @@ $ composer require square1/resized $img = $resized->process('http://www.example.com/some-image.jpg', '100', '100', 'This is a title'); ``` +Using the builder you can omit parameters and is more flexible: + +```php +$img = $resized->builder('http://www.example.com/some-image.jpg') + ->width(100) + ->quality(90) + ->output('webp') + ->url(); +``` + ## Testing ``` bash diff --git a/src/Builder.php b/src/Builder.php new file mode 100644 index 0000000..d2a8225 --- /dev/null +++ b/src/Builder.php @@ -0,0 +1,82 @@ +resized = $resized; + $this->params['url'] = $url; + } + + public function getParams() : array + { + return $this->params; + } + + public function width($width): self + { + $this->params['width'] = $width; + return $this; + } + + public function height($height): self + { + $this->params['height'] = $height; + return $this; + } + + public function title($title): self + { + $this->params['title'] = $title; + return $this; + } + + public function output($output): self + { + $this->params['options']['output'] = $output; + return $this; + } + + public function quality($quality): self + { + $this->params['options']['quality'] = $quality; + return $this; + } + + public function options(array $options): self + { + $this->params['options'] = $this->mergeOptions($options); + return $this; + } + + private function mergeOptions(array $options) : array + { + return array_merge( + $this->params['options'], + $options + ); + + } + + public function url(): string + { + return $this->resized->process( + $this->params['url'], + $this->params['width'] ?? '', + $this->params['height'] ?? '', + $this->params['title'] ?? '', + $this->params['options'] ?? [] + ); + } + + +} diff --git a/src/Resized.php b/src/Resized.php index b37d7f6..bd5e969 100644 --- a/src/Resized.php +++ b/src/Resized.php @@ -146,6 +146,15 @@ public function process($url, $width = '', $height = '', $title = '', $options = return implode('/', $fullUrl); } + /** + * @param $url + * @return Builder + */ + public function src($url) : Builder + { + return new Builder($this, $url); + } + /** * Get seo slug and file extension diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php new file mode 100644 index 0000000..eacf506 --- /dev/null +++ b/tests/BuilderTest.php @@ -0,0 +1,57 @@ +resized = new Resized('key', 'secret-d0be2dc421be4fcd0172e5afceea3970e2f3d940'); + } + + public function testProcessBuilder() + { + $processBuilder = $this->resized->src('http://www.example.com/some-image-to-resize.jpg'); + + $this->assertInstanceOf(Builder::class, $processBuilder); + } + + public function testProcessBuilderFull() + { + $params = $this->resized->src('http://www.example.com/some-image-to-resize.jpg') + ->width(100) + ->height(100) + ->output('webp') + ->title('A nice title') + ->options(['quality' => 80]) + ->getParams(); + + $this->assertEquals($params['url'], 'http://www.example.com/some-image-to-resize.jpg'); + $this->assertEquals($params['width'], 100); + $this->assertEquals($params['height'], 100); + $this->assertEquals($params['options'], ['output' => 'webp', 'quality' => 80]); + $this->assertEquals($params['title'], 'A nice title'); + } + + public function testProcessBuilderOptionsWillBeMerge() + { + $partialImage = $this->resized->src('http://www.example.com/some-image-to-resize.jpg') + ->width(100) + ->height(100) + ->quality(70) + ->output('webp') + ->title('A nice title') + ->options(['quality' => 80]) + ->options(['output' => 'jpeg']) + ->getParams(); + + $this->assertEquals($partialImage['options'], ['output' => 'jpeg', 'quality' => 80]); + } + +} diff --git a/tests/ResizedTest.php b/tests/ResizedTest.php index 3a78b94..1955aa2 100644 --- a/tests/ResizedTest.php +++ b/tests/ResizedTest.php @@ -150,4 +150,20 @@ public function testNoConstraintParams() $this->assertEquals($img, 'https://img.resized.co/key/eyJkYXRhIjoie1widXJsXCI6XCJodHRwOlxcXC9cXFwvd3d3LmV4YW1wbGUuY29tXFxcL3NvbWUtaW1hZ2UtdG8tcmVzaXplLmpwZ1wiLFwid2lkdGhcIjpcIlwiLFwiaGVpZ2h0XCI6XCJcIixcImRlZmF1bHRcIjpcImh0dHA6XFxcL1xcXC93d3cuZXhhbXBsZS5jb21cXFwvbm8taW1hZ2UuanBnXCIsXCJvcHRpb25zXCI6W119IiwiaGFzaCI6IjMzMGZhODdhOWFmNGJmNTZiOWI2ODQ5NjAxNTZmMmYwNWRiY2Y0ZTUifQ==/some-image-to-resize.jpg'); } + + public function testProcessBuild() + { + $resized = new Resized('key', 'secret-d0be2dc421be4fcd0172e5afceea3970e2f3d940'); + + $resized->setDefaultImage('http://www.example.com/no-image.jpg'); + $img = $resized->src('http://www.example.com/some-image-to-resize.jpg') + ->width(100) + ->height(100) + ->output('webp') + ->url(); + + $this->assertEquals($img, 'https://img.resized.co/key/eyJkYXRhIjoie1widXJsXCI6XCJodHRwOlxcXC9cXFwvd3d3LmV4YW1wbGUuY29tXFxcL3NvbWUtaW1hZ2UtdG8tcmVzaXplLmpwZ1wiLFwid2lkdGhcIjoxMDAsXCJoZWlnaHRcIjoxMDAsXCJkZWZhdWx0XCI6XCJodHRwOlxcXC9cXFwvd3d3LmV4YW1wbGUuY29tXFxcL25vLWltYWdlLmpwZ1wiLFwib3B0aW9uc1wiOntcIm91dHB1dFwiOlwid2VicFwifX0iLCJoYXNoIjoiOWE5MWM4NWM2NzJlZTY4YzU3OTExYjEwYjA3N2M1ZTRlNjcwN2ZiYSJ9/some-image-to-resize.jpg'); + + } + }