Skip to content

Commit

Permalink
Merge pull request #14 from sorenmalling/task/imageoptions-configuration
Browse files Browse the repository at this point in the history
TASK: Move imageOptions to configuration
  • Loading branch information
kdambekalns authored Aug 12, 2022
2 parents 1b5a49b + 747f709 commit 11dcf30
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 19 deletions.
14 changes: 13 additions & 1 deletion Classes/AssetSource/PixxioAssetSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class PixxioAssetSource implements AssetSourceInterface
*/
private $apiClientOptions = [];

/**
* @var array
*/
private $imageOptions = [];

/**
* @var string
*/
Expand Down Expand Up @@ -145,6 +150,12 @@ public function __construct(string $assetSourceIdentifier, array $assetSourceOpt
}
$this->apiClientOptions = $optionValue;
break;
case 'imageOptions':
if (!is_array($optionValue)) {
throw new \InvalidArgumentException(sprintf('Invalid image options specified for Pixx.io asset source %s', $assetSourceIdentifier), 1591605349);
}
$this->imageOptions = $optionValue;
break;
case 'sharedRefreshToken':
if (!is_string($optionValue) || empty($optionValue)) {
throw new \InvalidArgumentException(sprintf('Invalid shared refresh token specified for Pixx.io asset source %s', $assetSourceIdentifier), 1528806843);
Expand Down Expand Up @@ -292,7 +303,8 @@ public function getPixxioClient(): PixxioClient
$account->getAccountIdentifier(),
$this->apiEndpointUri,
$this->apiKey,
$this->apiClientOptions
$this->apiClientOptions,
$this->imageOptions
);

$this->pixxioClient->authenticate($clientSecret->getRefreshToken());
Expand Down
53 changes: 37 additions & 16 deletions Classes/Service/PixxioClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,50 @@ final class PixxioClient
* @param string $apiEndpointUri
* @param string $apiKey
* @param array $apiClientOptions
* @param array $imageOptions
*/
public function __construct(string $apiEndpointUri, string $apiKey, array $apiClientOptions)
public function __construct(string $apiEndpointUri, string $apiKey, array $apiClientOptions, array $imageOptions)
{
$this->apiEndpointUri = $apiEndpointUri;
$this->apiKey = $apiKey;
$this->apiClientOptions = $apiClientOptions;
$this->guzzleClient = new Client($this->apiClientOptions);
$this->imageOptions = [
(object)[
'width' => 400,
'height' => 400,
'quality' => 90
],
(object)[
'width' => 1500,
'height' => 1500,
'quality' => 90
],
(object)[
'sizeMax' => 1920,
'quality' => 90
]
$this->configureImageOptionsWithFallback($imageOptions);
}

private function configureImageOptionsWithFallback(array $imageOptions): void
{
$imageOptionsPresets = [
'thumbnailUri' =>
[
'width' => 400,
'height' => 400,
'quality' => 90
],
'previewUri' =>
[
'width' => 1500,
'height' => 1500,
'quality' => 90
],
'originalUri' =>
[
'sizeMax' => 1920,
'quality' => 90
]
];
foreach ($imageOptionsPresets as $imageOptionPresetKey => $imageOptionPresetConfiguration) {
if (isset($imageOptions[$imageOptionPresetKey]) === false) {
$this->imageOptions[] = $imageOptionPresetConfiguration;
continue;
}
$imageOption = $imageOptions[$imageOptionPresetKey];
if (isset($imageOption['crop']) && $imageOption['crop'] === false && isset($imageOption['height'])) {
unset($imageOption['height']);
unset($imageOption['crop']);
}
$this->imageOptions[] = $imageOption;
}
}

/**
Expand Down
6 changes: 4 additions & 2 deletions Classes/Service/PixxioServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ class PixxioServiceFactory
* @param string $apiEndpointUri
* @param string $apiKey
* @param array $apiClientOptions
* @param array $imageOptions
* @return PixxioClient
*/
public function createForAccount(string $accountIdentifier, string $apiEndpointUri, string $apiKey, array $apiClientOptions)
public function createForAccount(string $accountIdentifier, string $apiEndpointUri, string $apiKey, array $apiClientOptions, array $imageOptions)
{
$client = new PixxioClient(
$apiEndpointUri,
$apiKey,
$apiClientOptions
$apiClientOptions,
$imageOptions
);
return $client;
}
Expand Down
17 changes: 17 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ Neos:
enable: false
inUseTag: 'used-by-neos'

# Image options, are the formats returned for use in the asset properties "thumbnailUri", "previewUri" and "originalUri"
# imageOptions parameter are described here: https://tutorial.pixxio.cloud/cgi-bin/api/pixxio-api.pl/documentation/generalInformation/imageOptions
# Setting the "crop" parameter, removes the "height" attribute, and creates a non-cropped version
imageOptions:
thumbnailUri:
width: 400
height: 400
quality: 90
crop: false
previewUri:
width: 1500
height: 1500
quality: 90
originalUri:
sizeMax: 1920
quality: 90

# The icon for the asset source
icon: 'resource://Flownative.Pixxio/Public/Icons/PixxioWhite.svg'

Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,39 @@ Neos:
'verify': '/path/to/cert.pem'
```
## Using ImageOptions parameters for files
Via configuration, you can set what dimensions the returned images must have. The sizes are defined by the following keys:
* `thumbnailUri` used in the media browser list
* `previewUri` used in the detail page of a asset
* `originalUri` used for downloading the asset

The configuration is by default

```yaml
Media:
assetSources:
'flownative-pixxio':
imageOptions:
thumbnailUri:
width: 400
height: 400
quality: 90
crop: false
previewUri:
width: 1500
height: 1500
quality: 90
originalUri:
sizeMax: 1920
quality: 90
```

Each imageOptions can be overridden from your own packages configuration, by addressing the specific preset key.

By default, the assets from Pixx.io is returned in a cropped format. When this is the case, a editor can't see if a asset is horizontal or vertical, when looking in the Media Browser list.
By setting `crop: false` the image will be returned in a not-cropped version, and it's visible for the editor, to see the assets orientation

## Cleaning up unused assets

Whenever a pixx.io asset is used in Neos, the media file will be copied automatically to the internal Neos asset
Expand Down

0 comments on commit 11dcf30

Please sign in to comment.