Intervention Image

repository·develop·Indexed 10 days ago

https://github.com/intervention/image

A PHP image processing library providing a fluent interface for creating, editing, and composing images. It acts as an abstraction layer over GD, Imagick, and libvips drivers. Requires PHP >= 8.3 and the mbstring extension.

Tokens
8.1K
Snippets
36
Records
47
Agent score
97%

What's inside Intervention Image

  1. Supported image drivers

    develop

    Intervention Image uses an interchangeable driver architecture. You can choose from the following base layers for image operations:

    • GD Library: Built-in PHP extension.
    • Imagick: PHP extension for ImageMagick.
    • libvips: High-performance image processing library (requires the intervention/image-driver-vips driver).
  2. How to use Intervention Image

    develop

    Intervention Image provides a fluent interface for image manipulation. The workflow typically involves:

    1. Creating an ImageManager instance with a specific driver (GD, Imagick, or libvips).
    2. Decoding an image from a path, string, or resource using the manager.
    3. Applying transformations (like scale or insert) to the image object.
    4. Encoding the image into a specific format (e.g., JPEG, PNG) with desired quality.
    5. Saving the encoded result.
    use Intervention\Image\ImageManager;
    use Intervention\Image\Drivers\Gd\Driver as GdDriver;
    use Intervention\Image\Alignment;
    use Intervention\Image\Format;
    
    // 1. Create manager with a driver
    $manager = ImageManager::usingDriver(GdDriver::class);
    
    // 2. Read image
    $image = $manager->decodePath('images/example.webp');
    
    // 3. Transform
    $image->scale(height: 300);
    $image->insert('images/watermark.png', alignment: Alignment::BOTTOM_RIGHT);
    
    // 4. Encode
    $encoded = $image->encodeUsingFormat(Format::JPEG, quality: 65);
    
    // 5. Save
    $encoded->save('images/example.jpg');
  3. System requirements for Intervention Image

    develop

    Before installing, ensure your environment meets these requirements:

    • PHP: >= 8.3
    • PHP Extensions: mbstring
    • Image Processing Extensions: One of GD, Imagick, or libvips must be installed on your server.
  4. How Polygon transformations work

    develop

    A Polygon is a DrawableInterface object. Transformations like rotate(), movePointsX(), and alignHorizontally() modify the coordinates of the internal points.

    When using the adjust() method, you can pass a callback to a DrawableFactoryInterface (provided by $polygon->factory()) to perform complex, chained adjustments on the drawable object.

    $polygon->adjust(function ($factory) {
        $factory->rotate(45);
        $factory->movePointsY(20);
    });
  5. Convert HSL color to Hex or String

    develop

    You can export the HSL color into different formats:

    • Hexadecimal: Use toHex(bool $prefix = false) to convert the HSL color to a hex string. This performs an internal conversion to the RGB colorspace.
    • HSL String: Use toString() to get a CSS-compatible HSL string representation (e.g., hsl(200 50% 50%)). If the color is transparent, it includes the alpha component (e.g., hsl(200 50% 50% / 0.5)).
    $color = Color::create(200, 50, 50);
    
    echo $color->toHex(); // Outputs something like "#3498db"
    echo $color->toString(); // Outputs "hsl(200 50% 50%)"
  6. Access HSV color channels

    develop

    The Color class provides methods to retrieve individual color channels. Each method returns a ColorChannelInterface object representing that specific component of the color.

    • hue(): Returns the Hue channel.
    • saturation(): Returns the Saturation channel.
    • value(): Returns the Value channel.
    • alpha(): Returns the Alpha channel.
  7. Get CMYK color string representation

    develop

    The toString() method returns a string representation of the CMYK color.

    • For opaque colors, it returns: cmyk(c m y k)
    • For transparent colors, it returns: cmyk(c m y k / a)
    use Intervention\\Image\\Colors\\Cmyk\\Color;
    
    $color = Color::create(0, 100, 100, 0);
    echo $color->toString(); // "cmyk(0 100 100 0)"
  8. Rotate a Polygon around a pivot

    develop

    The rotate(float $angle) method rotates all points of the polygon clockwise around its designated pivot point. The angle is provided in degrees.

    Note: The rotation is calculated relative to the current pivot() point of the polygon.

    // Rotate the polygon 90 degrees clockwise
    $polygon->rotate(90.0);
  9. Parse a CMYK color from a string

    develop

    Use Color::parse(string $input) to convert a color string into a Color instance. This method utilizes the StringColorDecoder to interpret the input format.

    Throws:

    • InvalidArgumentException: If the input cannot be parsed.
    • ColorException: If the parsed result is not a valid CMYK color instance.
    use Intervention\\Image\\Colors\\Cmyk\\Color;
    
    $color = Color::parse('cmyk(0, 100, 100, 0)');
  10. Access RGB color channels

    develop

    Once you have a Color instance, you can access individual color channels. Each method returns a ColorChannelInterface object representing that specific component:

    • red(): Returns the red channel.
    • green(): Returns the green channel.
    • blue(): Returns the blue channel.
    • alpha(): Returns the alpha (transparency) channel.
    $redValue = $color->red()->value();
    $alphaValue = $color->alpha()->value();