Init Image Processor
======================

A lightweight, self-hosted PHP image optimization engine that behaves
like a mini CDN. Fetches images from your server, processes them on
the fly (resize, crop, compress, convert to WebP/AVIF, watermark),
then serves optimized cached files with proper HTTP headers.

Ideal for developers who want full control over their media pipeline
without relying on external CDNs or cloud APIs.

----------------------------------------------------------------------
FEATURES
----------------------------------------------------------------------

- 100% self-hosted — no third-party CDN or cloud storage.
- Resize images using width/height parameters.
- Fit modes: cover, contain, and fill.
- Optional cropping for perfect aspect ratios.
- Smart compression with adjustable quality.
- Convert to WebP or AVIF (if supported by server).
- Optional watermark overlay with auto-scaling.
- Secure domain-restricted remote fetching.
- Cache system with:
  * file-based caching (with atomic write to avoid race conditions)
  * HTTP caching (ETag, max-age, Expires)
- Fully parameterized CDN-style URL controller:
  * src       — image path
  * w/h       — resize dimensions
  * q         — quality (1–100)
  * webp      — WebP output
  * avif      — AVIF output
  * crop      — enable cropping
  * fit       — cover/contain/fill
  * watermark — add PNG watermark
- Allowed resize sizes to prevent cache pollution attacks.
- MIME validation + image dimension pre-check for security.
- Pixel-limit protection for extremely large source images.
- Optional lottery-based cache garbage collection.
- Protects against invalid paths & directory traversal.
- Uses standard PHP GD functions — no external dependencies.

----------------------------------------------------------------------
CONFIGURATION
----------------------------------------------------------------------

Edit the CONFIG section at the top of `init-image-processor.php`:

define('INIT_IMG_SOURCE_DOMAIN', 'https://example.com');
define('INIT_IMG_CACHE_DIR', __DIR__ . '/cache');
define('INIT_IMG_ALLOW_REMOTE', true);
define('INIT_IMG_SSL_VERIFY', true);

Additional security/performance options:

define('INIT_IMG_ALLOWED_WIDTHS', [320, 480, 800, 1024, 1920]);
define('INIT_IMG_MAX_PIXELS', 30 * 1024 * 1024);
define('INIT_IMG_CACHE_GC_PROBABILITY', 0);   // 0 = off, 1000 = 1/1000 requests
define('INIT_IMG_MEMORY_LIMIT', '256M');

System requirements:
- PHP >= 7.4 (PHP 8+ recommended)
- Extensions: gd, fileinfo, curl (optional but recommended)
- Writable /cache directory (auto-created if missing)

Optional:
- Place `watermark.png` beside the script to enable watermarking.
- AVIF and WebP output depend on GD build features.

----------------------------------------------------------------------
USAGE
----------------------------------------------------------------------

1. Upload `init-image-processor.php` to your server.
2. Configure the allowed source domain and cache directory.
3. Request optimized images using URL parameters.

Example usage:

/init-image-processor.php?src=uploads/photo.jpg&w=800&q=85&webp=1

Common parameters:

- Resize only:
  /init-image-processor.php?src=img.jpg&w=1200

- WebP conversion:
  /init-image-processor.php?src=img.jpg&webp=1

- AVIF (if supported):
  /init-image-processor.php?src=img.jpg&avif=1

- Crop for fixed aspect ratio:
  /init-image-processor.php?src=banner.jpg&w=1200&h=600&crop=1

- Contain mode:
  /init-image-processor.php?src=img.jpg&w=600&h=600&fit=contain

- Add watermark:
  /init-image-processor.php?src=img.jpg&w=1000&watermark=1

Output is cached automatically and reused until expiration.

----------------------------------------------------------------------
NOTES
----------------------------------------------------------------------

- This is a fully self-hosted image CDN engine — no API keys needed.
- Atomic file writes prevent cache corruption on high-traffic sites.
- Allowed sizes protect against parameter pollution & cache spam.
- MIME validation prevents PHP or script files disguised as images.
- Pixel limit + optional memory limit help prevent memory exhaustion.
- Best paired with Nginx or Apache rewrites to create clean URLs.
- Remote fetching is domain-restricted for security.
- GIF input is supported but converted to static PNG/JPEG.
- Uses `ETag` and `Cache-Control` headers for browser-level caching.
- If AVIF or WebP is not supported by server GD, those options
  are ignored gracefully.
- Cache keys include version numbers to prevent stale output.

----------------------------------------------------------------------
CREDIT
----------------------------------------------------------------------

© Init HTML  
https://inithtml.com
