Skip to content

Commit

Permalink
Add reset and hasImage methods and make generate public (#339)
Browse files Browse the repository at this point in the history
* Add reset and hasImage methods

* Make generate method public

* Update README
  • Loading branch information
thisispiers authored Apr 15, 2024
1 parent 1dcb9c7 commit dfbe53c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ Generates an image string.

Returns a SimpleImage object.

#### `generate($mimeType, $options)`

Generates an image.

- `$mimeType` (string) - The image format to output as a mime type (defaults to the original mime type).
- `$options` (array|int) - Array of options or Image quality as a percentage (default 100).

Returns an array: [mimeType, data]

#### Options array

Instead of providing the quality as an integer as the last function parameter you can also set various options depending on the targeted Mime type using an associative array.
Expand Down Expand Up @@ -296,6 +305,18 @@ Gets the image's current width.

Returns the width as an integer.

#### `hasImage()`

Checks if the SimpleImage object has loaded an image.

Returns a boolean.

#### `reset()`

Destroys the image resource.

Returns a SimpleImage object.

### Manipulation

#### `autoOrient()`
Expand Down
26 changes: 22 additions & 4 deletions src/claviska/SimpleImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,33 @@ public function __construct(string $image = '', array $flags = [])
*/
public function __destruct()
{
if ($this->image instanceof GdImage) {
imagedestroy($this->image);
}
$this->reset();
}

//////////////////////////////////////////////////////////////////////////////////////////////////
// Helper functions
//////////////////////////////////////////////////////////////////////////////////////////////////

/**
* Checks if the SimpleImage object has loaded an image.
*/
public function hasImage(): bool
{
return $this->image instanceof GdImage;
}

/**
* Destroys the image resource.
*/
public function reset(): static
{
if ($this->hasImage()) {
imagedestroy($this->image);
}

return $this;
}

/**
* Set flag value.
*
Expand Down Expand Up @@ -313,7 +331,7 @@ public function fromString(string $string): SimpleImage|static
*
* @throws Exception Thrown when WEBP support is not enabled or unsupported format.
*/
protected function generate(string $mimeType = null, array|int $options = []): array
public function generate(string $mimeType = null, array|int $options = 100): array
{
// Format defaults to the original mime type
$mimeType = $mimeType ?: $this->mimeType;
Expand Down

0 comments on commit dfbe53c

Please sign in to comment.