HomeImagickImagickDrawImagickPixelImagick Pixel IteratorImagick KernelDevelopmentTutorial Source codeReport an issue
Category
Example

Set sampling factor

← setProgressMonitor   setSeed →
Sets the image sampling factors.
Theoretically, this function allows you to set the sampling factors to be used by the JPEG compressor. In practice though, it does not seem to function particuarly well. I recommend using `Imagick::setImageProperty()` to set 'jpeg:sampling-factor' to one of the standard down-sampling types. e.g. 4:2:0
  • 4:4:4
  • 4:4:1
  • 4:4:0
  • 4:2:2
  • 4:2:0
  • 4:2:1
  • 4:2:0
  • 4:1:1
  • 4:1:0
e.g. Imagick::setImageProperty('jpeg:sampling-factor', '4:2:0');
Original size = 28582
Option 1,1,1 new size 28582
Option 1,1,2 new size 28582
Option 1,2,1 new size 0
Option 1,2,2 new size 0
Option 2,1,1 new size 23152
Option 2,1,2 new size 23152
Option 2,2,1 new size 28582

Example

function setSamplingFactors()
{
    $image_path = "../public/images/FineDetail.png";
    $imagick = new \Imagick(realpath($image_path));
    $imagick->setImageFormat('jpg');
    $imagick->setSamplingFactors(array('2x2', '1x1', '1x1'));

    $compressed = $imagick->getImageBlob();

    $reopen = new \Imagick();
    $reopen->readImageBlob($compressed);

    $reopen->resizeImage(
        $reopen->getImageWidth() * 4,
        $reopen->getImageHeight() * 4,
        \Imagick::FILTER_POINT,
        1
    );
    
    header("Content-Type: image/jpeg");
    echo $reopen->getImageBlob();
}