Skip to content

Commit

Permalink
Merge pull request #15 from vormkracht10/test-adding-filename-from-url
Browse files Browse the repository at this point in the history
Add filename to url, helper functions and test
  • Loading branch information
Baspa committed Aug 18, 2022
2 parents e738b8f + 3db2622 commit 9519888
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
"autoload": {
"psr-4": {
"Vormkracht10\\UploadcareTransformations\\": "src"
}
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
22 changes: 19 additions & 3 deletions src/UploadcareTransformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class UploadcareTransformation extends Transformations
protected array $transformations;
protected string $url;
protected string $baseUrl;
protected ?string $filename = null;

public function __construct(string $uuid, string $cdnUrl = 'https://ucarecdn.com/')
{
Expand All @@ -18,11 +19,26 @@ public function __construct(string $uuid, string $cdnUrl = 'https://ucarecdn.com
$this->baseUrl = $cdnUrl;
}

public function getUrl()
public function filename(string $filename)
{
$url = $this->baseUrl . $this->uuid;
$this->filename = $filename;

return $this->applyTransformations($url);
return $this;
}

public function getUrl(): string
{
$url = $this->applyTransformations($this->baseUrl . $this->uuid);

if (! str_ends_with($url, '/')) {
$url .= '/';
}

if ($this->filename) {
$url = rtrim($url, '/') . '/' . $this->filename;
}

return $url;
}

public function __toString()
Expand Down
17 changes: 17 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Vormkracht10\UploadcareTransformations\UploadcareTransformation;

if (! function_exists('uploadcare')) {
function uploadcare(string $uuid, string $cdnUrl = 'https://ucarecdn.com/'): UploadcareTransformation
{
return new UploadcareTransformation($uuid, $cdnUrl);
}
}

if (! function_exists('uc')) {
function uc(string $uuid, string $cdnUrl = 'https://ucarecdn.com/'): UploadcareTransformation
{
return uploadcare($uuid, $cdnUrl);
}
}
28 changes: 18 additions & 10 deletions tests/GenerateUrlTest.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
<?php

use Vormkracht10\UploadcareTransformations\UploadcareTransformation;

it('can generate a url without cdn', function () {
$uuid = '12a3456b-c789-1234-1de2-3cfa83096e25';
$transformation = (new UploadcareTransformation($uuid));
$transformation = uploadcare($uuid);

$url = $transformation->crop(320, '50p', 'center')->setFill('#ffffff')->getUrl();
$url = (string) $transformation->crop(320, '50p', 'center')->setFill('#ffffff');

expect($url)->toBe('https://ucarecdn.com/12a3456b-c789-1234-1de2-3cfa83096e25/crop/320x50p/center/set_fill/#ffffff');
expect($url)->toBe('https://ucarecdn.com/12a3456b-c789-1234-1de2-3cfa83096e25/crop/320x50p/center/set_fill/#ffffff/');
});

it('can generate a url with cdn', function () {
$uuid = '12a3456b-c789-1234-1de2-3cfa83096e25';
$cdnUrl = 'https://example.com/cdn/';

$transformation = (new UploadcareTransformation($uuid, $cdnUrl));
$transformation = uploadcare($uuid, $cdnUrl);

$url = $transformation->crop(320, '50p', 'center')->setFill('#ffffff')->getUrl();
$url = (string) $transformation->crop(320, '50p', 'center')->setFill('#ffffff');

expect($url)->toBe('https://example.com/cdn/12a3456b-c789-1234-1de2-3cfa83096e25/crop/320x50p/center/set_fill/#ffffff');
expect($url)->toBe('https://example.com/cdn/12a3456b-c789-1234-1de2-3cfa83096e25/crop/320x50p/center/set_fill/#ffffff/');
});

it('can generate a url using Conver to SRGB and ICC profile size threshold', function () {
$uuid = '12a3456b-c789-1234-1de2-3cfa83096e25';

$transformation = (new UploadcareTransformation($uuid));
$transformation = uploadcare($uuid);

$url = $transformation->convertToSRGB('fast')->iccProfileSizeThreshold(10)->getUrl();
$url = (string) $transformation->convertToSRGB('fast')->iccProfileSizeThreshold(10);

expect($url)->toBe('https://ucarecdn.com/12a3456b-c789-1234-1de2-3cfa83096e25/max_icc_size/10/srgb/fast');
expect($url)->toBe('https://ucarecdn.com/12a3456b-c789-1234-1de2-3cfa83096e25/max_icc_size/10/srgb/fast/');
});

it('can generate a url including filename with transformations', function () {
$uuid = '12a3456b-c789-1234-1de2-3cfa83096e25';
$transformation = uploadcare($uuid);

$url = (string) $transformation->crop(320, '50p', 'center')->filename('test.jpg');

expect($url)->toBe('https://ucarecdn.com/12a3456b-c789-1234-1de2-3cfa83096e25/crop/320x50p/center/test.jpg');
});

0 comments on commit 9519888

Please sign in to comment.