Skip to content

Commit

Permalink
issue-#169: Output auto problem , closes #169
Browse files Browse the repository at this point in the history
  • Loading branch information
sadok-f committed Aug 3, 2018
1 parent de6b27c commit 3cadf30
Showing 1 changed file with 13 additions and 29 deletions.
42 changes: 13 additions & 29 deletions src/Core/Entity/Image/OutputImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ class OutputImage
* OutputImage constructor.
*
* @param InputImage $inputImage
*
* @throws InvalidArgumentException
*/
public function __construct(InputImage $inputImage)
{
$this->inputImage = $inputImage;
$this->generateFilesName();
$this->generateFileExtension();
$this->outputImageExtension = $this->generateFileExtension();
$this->outputImagePath .= '.'.$this->outputImageExtension;
$this->outputImageName .= '.'.$this->outputImageExtension;
}

/**
Expand Down Expand Up @@ -166,39 +170,19 @@ protected function generateFileExtension()
{
$requestedOutput = $this->extractKey('output');

if ($requestedOutput == self::EXT_INPUT) {
$resolvedExtension = $this->extensionByMimeType($this->inputImage->sourceImageMimeType());
} elseif ($requestedOutput == self::EXT_AUTO) {
$resolvedExtension = $this->autoExtension();
} else {
if (!in_array($requestedOutput, $this->allowedOutExtensions)) {
// Maybe trow exception only when in debug mode ?
throw new InvalidArgumentException("Invalid file output requested : ".$requestedOutput);
}
$resolvedExtension = $requestedOutput;
if ($requestedOutput == self::EXT_AUTO && $this->isWebPBrowserSupported()) {
return self::EXT_WEBP;
}

$this->outputImageExtension = $resolvedExtension;
$this->outputImagePath .= '.'.$this->outputImageExtension;
$this->outputImageName .= '.'.$this->outputImageExtension;
}
if ($requestedOutput == self::EXT_INPUT || $requestedOutput == self::EXT_AUTO) {
return $this->extensionByMimeType($this->inputImage->sourceImageMimeType());
}

/**
* This method defines what extension / format to use in the output image, using the following criteria:
* 1. Optimal image format for the requesting browser
* 2. Source image format
* 3. JPG
*
* @return string One image extension
*/
protected function autoExtension(): string
{
// for now AUTO means webP, or ...
if ($this->isWebPBrowserSupported()) {
return self::EXT_WEBP;
if (!in_array($requestedOutput, $this->allowedOutExtensions)) {
throw new InvalidArgumentException("Invalid file output requested : ".$requestedOutput);
}

return $this->extensionByMimeType($this->inputImage->sourceImageMimeType());
return $requestedOutput;
}

/**
Expand Down

0 comments on commit 3cadf30

Please sign in to comment.