Tuesday, November 19, 2024
Google search engine
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

Recent Comments