pieceofcake2 / image
CakePHP 2 plugin that validates uploaded images by their contents: JPEG, PNG, GIF, WebP and strictly allowlisted SVG
Requires
- php: >=8.0
- ext-dom: *
- ext-libxml: *
- composer/installers: ^1.0 || ^2.0
- pieceofcake2/cakephp: ^2.12
Requires (Dev)
- ext-gd: *
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.6
- pieceofcake2/app: ^2.2
- pieceofcake2/phpcs-cakephp2: ^1.1
- pieceofcake2/phpstan-cakephp2: ^1.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-17 19:51:44 UTC
README
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
- Website: https://pieceofcake2.github.io/image/ (日本語: https://pieceofcake2.github.io/image/ja/)
- Markdown: docs/README.md (日本語: docs/ja/README.md) (for AI tools and offline reading)
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
domandlibxmlextensions - 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.