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:

  • imagick::COMPRESSION_UNDEFINED (0)
  • imagick::COMPRESSION_NO (1)
  • imagick::COMPRESSION_BZIP (2)
  • imagick::COMPRESSION_FAX (6)
  • imagick::COMPRESSION_GROUP4 (7)
  • imagick::COMPRESSION_JPEG (8)
  • imagick::COMPRESSION_JPEG2000 (9)
  • imagick::COMPRESSION_LOSSLESSJPEG (10)
  • imagick::COMPRESSION_LZW (11)
  • imagick::COMPRESSION_RLE (12)
  • imagick::COMPRESSION_ZIP (13)
  • imagick::COMPRESSION_DXT1 (3)
  • imagick::COMPRESSION_DXT3 (4)
  • imagick::COMPRESSION_DXT5 (5)

Return Value: This function returns TRUE on success.

Exceptions: This function thows ImagickException on error.



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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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;
?>

chevron_right


Output:

12

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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();
?>

chevron_right


Output:

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.