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

feat(media-library): add option to return original media file #985

Merged
merged 8 commits into from
Jun 24, 2021
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
1 change: 1 addition & 0 deletions config/glide.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@
'dpr' => '1',
],
'presets' => [],
'original_media_for_extensions' => [],
Keimeno marked this conversation as resolved.
Show resolved Hide resolved
];
2 changes: 2 additions & 0 deletions docs/.sections/getting-started/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ MEDIA_LIBRARY_ENDPOINT_TYPE=local
MEDIA_LIBRARY_IMAGE_SERVICE=A17\Twill\Services\MediaLibrary\Glide
```

If you want to add support for other image formats, which aren't covered by Glide, you can specify an array of extensions. For files matching the ending, the original media URL will be returned, instead of the modified one. For this, you can specify the following configuration key: `glide.original_media_for_extensions`

#### Imgix

As noted above, by default, Twill uses and recommends using [Imgix](https://imgix.com) to transform, optimize, and intelligently cache your uploaded images.
Expand Down
44 changes: 37 additions & 7 deletions src/Services/MediaLibrary/Glide.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,9 @@ public function render($path)
public function getUrl($id, array $params = [])
{
$defaultParams = config('twill.glide.default_params');
$addParamsToSvgs = config('twill.glide.add_params_to_svgs', false);

if (!$addParamsToSvgs && Str::endsWith($id, '.svg')) {
return $this->urlBuilder->getUrl($id);
}

return $this->urlBuilder->getUrl($id, array_replace($defaultParams, $params));
return $this->getOriginalMediaUrl($id) ??
$this->urlBuilder->getUrl($id, array_replace($defaultParams, $params));
}

/**
Expand Down Expand Up @@ -198,7 +194,7 @@ public function getPresetUrl($id, $preset)
*/
public function getRawUrl($id)
{
return $this->urlBuilder->getUrl($id);
return $this->getOriginalMediaUrl($id) ?? $this->urlBuilder->getUrl($id);
}

/**
Expand Down Expand Up @@ -273,4 +269,38 @@ protected function getFocalPointCrop($crop_params, $width, $height)

return [];
}

/**
* @param string $id
* @return string
*/
private function getOriginalMediaUrl($id)
{
$libraryDisk = $this->config->get('twill.media_library.disk');
$endpointType = $this->config->get('twill.media_library.endpoint_type');
$localMediaLibraryUrl = $this->config->get("filesystems.disks.$libraryDisk.url");
$originalMediaForExtensions = $this->config->get('twill.glide.original_media_for_extensions');
$addParamsToSvgs = $this->config->get('twill.glide.add_params_to_svgs', false);

if ((Str::endsWith($id, '.svg') && $addParamsToSvgs) || !Str::endsWith($id, $originalMediaForExtensions)) {
return null;
}

/** @var string $endpoint */
$endpoint;

switch ($endpointType) {
case 'local':
$endpoint = $localMediaLibraryUrl;
break;
case 's3':
$endpoint = s3Endpoint($libraryDisk);
break;
case 'azure':
$endpoint = azureEndpoint($libraryDisk);
break;
}

return $endpoint . '/' . $id;
}
}