Open In App

PHP | Imagick setImageCompression() Function

The Imagick::setImageCompression() function is an inbuilt function in PHP which is used to set the image compression type. 

Syntax:



bool Imagick::setImageCompression( int $compression )

Parameters: This function accepts a single parameter $compression which holds an integer matching to one of Imagick::COMPRESSION_* constants. Additionally, you can pass the constant directly like $imagick->setImageCompression(imagick::COMPRESSION_DXT1);. List of Compression constants are given below:

Return Value: This function returns TRUE on success. 



Exceptions: This function throws  ImagickException on error. 

Below programs illustrate the Imagick::setImageCompression() function in PHP: 

Program 1: 




<?php
 
// Create new Imagick Object
$imagick = new Imagick(
 
// Set the Compression to COMPRESSION_RLE
$imagick->setImageCompression(imagick::COMPRESSION_RLE);
 
// Get the Compression
$compression = $imagick->getImageCompression();
echo $compression;
?>

Output:

12

Program 2: 




<?php
 
// Create new Imagick Object
$imagick = new Imagick(
 
// Set the Compression to COMPRESSION_JPEG
$imagick->setImageCompression(imagick::COMPRESSION_JPEG);
 
// Set the Compression quality
// This is where that compression method imagick::COMPRESSION_JPEG is
// used in the program.
$imagick->setImageCompressionQuality(5);
 
// Show the output
$imagick->setformat('jpg');
header("Content-Type: image/jpg");
echo $imagick->getImageBlob();
?>

Output:

  

Reference: https://www.php.net/manual/en/imagick.setimagecompression.php


Article Tags :