Skip to content

Commit

Permalink
Merge pull request #28 from vormkracht10/hotfix/add-tests-for-automat…
Browse files Browse the repository at this point in the history
…ic-preview-addition

[Fix] Add tests for automatic preview addition
  • Loading branch information
markvaneijk authored Mar 9, 2023
2 parents afd8952 + faf90fa commit a64e614
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/UploadcareTransformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ public function getUrl(): string
// because these transformations won't work if they do not contain the preview transformation as well.
// Check if url contains 'resize', 'scale_crop' or 'preview'. If not add, add 'preview' to the url.
// By using 'preview' the image will not be changed and produce the biggest possible image.
if ((str_contains($url, 'blur_region') ||
str_contains($url, 'enhance') ||
str_contains($url, 'filter') ||
str_contains($url, 'zoom_objects')) && (! str_contains($url, 'preview') ||
! str_contains($url, 'scale_crop') ||
! str_contains($url, 'resize'))
if (
preg_match('~\/(blur_region|enhance|filter|zoom_objects)\/~', $url) &&
! preg_match('~\/(preview|scale_crop|resize)\/~', $url)
) {
$url .= '-/preview/';
}
Expand Down
24 changes: 24 additions & 0 deletions tests/GenerateUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,27 @@

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

it('adds preview automatically to url when using blur_region, enhance, filter or zoom_objects transformation when not already using preview, scale_crop or resize', function () {
$uuid = '12a3456b-c789-1234-1de2-3cfa83096e25';

$url = (string) uploadcare($uuid)->blurRegion(0, 0, 100, 100, 20)->filename('test.jpg');

expect($url)->toContain('/preview/');

$url = (string) uploadcare($uuid)->enhance(50)->filename('test.jpg');

expect($url)->toContain('/preview/');

$url = (string) uploadcare($uuid)->filter('adaris', 50)->filename('test.jpg');

expect($url)->toContain('/preview/');

$url = (string) uploadcare($uuid)->zoomObjects(50)->filename('test.jpg');

expect($url)->toContain('/preview/');

$url = (string) uploadcare($uuid)->zoomObjects(50)->resize(200)->filename('test.jpg');

expect($url)->not->toContain('/preview/');
});

0 comments on commit a64e614

Please sign in to comment.