// Creating an instance of Imagick $image = new Imagick('image.jpg'); // Set the compression type as JPEG and compression quality to 80 $image->setCompression(Imagick::COMPRESSION_JPEG); $image->setImageCompressionQuality(80); // Save the compressed image $image->writeImage('compressed_image.jpeg');
// Creating an instance of Imagick $image = new Imagick('image.png'); // Set the compression type as PNG and compression quality to 0 // This will make the image lossless $image->setCompression(Imagick::COMPRESSION_LZW); $image->setImageCompressionQuality(0); // Save the compressed image $image->writeImage('compressed_image.png');In the first example, we are setting the compression type as JPEG and the compression quality to 80. This will compress the image and reduce its file size. In the second example, we are setting the compression type as PNG and the compression quality to 0, which will make the image lossless. Package Library: The `setCompression` method is part of the Imagick PHP extension, which is a wrapper for the ImageMagick library. The ImageMagick library is a free and open-source image manipulation library that is used to create, edit, and convert images. The Imagick PHP extension allows developers to use ImageMagick functionality in their PHP code.