Sunday, June 14, 2026
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
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS