### Install Image Optimizers on Ubuntu/Debian Source: https://github.com/spatie/image-optimizer/blob/main/README.md Provides commands to install various image optimization tools on Ubuntu/Debian systems, including JpegOptim, Optipng, Pngquant, SVGO, Gifsicle, WebP, and AVIF. ```bash sudo apt-get install jpegoptim sudo apt-get install optipng sudo apt-get install pngquant sudo npm install -g svgo sudo apt-get install gifsicle sudo apt-get install webp sudo apt-get install libavif-bin # minimum 0.9.3 ``` -------------------------------- ### Install Image Optimizers on Fedora/RHEL/CentOS Source: https://github.com/spatie/image-optimizer/blob/main/README.md Provides commands to install various image optimization tools on Fedora, RHEL, and CentOS systems, including JpegOptim, Optipng, Pngquant, SVGO, Gifsicle, WebP, and AVIF. ```shell sudo dnf install epel-release sudo dnf install jpegoptim sudo dnf install optipng sudo dnf install pngquant sudo npm install -g svgo sudo dnf install gifsicle sudo dnf install libwebp-tools sudo dnf install libavif-tools ``` -------------------------------- ### Install Image Optimizers on MacOS Source: https://github.com/spatie/image-optimizer/blob/main/README.md Provides commands to install various image optimization tools on MacOS using Homebrew, including JpegOptim, Optipng, Pngquant, SVGO, Gifsicle, WebP, and AVIF. ```shell brew install jpegoptim brew install optipng brew install pngquant npm install -g svgo brew install gifsicle brew install webp brew install libavif ``` -------------------------------- ### Basic Image Optimization in PHP Source: https://github.com/spatie/image-optimizer/blob/main/README.md Demonstrates how to create an optimizer chain and optimize an image using the Spatie Image Optimizer package. It automatically detects and uses installed optimization binaries. ```php use Spatie\ImageOptimizer\OptimizerChainFactory; $optimizerChain = OptimizerChainFactory::create(); $optimizerChain->optimize($pathToImage); ``` -------------------------------- ### Image Resizing and Saving Source: https://github.com/spatie/image-optimizer/blob/main/README.md Example of using the spatie/image package to load an image, resize it, and save it in different formats (JPG, PNG, WEBP, AVIF). This demonstrates a common image manipulation workflow. ```php Spatie\Image\Image::load('original.jpg') ->width(2048) ->save('image.jpg'); // image.png, image.webp, image.avif ``` -------------------------------- ### Custom Optimization Chain Source: https://github.com/spatie/image-optimizer/blob/main/README.md This example demonstrates how to create a custom optimization chain by manually adding specific optimizers and configuring them with custom options. Here, only optipng and jpegoptim are used. ```php use Spatie\ImageOptimizer\OptimizerChain; use Spatie\ImageOptimizer\Optimizers\Jpegoptim; use Spatie\ImageOptimizer\Optimizers\Pngquant; $optimizerChain = (new OptimizerChain) ->addOptimizer(new Jpegoptim([ '--strip-all', '--all-progressive', ])) ->addOptimizer(new Pngquant([ '--force', ])); ``` -------------------------------- ### Optimizing Image to a Different Path Source: https://github.com/spatie/image-optimizer/blob/main/README.md This example shows how to optimize an image and save the optimized version to a different output path, leaving the original image untouched. ```php use Spatie\ImageOptimizer\OptimizerChainFactory; $optimizerChain = OptimizerChainFactory::create(); $optimizerChain->optimize($pathToImage, $pathToOutput); ``` -------------------------------- ### Running Tests Source: https://github.com/spatie/image-optimizer/blob/main/README.md Command to execute the test suite for the spatie/image-optimizer package using Composer. ```bash composer test ``` -------------------------------- ### Cwebp Configuration Details Source: https://github.com/spatie/image-optimizer/blob/main/README.md Details the configuration for Cwebp, used for optimizing WebP images. It lists the specific command-line options used for compression and quality. ```APIDOC Cwebp Configuration: - Compression method: -m 6 (slowest compression for best results) - Analysis passes: -pass 10 (maximizes analysis) - Multithreading: -mt (for speed improvements) - Quality factor: -q 90 (least noticeable changes) ``` -------------------------------- ### Logging Optimization Process Source: https://github.com/spatie/image-optimizer/blob/main/README.md Shows how to enable logging for the image optimization process by setting a logger that implements the Psr\Log\LoggerInterface. This allows you to track which optimizers are used and which commands are executed. ```php use Spatie\ImageOptimizer\OptimizerChainFactory; $optimizerChain = OptimizerChainFactory::create(); $optimizerChain ->useLogger(new MyLogger()) ->optimize($pathToImage); ``` -------------------------------- ### avifenc Configuration Details Source: https://github.com/spatie/image-optimizer/blob/main/README.md Details the configuration for avifenc, used for optimizing AVIF images. It outlines the constant quality level, multithreading, quantizer ranges, and tuning parameters. ```APIDOC avifenc Configuration: - Constant Quality level: -a cq-level=23 (lower values = better quality, greater file size; range 0-63) - Number of jobs (threads): -j all (uses all available cores) - Min color quantizer: --min 0 (range 0-63) - Max color quantizer: --max 63 (range 0-63) - Min alpha quantizer: --minalpha 0 (range 0-63) - Max alpha quantizer: --maxalpha 63 (range 0-63) - Rate control mode: -a end-usage=q (Constant Quality mode) - Encoder tuning: -a tune=ssim (SSIM for distortion metric) ``` -------------------------------- ### Default Image Optimization Source: https://github.com/spatie/image-optimizer/blob/main/README.md This snippet demonstrates the default way to use the image optimizer package. It creates an optimizer chain and optimizes a given image, overwriting the original file with the optimized version. ```php use Spatie\ImageOptimizer\OptimizerChainFactory; $optimizerChain = OptimizerChainFactory::create(); $optimizerChain->optimize($pathToImage); ``` -------------------------------- ### Adding a Custom Optimizer Source: https://github.com/spatie/image-optimizer/blob/main/README.md Demonstrates how to add a custom optimizer to the OptimizerChain. This involves creating an instance of your custom optimizer and adding it to the chain before optimizing an image. ```php use Spatie\ImageOptimizer\OptimizerChainFactory; $optimizerChain = OptimizerChainFactory::create(); $optimizerChain ->addOptimizer(new YourCustomOptimizer()) ->optimize($pathToImage); ``` -------------------------------- ### Custom Optimizer Interface Source: https://github.com/spatie/image-optimizer/blob/main/README.md Defines the interface for creating custom image optimizers. Implementations must provide methods for binary name, handling specific images, setting image paths and options, and generating the optimization command. ```php namespace Spatie\ImageOptimizer\Optimizers; use Spatie\ImageOptimizer\Image; interface Optimizer { /** * Returns the name of the binary to be executed. * * @return string */ public function binaryName(): string; /** * Determines if the given image can be handled by the optimizer. * * @param \Spatie\ImageOptimizer\Image $image * * @return bool */ public function canHandle(Image $image): bool; /** * Set the path to the image that should be optimized. * * @param string $imagePath * * @return $this */ public function setImagePath(string $imagePath); /** * Set the options the optimizer should use. * * @param array $options * * @return $this */ public function setOptions(array $options = []); /** * Get the command that should be executed. * * @return string */ public function getCommand(): string; } ``` -------------------------------- ### SVGO Configuration Details Source: https://github.com/spatie/image-optimizer/blob/main/README.md Details the configuration for SVGO, used for optimizing SVGs. It mentions that default configurations are used, excluding 'cleanupIDs' and 'removeViewBox' plugins due to potential display issues with multiple SVGs. ```APIDOC SVGO Configuration: - Uses default SVGO configuration. - Excludes 'cleanupIDs' plugin. - Excludes 'removeViewBox' plugin. - Reason for exclusion: Known to cause issues when displaying multiple optimized SVGs on one page. - Warning: SVGO can potentially break SVGs. Refer to external blog posts for more information. ``` -------------------------------- ### Gifsicle Configuration Details Source: https://github.com/spatie/image-optimizer/blob/main/README.md Details the configuration for Gifsicle, used for optimizing GIFs. It specifies the optimization level used. ```APIDOC Gifsicle Configuration: - Optimization flag: -O3 - Description: Sets the optimization level to Gifsicle's maximum, resulting in the slowest but best optimization results. ``` -------------------------------- ### PNG Optimization Options Source: https://github.com/spatie/image-optimizer/blob/main/README.md Details the two-step optimization process for PNG images using Pngquant 2 (lossy compression) followed by Optipng. It specifies Optipng's optimization level and interlacing settings. ```APIDOC PNG Optimization: Tools: Pngquant 2 (lossy), Optipng Pngquant 2 Options: Defaults are used. Optipng Options: -i0: Results in a non-interlaced, progressive scanned image. -o2: Sets the optimization level to two, involving multiple IDAT compression trials. ``` -------------------------------- ### JPG Optimization Options Source: https://github.com/spatie/image-optimizer/blob/main/README.md Details the options used by Spatie Image Optimizer for JPG compression via JpegOptim. It focuses on quality, stripping metadata, and progressive rendering. ```APIDOC JPG Optimization: Tool: JpegOptim Options: -m85: Stores the image with 85% quality, satisfying Google's Pagespeed compression rules. --strip-all: Strips out all text information such as comments and EXIF data. --all-progressive: Ensures the resulting image is progressive, allowing for multi-pass download with progressively higher details. ``` -------------------------------- ### Setting Optimizer Timeout Source: https://github.com/spatie/image-optimizer/blob/main/README.md This snippet illustrates how to set a maximum timeout in seconds for each individual optimizer within the optimization chain. This prevents a single optimizer from running indefinitely. ```php $optimizerChain ->setTimeout(10) ->optimize($pathToImage); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.