Friday, October 17, 2025
HomeLanguagesPHP | Imagick setSamplingFactors() Function

PHP | Imagick setSamplingFactors() Function

The Imagick::setSamplingFactors() function is an inbuilt function in PHP which is used to set the image sampling factors.

Syntax: 

bool Imagick::setSamplingFactors( array $factors )

Parameters: This function accepts a single parameter $factors which holds an associative array containing sampling factors.

Return Value: This function returns TRUE on success.

Exceptions: This function throws ImagickException on error.

Below given programs illustrate the Imagick::setSamplingFactors() function in PHP:

Program 1: 

PHP




<?php
 
// Create a new imagick object
$imagick = new Imagick(
 
// Set the Sampling factors
$imagick->setSamplingFactors(array('6', '7', '8'));
 
// Get the Sampling factors
$samplingFactors = $imagick->getSamplingFactors();
print("<pre>".print_r($samplingFactors, true)."</pre>");
?>


Output: 

Array
(
    [0] => 6
    [1] => 7
    [2] => 8
)

Program 2:  

PHP




<?php
 
// Create a new imagick object
$imagick = new Imagick(
 
// Set the format to jpg
$imagick->setImageFormat('jpg');
 
// Set the sampling factors
$imagick->setSamplingFactors(array('1x1', '2x2'));
 
// Save the image
$compressed = $imagick->getImageBlob();
 
// Create a new imagick object
$reopen = new Imagick();
 
$reopen->readImageBlob($compressed);
 
// Resize it to same size
$reopen->resizeImage(667, 184, 0, 1);
 
// Show the output
header("Content-Type: image/jpg");
echo $reopen->getImageBlob();
?>


Output: 

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

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS