### Install spatie/image package via Composer (Bash) Source: https://github.com/spatie/image/blob/main/docs/installation-and-setup.md This command installs the spatie/image package into your project using Composer, the dependency manager for PHP. It adds the package as a requirement in your composer.json file and downloads its dependencies. ```bash composer require spatie/image ``` -------------------------------- ### Manually Cropping an Image with spatie/image (PHP) Source: https://github.com/spatie/image/blob/main/docs/introduction.md This example shows how to use the `manualCrop` method to crop a specific region of an image. It loads 'example.jpg' and crops a 600x400 area starting at coordinates (20, 620), then saves the result. ```php Image::load('example.jpg') ->manualCrop(600, 400, 20, 620) ->save(); ``` -------------------------------- ### Running Spatie\Image Tests Source: https://github.com/spatie/image/blob/main/README.md Outlines the steps to run the tests for the Spatie\Image package. It involves installing a Node.js dependency (`pixelmatch`) using npm and then executing the Composer test script. ```bash npm i pixelmatch composer test ``` -------------------------------- ### Installing Spatie\Image PHP Package Source: https://github.com/spatie/image/blob/main/README.md Installs the Spatie\Image package using Composer, the PHP dependency manager. This command adds the package to your project's dependencies. ```bash composer require spatie/image ``` -------------------------------- ### Setting Watermark Size by Percentage - PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/adding-a-watermark.md This example shows how to set the watermark's width and height using percentage units. It positions the watermark at the top and sets its width to 100% and height to 50% of the main image dimensions, demonstrating resizing while maintaining aspect ratio. ```php $image->watermark('watermark.png', AlignPosition::Top, width: 100, widthUnit: Unit::Percent, height: 50, heightUnit: Unit::Percent ); ``` -------------------------------- ### Setting Watermark Opacity - PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/adding-a-watermark.md This example shows how to adjust the watermark's opacity using the `alpha` parameter. The value is a percentage between 0 (fully transparent) and 100 (fully opaque), allowing control over the watermark's visibility. ```php $image->watermark('watermark.png',alpha: 50); ``` -------------------------------- ### Performing Manual Crop with Spatie Image (PHP) Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/resizing-images.md Crop a specific rectangular area of the image by providing the desired width and height of the cropped area, along with the starting X and Y coordinates (in pixels) from the top-left corner of the original image. ```php $image->manualCrop(int $width, int $height, int $x, int $y); ``` ```php Image::load('example.jpg') ->manualCrop(600, 400, 20, 620) ->save(); ``` -------------------------------- ### Setting Watermark Position - PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/adding-a-watermark.md This example shows how to change the position of the watermark using the `AlignPosition` enum. The second argument specifies where the watermark should be placed on the image, overriding the default bottom-right position. ```php $image->watermark('watermark.png', AlignPosition::center); ``` -------------------------------- ### Creating Image Instances in Spatie Image v3 (PHP) Source: https://github.com/spatie/image/blob/main/docs/upgrading.md Demonstrates the three primary methods for initializing an Image object in Spatie Image version 3: using the constructor with a path, using the static load method, and selecting a specific driver before loading. ```php // 1. By passing the path to the image in the constructor. $image = new Image('path/to/image.jpg'); // 2. Using the static load method. $image = Image::load('path/to/image.jpg'); // 3. Selecting a driver and then loading the image. $image = Image::useImageDriver(ImageDriver::Imagick)->load('path/to/image.jpg'); ``` -------------------------------- ### Converting PNG to JPG with Fit, Background, and Border (PHP) Source: https://github.com/spatie/image/blob/main/docs/introduction.md This snippet illustrates converting a transparent PNG image to JPG format. It loads 'github-logo.png', fits it to 500x300 using the 'Fill' strategy, adds a light blue background, applies a 15px expanding border with color '007698', and saves the output as 'example.jpg'. ```php Image::load('github-logo.png') ->fit(Fit::Fill, 500, 300) ->background('lightblue') ->border(15, BorderType::Expand, '007698') ->save('example.jpg'); ``` -------------------------------- ### Optimize Image with Default Settings - PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/optimizing-images.md Demonstrates how to optimize an image using the default settings provided by the spatie/image package. It loads an image, applies the default optimization chain, and saves the result to a new file. ```php Image::load('example.jpg') ->optimize() ->save('example-optimized.jpg'); ``` -------------------------------- ### Applying Sepia and Blur Effects with spatie/image (PHP) Source: https://github.com/spatie/image/blob/main/docs/introduction.md This snippet demonstrates how to chain multiple image manipulation methods using the spatie/image package. It loads an image, applies a sepia filter, adds a blur effect with a radius of 50, and saves the result. ```php Image::load('example.jpg') ->sepia() ->blur(50) ->save(); ``` -------------------------------- ### Applying Chained Manipulations with Spatie Image PHP Source: https://github.com/spatie/image/blob/main/docs/usage/basic-usage.md Loads an image and applies multiple manipulations (sepia and blur with a radius of 50) in a chained manner before saving the result. Demonstrates the chaining capability of manipulation methods. ```php use Spatie\Image\Image; Image::load('example.jpg') ->sepia() ->blur(50) ->save(); ``` -------------------------------- ### Applying Multiple Brightness Manipulations with Spatie Image PHP Source: https://github.com/spatie/image/blob/main/docs/usage/basic-usage.md Illustrates applying the same manipulation (`brightness`) multiple times sequentially. The image's brightness is first reduced by 40% and then further reduced by 20% before saving. ```php use Spatie\Image\Image; // This will lower the brightness first by 40% and then by 20% Image::load('example.jpg') ->brightness(-40) ->brightness(-20) ->save(); ``` -------------------------------- ### Loading Image with Spatie Image PHP Source: https://github.com/spatie/image/blob/main/docs/usage/basic-usage.md Loads an image from a specified file path using the static `load` method of the `Spatie\Image\Image` class. This is the first step before applying any manipulations or saving the image. ```php use Spatie\Image\Image; $image = Image::load(string $pathToImage); ``` -------------------------------- ### Using Custom Driver with Spatie Image PHP Source: https://github.com/spatie/image/blob/main/docs/usage/basic-usage.md Demonstrates how to use a custom image driver implementation by passing the driver class (`MyDriver::class`) to the `useImageDriver` method before loading the image. ```php use Spatie\Image\Image; Image::useImageDriver(MyDriver::class)->loadFile(string $pathToImage); ``` -------------------------------- ### Manipulating Images with Spatie\Image PHP Source: https://github.com/spatie/image/blob/main/README.md Demonstrates various image manipulation operations using the Spatie\Image package, including resizing, applying filters (greyscale, brightness), setting quality, rotating, and sharpening. It shows how to load an image, chain methods, and save the result. ```php 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)s ->save(); // rotate the image and sharpen it Image::load($pathToImage) ->orientation(90) ->sharpen(15) ->save(); ``` -------------------------------- ### Saving Manipulated Image to File with Spatie Image PHP Source: https://github.com/spatie/image/blob/main/docs/usage/basic-usage.md Loads an image, applies a width manipulation (sets width to 50 pixels), and saves the modified image to a new file named 'modified-example.jpg' using the `save` method. ```php use Spatie\Image\Image; Image::load('example.jpg') ->width(50) ->save('modified-example.jpg'); ``` -------------------------------- ### Selecting GD Driver with Spatie Image PHP Source: https://github.com/spatie/image/blob/main/docs/usage/basic-usage.md Explicitly selects the GD image driver using `useImageDriver(ImageDriver::Gd)` before loading an image with `loadFile`. This overrides the default Imagick driver. ```php use Spatie\Image\Image; Image::useImageDriver(ImageDriver::Gd)->loadFile(string $pathToImage); ``` -------------------------------- ### Customize Image Optimization Chain - PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/optimizing-images.md Shows how to customize the image optimization process by creating an OptimizerChain instance. You can add specific optimizers with custom options and set a timeout for the optimization process before applying it to an image. ```php $optimizerChain = (new OptimizerChain) ->addOptimizer(new Jpegoptim([ '--strip-all', '--all-progressive', '-m85' ])) ->setTimeout(90); Image::load('example.jpg') ->optimize($optimizerChain) ->save(); ``` -------------------------------- ### Setting Watermark Padding - PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/adding-a-watermark.md This snippet demonstrates how to add padding around the watermark using the `paddingX`, `paddingY`, and `paddingUnit` parameters. The `paddingUnit` allows specifying padding in pixels (default) or percentages using the `Unit::Percent` enum. ```php $image->watermark('watermark.png', paddingX: 10, paddingY: 10, paddingUnit: Unit::Percent ); // 10% padding around the watermark ``` -------------------------------- ### Picking Color with Spatie Image (PHP) Source: https://github.com/spatie/image/blob/main/docs/usage/colors.md Demonstrates how to use the `pickColor` method on an `Image` instance loaded from a file. The method takes the x and y coordinates of the pixel and a `ColorFormat` enum value to specify the desired output format for the color. ```php $image = Image::load(string $pathToImage)->pickColor($x, $y, ColorFormat::Rgba); ``` -------------------------------- ### Retrieving Base64 String with Options with Spatie Image PHP Source: https://github.com/spatie/image/blob/main/docs/usage/basic-usage.md Loads an image and retrieves its content as a base64 encoded string, specifying the format ('jpeg') and opting to exclude the mime type prefix by setting the second parameter to `false`. ```php Image::load('example.jpg') ->base64('jpeg', $prefixWithFormat = false); ``` -------------------------------- ### Setting Watermark Fit Method - PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/adding-a-watermark.md This snippet demonstrates using the `fit` parameter to control how the watermark is resized within the specified dimensions. It sets the watermark to the top, 100% width, and 50% height, and uses `Fit::Stretch` to force the watermark to fill the defined area, potentially distorting its aspect ratio. ```php $image->watermark('watermark.png',AlignPosition::Top, width:100,widthUnit:Unit::Percent, height:50,heightUnit:Unit::Percent, fit: Fit::Stretch); ``` -------------------------------- ### Retrieving Image Dimensions using spatie/image (PHP) Source: https://github.com/spatie/image/blob/main/docs/usage/retrieving-properties.md This snippet shows how to load an image file and then retrieve its width and height using the getWidth() and getHeight() methods provided by the spatie/image library. ```php Image::load('example.jpg')->getWidth() // returns 1600 Image::load('example.jpg')->getHeight() // returns 1052 ``` -------------------------------- ### Resizing Image Width and Height with Spatie Image (PHP) Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/resizing-images.md Resize both the width and height of an image simultaneously using the `resize` method. This method sets the exact dimensions. ```php Image::load('example.jpg')->resize(250, 200); ``` -------------------------------- ### Retrieving Base64 String with Spatie Image PHP Source: https://github.com/spatie/image/blob/main/docs/usage/basic-usage.md Loads an image and retrieves its content as a base64 encoded string using the `base64` method. By default, it returns a JPEG formatted string including the mime type. ```php use Spatie\Image\Image; Image::load('example.jpg') ->base64(); ``` -------------------------------- ### Adding basic text - Spatie Image PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/text.md Adds text to the image using the default settings, placing it in the top-left corner. This is the simplest way to overlay text. ```php $image->text('Hello there!'); ``` -------------------------------- ### Adjusting Contrast with Spatie Image (PHP) Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/adjustments.md Adjusts the contrast of the image using the `contrast` method. It accepts an integer value between -100 (less contrast) and 100 (more contrast). ```php $image->contrast(20); ``` -------------------------------- ### Adjusting Brightness with Spatie Image (PHP) Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/adjustments.md Adjusts the brightness of the image using the `brightness` method. It takes a single integer value between -100 (darker) and 100 (brighter) as input. ```php $image->brightness(-20); ``` -------------------------------- ### Fitting Image within Dimensions with Spatie Image (PHP) Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/resizing-images.md Fit the image within specified width and height boundaries using a defined fit method from the `Fit` enum. Different fit methods control how the image is scaled and positioned within the target dimensions, optionally filling remaining space with a background color. ```php $image->fit(Fit $fit, int $width, int $height); ``` ```php # Example of how to set background colour to fill remaining pixels use Spatie\Image\Enums\Fit; $image ->fit(fit: Fit::FillMax, desiredWidth: 497, desiredHeight: 290, backgroundColor: '#ff5733'); ``` ```php use Spatie\Image\Enums\Fit; Image::load('example.jpg') ->fit(Fit:Stretch, 450, 150) ->save(); ``` -------------------------------- ### Applying Sepia Filter (PHP) Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/effects.md The `sepia` method adds a sepia filter to the `Image`, giving it an old-fashioned, brownish tone. ```php $image->sepia(); ``` -------------------------------- ### Colorizing Image with Spatie Image (PHP) Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/adjustments.md Applies a color filter to the image using the `colorize` method. It accepts three integer values between -100 and 100 for the red, green, and blue channels respectively. ```php $image->colorize(100, 0, 0); ``` -------------------------------- ### Applying Sharpen Effect (PHP) Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/effects.md The `sharpen` method sharpens the `Image`. The method accepts a value between `0` and `100` to control the intensity of the sharpening. ```php $image->sharpen(40); ``` -------------------------------- ### Wrapping text - Spatie Image PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/text.md Automatically wraps the text to fit within a specified maximum width in pixels using the `width` parameter. Useful for long text strings. ```php $image->text('Hello there! This is a long piece of text that we should wrap.', width: 1000); ``` -------------------------------- ### Adjusting Gamma with Spatie Image (PHP) Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/adjustments.md Adjusts the gamma correction of the image using the `gamma` method. It takes a float value between 0.1 and 9.99 as input. ```php $image->gamma(4.1); ``` -------------------------------- ### Positioning text - Spatie Image PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/text.md Sets the exact position of the text on the image using the `x` and `y` parameters. Coordinates are typically measured from the top-left corner. ```php $image->text('Hello there!', x: 10, y: 10); ``` -------------------------------- ### Setting text font size - Spatie Image PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/text.md Controls the size of the text in pixels using the `fontSize` parameter. A larger value results in bigger text. ```php $image->text('Hello there!', fontSize: 100); ``` -------------------------------- ### Performing Focal Crop with Spatie Image (PHP) Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/resizing-images.md Crop the image around a specific focal point defined by X and Y percentages (0-100). An optional zoom level can be applied around the focal point. ```php $image->focalCrop(int $width, int $height, int $cropCenterX = null, $cropCenterY = null); ``` -------------------------------- ### Setting text font family - Spatie Image PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/text.md Defines the font family to use for the text by providing a path to a font file (e.g., TTF). This parameter is required when using the GD driver. ```php $image->text('Hello there!', fontPath: __DIR__ . '/arial.ttf'); ``` -------------------------------- ### Setting Image Width and Height with Spatie Image (PHP) Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/resizing-images.md Modify the width or height of an image independently. The image will be resized to fit within the specified dimension while maintaining its original aspect ratio. ```php $image->width(int $width); $image->height(int $height); ``` ```php Image::load('example.jpg') ->width(250) ->height(250) ->save(); ``` -------------------------------- ### Applying Greyscale Effect (PHP) Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/effects.md The `greyscale` method converts the `Image` to greyscale, removing all color information. ```php $image->greyscale(); ``` -------------------------------- ### Cropping Image with Spatie Image (PHP) Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/resizing-images.md Crop a specific area of the image to the given width and height. The area to crop is determined by a position from the `CropPosition` enum. ```php $image->crop(int $width, int $height, CropPosition $position = CropPosition::Center); ``` ```php use Spatie\Image\Enums\CropPosition; Image::load('example.jpg') ->crop(250, 250, CropPosition::TopRight) ->save(); ``` -------------------------------- ### Setting text font color - Spatie Image PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/text.md Specifies the color of the text using the `color` parameter. The color can be a named color or a hexadecimal RGB(A) value. ```php $image->text('Hello there!', color: ''); ``` -------------------------------- ### Applying Blur Effect (PHP) Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/effects.md The `blur` method blurs the `Image`. The method accepts a value between `0` and `100` to control the intensity of the blur. ```php $image->blur(50); ``` -------------------------------- ### Adding Default Watermark - PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/adding-a-watermark.md This snippet demonstrates the basic usage of the `watermark` method to add a watermark image to an existing image. By default, the watermark is placed in the bottom right corner of the image. ```php $image->watermark('watermark.png'); ``` -------------------------------- ### Setting Image Background Color in PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/image-canvas.md The `background` method sets the background for transparent images. The color can be specified using a color name or a hexadecimal RGB(A) value. ```php $image->background('darkgray'); ``` -------------------------------- ### Auto-Detecting Image Orientation in PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/image-canvas.md Calling the `orientation` method without any parameters automatically detects and applies the correct orientation based on the image's EXIF data. ```php $image->orientation(); ``` -------------------------------- ### Applying Pixelate Effect (PHP) Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/effects.md The `pixelate` method pixelates the `Image`. The method accepts a value between `0` and `100` to control the size of the pixels. ```php $image->pixelate(50); ``` -------------------------------- ### Adding Border to Image in PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/image-canvas.md The `border` method adds a border to the image. It requires a width, a `BorderType` enum value (e.g., `Shrink`), and a color (e.g., hexadecimal). ```php $image->border(15, BorderType::Shrink, '007698'); ``` -------------------------------- ### Flipping Image Horizontally in PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/image-canvas.md The `flip` method mirrors the image. It accepts a `FlipDirection` enum value, such as `Horizontal`, `Vertical`, or `Both`. ```php $image->flip(FlipDirection::Horizontal); ``` -------------------------------- ### Rotating Image by Specific Orientation in PHP Source: https://github.com/spatie/image/blob/main/docs/image-manipulations/image-canvas.md The `orientation` method can rotate the image by passing a specific `Orientation` enum value, such as `Rotate180`. ```php $image->orientation(Orientation::Rotate180); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.