Skip to content

Commit

Permalink
Refactor TransformableImage for new convert method
Browse files Browse the repository at this point in the history
  • Loading branch information
ctessier authored Aug 28, 2023
1 parent 74c1535 commit c898e02
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/TransformableImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,12 @@ trait TransformableImage
private $quality = 90;

/**
* Indicates if the image is croppable.
* The format of the resulting image.
*
* @var bool
* @var string
*/
private $outputFormat;

/**
* Indicates the allowed extensions for the output format.
*
* @var array
*/
private $allowedExtensions = ['jpg', 'png', 'gif', 'tif', 'bmp', 'ico', 'psd', 'webp', 'data-url'];

/**
* The Intervention Image instance.
*
Expand Down Expand Up @@ -185,9 +178,12 @@ public function quality(int $quality)
*
* @return $this
*/
public function convert($format)
public function convert(string $format)
{
if (!in_array($format, $this->allowedExtensions)) {
/**
* @See https://image.intervention.io/v2/api/encode
*/
if (!in_array($format, ['jpg', 'png', 'gif', 'tif', 'bmp', 'ico', 'psd', 'webp', 'data-url'])) {
throw new \Exception("Unsupported output format: $format");
}

Expand Down Expand Up @@ -228,7 +224,7 @@ public function transformImage(UploadedFile $uploadedFile, ?object $cropperData)
$this->convertImage($this->outputFormat);
}

$this->image->save($uploadedFile->getPathName(), $this->quality, $this->outputFormat ? $this->outputFormat : $uploadedFile->getClientOriginalExtension());
$this->image->save($uploadedFile->getPathName(), $this->quality, $this->outputFormat ?? $uploadedFile->getClientOriginalExtension());
$this->image->destroy();
}

Expand Down Expand Up @@ -268,12 +264,12 @@ private function orientateImage()
}

/**
* Encodes the image to the given format.
* Encode the image to the given format.
*
* @return void
*/
private function convertImage($format)
private function convertImage(string $format)
{
$this->image->encode($format);
$this->image->encode($format, $this->quality);
}
}

0 comments on commit c898e02

Please sign in to comment.