Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.idea
build
composer.lock
.phpunit.result.cache
docs
vendor
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 82 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Square1\Resized;

class Builder
{
private $resized;

private $params;

public function __construct(
Resized $resized,
string $url
)
{
$this->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'] ?? []
);
}


}
9 changes: 9 additions & 0 deletions src/Resized.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Square1\Resized\Test;

use PHPUnit\Framework\TestCase;
use Square1\Resized\Builder;
use Square1\Resized\Resized;

class BuilderTest extends TestCase
{
private $resized;

public function setUp(): void
{
$this->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]);
}

}
16 changes: 16 additions & 0 deletions tests/ResizedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

}

}