PHPackages                             brammo/image - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. brammo/image

ActiveCakephp-plugin[Utility &amp; Helpers](/categories/utility)

brammo/image
============

Image helper for CakePHP

1.0(4mo ago)01MITPHPPHP &gt;=8.1

Since Dec 23Pushed 4mo agoCompare

[ Source](https://github.com/brammo/image)[ Packagist](https://packagist.org/packages/brammo/image)[ Docs](https://github.com/brammo/image)[ RSS](/packages/brammo-image/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (7)Versions (2)Used By (0)

Image helper
============

[](#image-helper)

[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](LICENSE)

A [CakePHP](https://cakephp.org/) plugin for image manipulation

Requirements
------------

[](#requirements)

- PHP 8.1+
- CakePHP 5.0+

Installation
------------

[](#installation)

You can install this plugin using [Composer](https://getcomposer.org):

```
composer require brammo/image
```

### Load the Plugin

[](#load-the-plugin)

Add the following to your `Application.php`:

```
public function bootstrap(): void
{
    parent::bootstrap();

    $this->addPlugin('Brammo/Image');
}
```

Or load via command line:

```
bin/cake plugin load Brammo/Image
```

Usage
-----

[](#usage)

Load the helpers in your `AppView.php`:

```
public function initialize(): void
{
    parent::initialize();

    // Load individual helpers
    $this->loadHelper('Brammo/Image.Image');
}
```

---

ImageHelper
-----------

[](#imagehelper)

Process and cache images with automatic resizing, cropping, and fitting operations. Supports GD, ImageMagick, and libvips image processing libraries via [Intervention Image](https://image.intervention.io/).

### Installation

[](#installation-1)

The ImageHelper requires the Intervention Image library (installed automatically):

```
composer require intervention/image
```

For libvips support (optional):

```
composer require intervention/image-driver-vips
```

### Loading the Helper

[](#loading-the-helper)

```
// In AppView.php
$this->loadHelper('Brammo/Image.Image');

// With custom configuration
$this->loadHelper('Brammo/Image.Image', [
    'tempFolder' => '/cache/images',  // Cache folder (relative to webroot)
    'driver' => 'auto',               // 'auto', 'imagick', 'vips', or 'gd'
    'backgroundColor' => '#ffffff',   // Background color for fit method
]);
```

### Configuration Options

[](#configuration-options)

OptionDefaultDescription`tempFolder``/thumb`Directory to store cached images (relative to webroot)`driver``auto`Image processing driver. Auto-detection priority: imagick &gt; vips &gt; gd`backgroundColor``#ffffff`Background fill color for the `fit` method (hex code)### Methods

[](#methods)

#### resize()

[](#resize)

Scale image to fit within specified dimensions while maintaining aspect ratio.

```
// Resize to fit within 200x200 (maintains aspect ratio)
echo $this->Image->resize('/images/photo.jpg', 200, 200);
// Returns: /thumb/200x200/images/photo.jpg

// Square shorthand (height = width)
echo $this->Image->resize('/images/photo.jpg', 200);

// Convert to WebP format
echo $this->Image->resize('/images/photo.jpg', 200, 200, 'webp');
// Returns: /thumb/200x200/images/photo.webp
```

#### crop()

[](#crop)

Crop image to exact dimensions from center. Image is scaled to cover the target dimensions, then center-cropped.

```
// Crop to exactly 100x100
echo $this->Image->crop('/images/photo.jpg', 100, 100);
// Returns: /thumb/100x100c/images/photo.jpg

// Square shorthand
echo $this->Image->crop('/images/photo.jpg', 100);
```

#### fit()

[](#fit)

Fit image within dimensions and fill remaining space with background color.

```
// Fit within 300x200, pad with white background
echo $this->Image->fit('/images/photo.jpg', 300, 200);
// Returns: /thumb/300x200f/images/photo.jpg

// Custom background color (configured in helper options)
$this->loadHelper('Brammo/Image.Image', [
    'backgroundColor' => '#000000',  // Black background
]);
```

#### getDriverInfo()

[](#getdriverinfo)

Get information about available image processing drivers.

```
$info = $this->Image->getDriverInfo();
// Returns:
// [
//     'driver' => 'imagick',  // Currently selected driver
//     'available' => [
//         'imagick' => true,
//         'vips' => false,
//         'gd' => true,
//     ],
// ]
```

### Cache Path Structure

[](#cache-path-structure)

Processed images are cached with the following path structure:

```
{tempFolder}/{width}x{height}{suffix}/{original/path/structure}/{filename}.{format}

```

MethodSuffixExampleresize(none)`/thumb/200x200/images/products/photo.jpg`crop`c``/thumb/200x200c/images/products/photo.jpg`fit`f``/thumb/200x200f/images/products/photo.jpg`The original directory structure is preserved to avoid filename collisions from different folders.

### Supported Formats

[](#supported-formats)

FormatExtensionJPEG`jpg`, `jpeg`PNG`png`GIF`gif`WebP`webp`AVIF`avif`BMP`bmp`### Error Handling

[](#error-handling)

- If the source image doesn't exist, the original path is returned
- If processing fails, the original path is returned
- If the cache directory is not writable, a `RuntimeException` is thrown

### Example Usage in Templates

[](#example-usage-in-templates)

```
// Product thumbnails
