Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 8.0+ version #316

Merged
merged 11 commits into from
Feb 27, 2023
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ root = true

[*]
indent_style = space
indent_size = 2
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
Expand Down
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/examples export-ignore
/.github export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
14 changes: 13 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# System files
# ------------
.DS_Store
test/

# Editors
# (sensitive workspace files)
# ---------------------------
*.sublime-project
*.sublime-workspace
/.vscode
/.idea
/.nova

vendor/
91 changes: 78 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ try {

## Requirements

- PHP 5.6+
- PHP 8.0+
- [GD extension](http://php.net/manual/en/book.image.php)

## Features

- Supports reading, writing, and converting GIF, JPEG, PNG, WEBP, BMP formats.
- Supports reading, writing, and converting GIF, JPEG, PNG, WEBP, BMP, AVIF formats.
- Reads and writes files, data URIs, and image strings.
- Manipulation: crop, resize, overlay/watermark, adding TTF text
- Drawing: arc, border, dot, ellipse, line, polygon, rectangle, rounded rectangle
- Filters: blur, brighten, colorize, contrast, darken, desaturate, edge detect, emboss, invert, opacity, pixelate, sepia, sharpen, sketch
- Utilities: color adjustment, darken/lighten color, extract colors
- Properties: exif data, height/width, mime type, orientation
- Color arguments can be passed in as any CSS color (e.g. `LightBlue`), a hex color, or an RGB(A) array.
- Support for alpha-transparency (GIF, PNG, WEBP)
- Support for alpha-transparency (GIF, PNG, WEBP, AVIF)
- Chainable methods
- Uses exceptions
- Load with Composer or manually (just one file)
Expand Down Expand Up @@ -141,52 +141,117 @@ Returns a SimpleImage object.

### Savers

#### `toDataUri($mimeType, $quality)`
#### `toDataUri($mimeType, $options)`

Generates a data URI.

- `$mimeType` (string) - The image format to output as a mime type (defaults to the original mime type).
- `$quality` (int) - Image quality as a percentage (default 100). This argument has no effect on PNG images, since the format is lossless.
- `$options` (array|int) - Array of options or Image quality as a percentage (default 100).

Returns a string containing a data URI.

#### `toDownload($filename, $mimeType, $quality)`
#### `toDownload($filename, $mimeType, $options)`

Forces the image to be downloaded to the clients machine. Must be called before any output is sent to the screen.

- `$filename`* (string) - The filename (without path) to send to the client (e.g. 'image.jpeg').
- `$mimeType` (string) - The image format to output as a mime type (defaults to the original mime type).
- `$quality` (int) - Image quality as a percentage (default 100). This argument has no effect on PNG images, since the format is lossless.
- `$options` (array|int) - Array of options or Image quality as a percentage (default 100).

Returns a SimpleImage object.

#### `toFile($file, $mimeType, $quality)`
#### `toFile($file, $mimeType, $options)`

Writes the image to a file.

- `$mimeType` (string) - The image format to output as a mime type (defaults to the original mime type).
- `$quality` (int) - Image quality as a percentage (default 100). This argument has no effect on PNG images, since the format is lossless.
- `$options` (array|int) - Array of options or Image quality as a percentage (default 100).

Returns a SimpleImage object.

#### `toScreen($mimeType, $quality)`
#### `toScreen($mimeType, $options)`

Outputs the image to the screen. Must be called before any output is sent to the screen.

- `$mimeType` (string) - The image format to output as a mime type (defaults to the original mime type).
- `$quality` (int) - Image quality as a percentage (default 100). This argument has no effect on PNG images, since the format is lossless.
- `$options` (array|int) - Array of options or Image quality as a percentage (default 100).

Returns a SimpleImage object.

#### `toString($mimeType, $quality)`
#### `toString($mimeType, $options)`

Generates an image string.

- `$mimeType` (string) - The image format to output as a mime type (defaults to the original mime type).
- `$quality` (int) - Image quality as a percentage (default 100). This argument has no effect on PNG images, since the format is lossless.
- `$options` (array|int) - Array of options or Image quality as a percentage (default 100).

Returns a SimpleImage object.

#### 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.

```php
$image->toFile($file, 'image/avif', [
// JPG, WEBP, AVIF (default 100)
'quality' => 100,

// AVIF (default -1 which is 6)
// range of slow and small file 0 to 10 fast but big file
'speed' => -1,
]);
```

```php
$image->toFile($file, 'image/bmp', [
// BMP: boolean (default true)
'compression' => true,

// BMP, JPG (default null, keep the same)
'interlace' => null,
]);
```

```php
$image->toFile($file, 'image/gif', [
// GIF, PNG (default true)
'alpha' => true,
]);
```

```php
$image->toFile($file, 'image/jpeg', [
// BMP, JPG (default null, keep the same)
'interlace' => null,

// JPG, WEBP, AVIF (default 100)
'quality' => 100,
]);
```

```php
$image->toFile($file, 'image/png', [
// GIF, PNG (default true)
'alpha' => true,

// PNG: 0-10, defaults to zlib (default 6)
'compression' => -1,

// PNG (default -1)
'filters' => -1,

// has no effect on PNG images, since the format is lossless
// 'quality' => 100,
]);
```

```php
$image->toFile($file, 'image/webp', [
// JPG, WEBP, AVIF (default 100)
'quality' => 100,
]);
```

### Utilities

#### `getAspectRatio()`
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"description": "A PHP class that makes working with images as simple as possible.",
"license": "MIT",
"require": {
"php": ">=5.6.0",
"php": ">=8.0",
"ext-gd": "*",
"league/color-extractor": "0.3.*"
"league/color-extractor": "0.4.*"
},
"authors": [
{
Expand All @@ -18,5 +18,8 @@
"psr-0": {
"claviska": "src/"
}
},
"require-dev": {
"laravel/pint": "^1.5"
}
}
150 changes: 150 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified example/flag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 15 additions & 15 deletions example/index.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<?php

require '../src/claviska/SimpleImage.php';

// Ignore notices
error_reporting(E_ALL & ~E_NOTICE);

try {
// Create a new SimpleImage object
$image = new \claviska\SimpleImage();

// Manipulate it
$image
->fromFile('parrot.jpg') // load parrot.jpg
->autoOrient() // adjust orientation based on exif data
->bestFit(300, 600) // proportionally resize to fit inside a 250x400 box
->flip('x') // flip horizontally
->colorize('DarkGreen') // tint dark green
->border('black', 5) // add a 5 pixel black border
->overlay('flag.png', 'bottom right') // add a watermark image
->toScreen(); // output to the screen
// Create a new SimpleImage object
$image = new \claviska\SimpleImage();

// Manipulate it
$image
->fromFile('parrot.jpg') // load parrot.jpg
->autoOrient() // adjust orientation based on exif data
->bestFit(300, 600) // proportionally resize to fit inside a 250x400 box
->flip('x') // flip horizontally
->colorize('DarkGreen') // tint dark green
->border('black', 5) // add a 5 pixel black border
->overlay('flag.png', 'bottom right') // add a watermark image
->toScreen(); // output to the screen
} catch(Exception $err) {
// Handle errors
echo $err->getMessage();
// Handle errors
echo $err->getMessage();
}
Binary file added example/parrot.avif
Binary file not shown.
Binary file added example/parrot.bmp
Binary file not shown.
Binary file added example/parrot.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/parrot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/parrot.webp
Binary file not shown.
Loading