Spatie Image

repository·main·Indexed 23 days ago

https://github.com/spatie/image

A PHP library providing a fluent API for image manipulations including resizing, cropping, and applying filters. It supports multiple drivers such as Imagick, GD, and Vips, and offers features for watermarking, color adjustments, image optimization via spatie/image-optimizer, and various fit and crop strategies.

Tokens
9.7K
Snippets
48
Records
90
Agent score
76%

What's inside spatie/image

  1. Choose a border type

    main

    When using the border() method, you can specify how the border is applied using BorderType constants:

    • BorderType::Overlay: The border is added as an overlay on top of the existing image content.
    • BorderType::Shrink: The image content is shrunk to fit inside the border. The canvas size remains unchanged.
    • BorderType::Expand: The border is added to the outside of the image, which expands the total canvas size.
  2. Migrate from v2 to v3

    main
    Version 3 is a major rewrite that streamlines manipulations and removes several dependencies. Note that some image processing results may differ slightly from v2 due to improvements made across various methods. Key architectural changes include a slimmer Image class that focuses on driver delegation (GD or Imagick) and changes to how manipulations are applied.
  3. Configure the Vips driver

    main

    The Vips driver is a high-performance alternative to Imagick or GD. It is faster and uses less memory, which is beneficial for large images or high-volume processing.

    To use it, you must first ensure libvips is installed on your system, then install the PHP bindings via Composer.

    composer require jcupitt/vips
  4. Basic image manipulation with Spatie\Image\Image

    main

    The Spatie\Image\Image class provides an expressive API for loading, modifying, and saving images. You can chain methods to perform multiple operations such as resizing, color adjustments, and rotations.

    use Spatie\Image\Image;
    
    // modifying the image so it fits in a 100x100 rectangle without altering aspect ratio
    Image::load($pathToImage)
       ->width(100)
       ->height(100)
       ->save($pathToNewImage);
       
    // overwriting the original image with a greyscale version   
    Image::load($pathToImage)
       ->greyscale()
       ->save();
       
    // make image darker and save it in low quality
    Image::load($pathToImage)
       ->brightness(-30)
       ->quality(25)
       ->save();
       
    // rotate the image and sharpen it
    Image::load($pathToImage)
       ->orientation(90)
       ->sharpen(15)
       ->save();
  5. Requirements for image optimization

    main

    Image optimization relies on the spatie/image-optimizer package, which requires specific optimization tools to be installed on your system. Common tools include:

    • JpegOptim: For JPEG optimization.
    • Pngquant: For PNG optimization.

    Ensure these binaries are present on your system path for the optimize() method to function.

  6. Save an image to a new location

    main

    By default, calling the save() method on an Image instance applies all manipulations to the original file. To save the manipulated image as a copy in a different location, pass the desired output path as a string argument to the save() method.

    Image::load('example.jpg')
        ->sepia()
        ->save('sepia-example.jpg');
  7. Apply image manipulations

    main

    Manipulations are applied by chaining method calls on the Image instance. All manipulations are applied in the order they are called. If you call the same manipulation multiple times, each call is applied sequentially.

    use Spatie\Image\Image;
    
    // Chaining multiple manipulations
    Image::load('example.jpg')
        ->sepia()
        ->blur(50)
        ->save();
    
    // Sequential application of the same manipulation
    Image::load('example.jpg')
        ->brightness(-40)
        ->brightness(-20)
        ->save();