Skip to content

Commit

Permalink
Merge pull request #108 from Ruitjes/add-webp-support
Browse files Browse the repository at this point in the history
Added new method for intervention WebP support
  • Loading branch information
ctessier authored Aug 28, 2023
2 parents 675329c + c898e02 commit c76885e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ AdvancedImage::make('Photo')->resize(600, 400),
AdvancedImage::make('Photo')->resize(null, 300),
```

*Note: this method uses [Intervention Image `resize()`](https://image.intervention.io/v2/api/resize) with the upsize and aspect ratio constraints.*
_Note: this method uses [Intervention Image `resize()`](https://image.intervention.io/v2/api/resize) with the upsize and aspect ratio constraints._

### `autoOrientate()`

Expand All @@ -118,7 +118,20 @@ This can be mandatory in some cases for the cropper to work properly.
AdvancedImage::make('Photo')->autoOrientate(),
```

*Note: PHP must be compiled in with `--enable-exif` to use this method. Windows users must also have the mbstring extension enabled. See [the Intervention Image documentation](https://image.intervention.io/v2/api/orientate) for more details.*
_Note: PHP must be compiled in with `--enable-exif` to use this method. Windows users must also have the mbstring extension enabled. See [the Intervention Image documentation](https://image.intervention.io/v2/api/orientate) for more details._

### `convert(string $format)`

Specify the desired output image format.

```php
AdvancedImage::make('Photo')->convert('webp'),
```

_Note: _

- _To see all the supported formats that can be used to convert your files see [Intervention Image `encode()`]:(https://image.intervention.io/v2/api/encode._
- _For WebP support GD driver must be used with PHP 5 >= 5.5.0 or PHP 7 in order to use imagewebp(). If Imagick is used, it must be compiled with libwebp for WebP support._

### `quality(int $quality)`

Expand All @@ -130,4 +143,4 @@ This only applies to JPG format as PNG compression is lossless. The value must r
AdvancedImage::make('Photo')->resize(600, 400)->quality(95),
```

*Note: the quality will be passed to the [Intervention Image `save()`](https://image.intervention.io/v2/api/save) method.*
_Note: the quality will be passed to the [Intervention Image `save()`](https://image.intervention.io/v2/api/save) method._
45 changes: 44 additions & 1 deletion src/TransformableImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ trait TransformableImage
*/
private $quality = 90;

/**
* The format of the resulting image.
*
* @var string
*/
private $outputFormat;

/**
* The Intervention Image instance.
*
Expand Down Expand Up @@ -163,6 +170,28 @@ public function quality(int $quality)
return $this;
}

/**
* Specify the desired output image format.
* This method sets the output format to be used by Intervention.
*
* @throws \Exception
*
* @return $this
*/
public function convert(string $format)
{
/**
* @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");
}

$this->outputFormat = $format;

return $this;
}

/**
* Transform the uploaded file.
*
Expand Down Expand Up @@ -191,7 +220,11 @@ public function transformImage(UploadedFile $uploadedFile, ?object $cropperData)
$this->resizeImage();
}

$this->image->save($uploadedFile->getPathName(), $this->quality, $uploadedFile->getClientOriginalExtension());
if ($this->outputFormat) {
$this->convertImage($this->outputFormat);
}

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

Expand Down Expand Up @@ -229,4 +262,14 @@ private function orientateImage()
{
$this->image->orientate();
}

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

0 comments on commit c76885e

Please sign in to comment.