PHP Logointervention/image

Intervention Image is an open-source PHP image manipulation library. It provides an expressive and fluent API to create, edit, and compose images, simplifying common image processing tasks in PHP applications. The library is driver-agnostic, meaning it can use either the GD library (built into PHP) or the Imagick PHP extension (a wrapper for ImageMagick) to perform its operations, allowing developers to choose the best backend for their server environment or specific needs. It automatically detects and uses the best available driver but also allows explicit configuration.

Key features and capabilities include:

1. Image Creation: Creating new images from scratch, from file paths, URLs, or even binary image data.
2. Resizing and Cropping: Efficiently resizing images to specific dimensions, fitting within bounds, or cropping a region.
3. Watermarking: Adding text or image watermarks to photos.
4. Text Manipulation: Drawing text on images with custom fonts, colors, and positioning.
5. Applying Effects: Numerous image filters and effects like grayscale, blur, sepia, brightness, contrast, colorizing, and more.
6. Format Conversion: Saving images in various formats (JPG, PNG, GIF, WEBP, etc.) and controlling quality.
7. Rotation and Flipping: Rotating images by degrees or flipping them horizontally/vertically.
8. Canvas Manipulation: Adding borders, backgrounds, and merging multiple images.
9. Laravel Integration: Widely popular in the Laravel ecosystem, though it can be used with any PHP project.

The library is installed via Composer and makes image handling much more intuitive and less verbose compared to directly using GD or Imagick functions. Its method chaining allows for complex image manipulations in a single, readable statement.

Example Code

<?php

require 'vendor/autoload.php';

use Intervention\Image\ImageManagerStatic as Image;

// Configure Intervention Image to use the GD driver (or Imagick if preferred)
// If not configured, it will try to auto-detect.
// Image::configure(['driver' => 'gd']); 

// --- Example 1: Basic Resizing and Saving ---

$sourceImagePath = 'path/to/your/image.jpg'; // Make sure this path exists and points to an image
$destinationPath = 'path/to/save/resized_image.jpg';

try {
    // Check if the source image exists
    if (!file_exists($sourceImagePath)) {
        throw new Exception("Source image not found: " . $sourceImagePath);
    }

    // Open an image file
    $img = Image::make($sourceImagePath);

    // Resize the image to a width of 300 pixels, maintaining aspect ratio
    $img->resize(300, null, function ($constraint) {
        $constraint->aspectRatio();
        $constraint->upsize(); // Prevent upsizing small images
    });

    // Save the resized image
    $img->save($destinationPath);

    echo "Image successfully resized and saved to: " . $destinationPath . "\n";

} catch (Exception $e) {
    echo "Error processing image: " . $e->getMessage() . "\n";
}

// --- Example 2: Adding a Text Watermark and Converting Format ---

$sourceImagePath2 = 'path/to/your/another_image.png'; // Another example image
$watermarkedPath = 'path/to/save/watermarked_image.webp';

try {
    if (!file_exists($sourceImagePath2)) {
        throw new Exception("Source image for watermark not found: " . $sourceImagePath2);
    }

    $img2 = Image::make($sourceImagePath2);

    // Resize to max 800px width, height auto
    $img2->resize(800, null, function ($constraint) {
        $constraint->aspectRatio();
        $constraint->upsize();
    });

    // Add a text watermark
    $img2->text('© My Website', 10, 10, function($font) {
        $font->file(dirname(__FILE__) . '/fonts/arial.ttf'); // Ensure you have a font file, e.g., 'arial.ttf'
        $font->size(24);
        $font->color('#fdf6e3');
        $font->align('left');
        $font->valign('top');
        $font->angle(0);
    });

    // Save the watermarked image in WEBP format with 80% quality
    $img2->save($watermarkedPath, 80, 'webp');

    echo "Watermarked image saved to: " . $watermarkedPath . "\n";

} catch (Exception $e) {
    echo "Error processing watermarked image: " . $e->getMessage() . "\n";
}

/*
To run this code:
1. Make sure you have Composer installed.
2. In your project directory, run: `composer require intervention/image`
3. Create dummy image files (e.g., 'path/to/your/image.jpg', 'path/to/your/another_image.png')
4. Create the 'path/to/save' directory if it doesn't exist.
5. For the watermark example, ensure you have a font file (e.g., 'arial.ttf') in a 'fonts' subdirectory or provide a full path to a system font.
6. Run the PHP script from your terminal: `php your_script_name.php`
*/
?>