Skip to content

Commit

Permalink
Update to PHP 8.2 and Nette 3+
Browse files Browse the repository at this point in the history
  • Loading branch information
harmim committed Jun 30, 2023
1 parent 50dbcda commit 8b50674
Show file tree
Hide file tree
Showing 15 changed files with 632 additions and 495 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -11,11 +11,11 @@ VENDOR_DIR := vendor
VENDOR_BIN_DIR := $(VENDOR_DIR)/bin

CODE_CHECKER := nette/code-checker
CODE_CHECKER_VERSION := ~3.2.3
CODE_CHECKER_VERSION := ~3.3.0
CODE_CHECKER_DIR := $(TEMP_DIR)/code-checker

COVERALLS := php-coveralls/php-coveralls
COVERALLS_VERSION := ^2.5.3
COVERALLS_VERSION := ^2.5
COVERALLS_DIR := $(TEMP_DIR)/coveralls

ifeq ($(CI), 1)
Expand Down
111 changes: 53 additions & 58 deletions README.md
Expand Up @@ -19,7 +19,7 @@ There is `Image storage` for storing images easily and/or deleting them from the
There are also several ways how to resize and/or process images. Then, you can get a stored image path directly,
or you can use prepared [Latte](https://latte.nette.org) macros to generate HTML tags. See [Usage](#Usage).

**Requires the PHP version `7.4` or newer and PHP extensions `gd` and `fileinfo`.**
**Requires the PHP version `8.2` or newer and PHP extensions `fileinfo`, `gd`, and `intl`.**


## Installation
Expand All @@ -32,14 +32,11 @@ composer require harmim/images

## Usage

For working with images, we need `Harmim\Images\ImageStorage`:
For working with images, we need `\Harmim\Images\ImageStorage`:

### Without Nette

```php
use Harmim\Images\DI\ImagesExtension;
use Harmim\Images\ImageStorage;

$customConfig = [
'wwwDir' => __DIR__ . DIRECTORY_SEPARATOR . 'www',
'compression' => 90,
Expand All @@ -48,17 +45,16 @@ $customConfig = [
'img-small' => [
'width' => 50,
'height' => 50,
'transform' => ImageStorage::RESIZE_EXACT,
'transform' => \Harmim\Images\Resize::EXACT,
...
],
...
],
...
];

$imageStorage = new ImageStorage(array_merge_recursive(
ImagesExtension::DEFAULTS,
$imageStorage = new \Harmim\Images\ImageStorage(\Nette\Utils\Arrays::mergeTree(
$customConfig,
\Harmim\Images\DI\ImagesExtension::DEFAULTS,
));
```

Expand All @@ -69,7 +65,7 @@ In `$customConfig`, you can specify a custom configuration. See [Configuration](
You can enable and customise the extension using your NEON config:
```neon
extensions:
images: Harmim\Images\DI\ImagesExtension
images: \Harmim\Images\DI\ImagesExtension
images:
compression: 90
Expand All @@ -78,53 +74,46 @@ images:
img-small:
width: 50
height: 50
transform: Harmim\Images\ImageStorage::RESIZE_EXACT
transform: ::constant(\Harmim\Images\Resize::EXACT)
...
...
...
```

In the `images` section, you can specify a custom configuration. See [Configuration](#Configuration).

`Harmim\Images\ImageStorage` is now registrated in the DI container. You can get it directly from the container:
`\Harmim\Images\ImageStorage` is now registrated in the DI container. You can get it directly from the container:
```php
use Harmim\Images\ImageStorage;

/** @var Nette\DI\Container $container */

/** @var \Nette\DI\Container $container */
$imageStorage = $container->getService('images.imageStorage');
// or
$imageStorage = $container->getByType(ImageStorage::class);
$imageStorage = $container->getByType(\Harmim\Images\ImageStorage::class);
```

Of course, you can inject `Harmim\Images\ImageStorage` through a constructor, inject method, inject annotation, or
Of course, you can inject `\Harmim\Images\ImageStorage` through a constructor, inject method, inject annotation, or
any other way.

If you want to use `Harmim\Images\ImageStorage` in a presenter or control where inject methods are called, you
can use trait `Harmim\Images\TImageStorage`. In your presenters, controls, and theire templates, there will be
If you want to use `\Harmim\Images\ImageStorage` in a presenter or control where inject methods are called, you
can use trait `\Harmim\Images\TImageStorage`. In your presenters, controls, and theire templates, there will be
variable `$imageStorage`.
```php
use Harmim\Images\TImageStorage
use Nette\Application\UI\Control;
use Nette\Application\UI\Presenter;

abstract class BasePresenter extends Presenter
abstract class BasePresenter extends \Nette\Application\UI\Presenter
{
use TImageStorage;
use \Harmim\Images\TImageStorage;
}

abstract class BaseControl extends Control
abstract class BaseControl extends \Nette\Application\UI\Control
{
use TImageStorage;
use \Harmim\Images\TImageStorage;
}
```

The extension installs images macros to Latte. See [Macros](#Macros).

### Storing Images

You can store an image using method `Harmim\Images\ImageStorage::saveImage(string $name, string $path): string` or
`Harmim\Images\ImageStorage::saveUpload(Nette\Http\FileUpload $file): string`. An original image will be stored;
You can store an image using method `\Harmim\Images\ImageStorage::saveImage(string $name, string $path): string` or
`\Harmim\Images\ImageStorage::saveUpload(\Nette\Http\FileUpload $file): string`. An original image will be stored;
then, it will be compresed.

Both methods return a stored image file name. You can use this file name to delete, resize, or retrieve the image.
Expand All @@ -133,32 +122,36 @@ Images are stored with a unique file name and location.

### Deleting Images

Using method `Harmim\Images\ImageStorage::deleteImage(string $fileName, array $excludedTypes = []): void`,
you can delete an image by `$fileName` which should be a file name returned by `Harmim\Images\ImageStorage::saveImage`
or `Harmim\Images\ImageStorage::saveUpload`.
Using method `\Harmim\Images\ImageStorage::deleteImage(string $fileName, array $excludedTypes = []): void`,
you can delete an image by `$fileName` which should be a file name returned by `\Harmim\Images\ImageStorage::saveImage`
or `\Harmim\Images\ImageStorage::saveUpload`.

If you pass `$excludedTypes`, only other types will be deleted; otherwise, all types, the original image, and
the compressed image will be deleted.

### Getting Stored Images' Paths

You can get a stored image path using method
`Harmim\Images\ImageStorage::getImageLink(string $fileName, ?string $type = null, array $options = []): ?Harmim\Images\Image`
or [Macros](#Macros). You can pass a specific type defined in inital options, or you can pass specific options.
See [Configuration](#Configuration). `$fileName` should be a file name returned by
`Harmim\Images\ImageStorage::saveImage`or `Harmim\Images\ImageStorage::saveUpload`.
`\Harmim\Images\ImageStorage::getImageLink(string $fileName, ?string $type = null, array $config = []): ?\Harmim\Images\Image`
or [Macros](#Macros). You can pass a specific type defined in an inital configuration, or you can pass a specific
configuration. See [Configuration](#Configuration). `$fileName` should be a file name returned by
`\Harmim\Images\ImageStorage::saveImage`or `\Harmim\Images\ImageStorage::saveUpload`.

If you try to get an image of a size or a type for a first time, this image is not yet created, so it will be created
now. Next time, you will get a resized image.

If the image does not exist, a placeholder will be returned.

In case you need to get an original/compressed image, in the configuration, you can use the
`\Harmim\Images\ImageStorage::RETURN_ORIG/RETURN_COMPRESSED` constant, respectively. For example,
`[\Harmim\Images\ImageStorage::RETURN_ORIG => true]`. It is also possible to use these options in macros.

### Macros

#### `img`

```latte
{img [$image] [image-type] [options]}
{img [$image] [image-type] [config]}
```
Renders the `img` tag:
```html
Expand Down Expand Up @@ -189,7 +182,7 @@ Examples:
#### `n:img`

```latte
<img n:img="[$image] [image-type] [options]" alt="foo">
<img n:img="[$image] [image-type] [config]" alt="foo">
```
Renders the `src` attribute. It can be used, e.g., in the `img` element.

Expand All @@ -209,7 +202,7 @@ Examples:
#### `imgLink`

```latte
{imgLink [$image] [image-type] [options]}
{imgLink [$image] [image-type] [config]}
```
Returns a relative path (from the resource root directory) to a given image.

Expand Down Expand Up @@ -245,40 +238,42 @@ Examples:
* Default: `1024`.
- `height`: (`int`) An image height.
* Default: `1024`.
- `compression`: (`int`) A compression quality. See `Nette\Utils\Image::save`.
- `compression`: (`int`) A compression quality. See `\Nette\Utils\Image::save`.
* Default: `85`.
- `transform`: (`string`) One of `Harmim\Images\ImageStorage::RESIZE_...` constants, or more constants separated by `|`:

| Option | Description |
|-----------------------|-------------------------------------------------------------------------------|
| `RESIZE_SHRINK_ONLY` | Only shrinking (prevents a small image from being stretched). |
| `RESIZE_STRETCH` | Do not keep the aspect ratio. |
| `RESIZE_FIT` | The resulting dimensions will be smaller or equal to the required dimensions. |
| `RESIZE_FILL` | Fills (and possibly exceeds in one dimension) the target area. |
| `RESIZE_EXACT` | Fills the target area and cuts off what goes beyond. |
| `RESIZE_FILL_EXACT` | Placees a not stretched image to the exact blank area. |

* Default: ` Harmim\Images\ImageStorage::RESIZE_FIT`.
- `imgTagAttributes`: (`array`) `img` attributes you can use in the `{img}` Latte macro, other attributes are ignored.
- `transform`: (`\Harmim\Images\Resize|list<\Harmim\Images\Resize>`) See [Transform-Options](#Transform-Options).
* Default: `\Harmim\Images\Resize::OR_SMALLER`.
- `imgTagAttributes`: (`list<string>`) `img` attributes you can use in the `{img}` Latte macro, other attributes
are ignored.
* Default: `[alt, height, width, class, hidden, id, style, title, data]`.
- `types`: (`array`) A configuration for image types overriding the default configuration.
- `types`: (`array<string, mixed>`) A configuration for image types overriding the default configuration.
* Default: `[]`.
* Example:

```neon
types:
img-small:
width: 50
height: 50
img-gallery:
lazy: true
transform: Harmim\Images\ImageStorage::RESIZE_STRETCH
transform:
- ::constant(\Harmim\Images\Resize::STRETCH)
- ::constant(\Harmim\Images\Resize::COVER)
```

- `lazy`: (`bool`) Render the `{img}` Latte macro as a lazy image (with the `data-src` attribute, `lazy` class, and
normal `img` tag in the `noscript` tag).
* Default: `false`.

### Transform-Options

| Option | Description |
|--------------------------------------|-------------------------------------------------------------------------------|
| `\Harmim\Images\Resize::SHRINK_ONLY` | Only shrinking (prevents a small image from being stretched). |
| `\Harmim\Images\Resize::STRETCH` | Do not keep the aspect ratio. |
| `\Harmim\Images\Resize::OR_SMALLER` | The resulting dimensions will be smaller or equal to the required dimensions. |
| `\Harmim\Images\Resize::OR_BIGGER` | Fills (and possibly exceeds in one dimension) the target area. |
| `\Harmim\Images\Resize::COVER` | Fills the target area and cuts off what goes beyond. |
| `\Harmim\Images\Resize::EXACT` | Placees a not stretched image to the exact blank area. |


## License

Expand Down
24 changes: 14 additions & 10 deletions composer.json
Expand Up @@ -14,26 +14,30 @@
}
],
"require": {
"php": ">=7.4",
"ext-gd": "*",
"php": ">=8.2",
"ext-fileinfo": "*",
"nette/di": "^2.4",
"nette/utils": "^2.4",
"latte/latte": "^2.4",
"nette/http": "^2.4",
"nette/finder": "^2.4"
"ext-gd": "*",
"ext-intl": "*",
"latte/latte": "^3.0",
"nette/di": "^3.1",
"nette/http": "^3.2",
"nette/utils": "^4.0"
},
"require-dev": {
"nette/tester": "^2.0"
"nette/application": "^3.1",
"nette/tester": "^2.5"
},
"suggest": {
"nette/application": "To use the 'Harmim\\Images\\TImageStorage' trait."
},
"autoload": {
"psr-4": {
"Harmim\\Images\\": "src/"
"Harmim\\Images\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Harmim\\Tests\\": "tests/"
"Harmim\\Tests\\": "tests"
}
}
}
12 changes: 8 additions & 4 deletions docker/php/Dockerfile
@@ -1,17 +1,21 @@
# Author: Dominik Harmim <harmim6@gmail.com>

FROM php:7.4-cli
FROM php:8.2-cli

RUN apt-get update -y && \
apt-get install -y \
git \
zip \
libjpeg-dev \
libpng-dev && \
libpng-dev \
libicu-dev && \
apt-get clean -y

RUN docker-php-ext-configure gd --with-jpeg
RUN docker-php-ext-install gd
RUN docker-php-ext-configure gd --with-jpeg && \
docker-php-ext-configure intl
RUN docker-php-ext-install \
gd \
intl

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
php composer-setup.php --install-dir=/usr/bin --filename=composer --quiet && \
Expand Down

0 comments on commit 8b50674

Please sign in to comment.