Search by

CakePHP 2 plugin that validates uploaded images by their contents: JPEG, PNG, GIF, WebP and strictly allowlisted SVG

Package info

github.com/pieceofcake2/image

Type:cakephp-plugin

pkg:composer/pieceofcake2/image

Statistics

Installs: 1 789

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-10-17 19:51 UTC

This package is auto-updated.

Last update: 2026-10-17 19:51:44 UTC


README

GitHub License Packagist Version PHP CakePHP PHPStan CI Codecov

A plugin for pieceofcake2/cakephp applications that validates uploaded images by their contents: the type (JPEG, PNG, GIF, WebP and SVG) and the size, and the extension to give the saved file. SVG documents are accepted only if they contain nothing but static graphics, with a strict allowlist: no scripts, event handlers, external references, animations or XML entities.

Documentation

It covers the validation rules, the settings, what SVG documents are accepted and how to serve uploaded SVG files safely.

The Markdown documentation is also included in the Composer package, in the plugin's directory: plugins/Image/docs/README.md (the location depends on your installer-paths).

Requirements

  • PHP 8.0+ with the dom and libxml extensions
  • pieceofcake2/cakephp 2.12+

Installation

composer require pieceofcake2/image

Load the plugin in config/bootstrap.php (legacy layout: app/Config/bootstrap.php):

CakePlugin::load('Image');

See Installation for the installer-paths setting.

Usage

class Hotel extends AppModel
{
    public $actsAs = ['Image.ImageValidation'];

    public $validate = [
        'photo' => [
            'type' => ['rule' => ['imageType'], 'message' => 'Select a JPEG or PNG image'],
            'width' => ['rule' => ['imageWidth', 295], 'message' => 'The width must be 295px'],
        ],
        'logo' => [
            'type' => ['rule' => ['imageType', ['jpeg', 'png', 'svg']], 'message' => 'Select a JPEG, PNG or SVG image'],
        ],
    ];
}

Save the file with the extension of its contents ($this->Hotel->imageExtension($file['tmp_name'])), and serve uploaded SVG files with a restrictive Content-Security-Policy. See Validation Rules and Serving Uploaded SVG Files.

Contributing

See the contribution guidelines.

License

Licensed under the MIT License. See LICENSE.