Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Add tests for automatic preview addition #28

Merged
merged 1 commit into from
Mar 9, 2023
Merged
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
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/');
});