Monday, November 18, 2024
Google search engine
HomeLanguagesPHP | Imagick setImageCompression() Function

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 throws  ImagickException on error. 

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

Program 1: 

php




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




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

RELATED ARTICLES

Most Popular

Recent Comments