HomeImagickImagickDrawImagickPixelImagick Pixel IteratorImagick KernelDevelopmentTutorial Source codeReport an issue
Category
Example

Imagick::setOption

← setIteratorIndex   setProgressMonitor →
Associates one or more options with the wand.

Setting options on an image allows for control of settings used to for encoders and decoders, and image processing operations.

If you are writing PNG images with transparency and color palettes, and want to strip all non-essential chunks, it is recommended to use:

$image->setOption('png:include-chunk', 'none,tRNS');
rather than excluding all chunks, as without the transparent chunk, the image would be saved as 32bit PNG.

Many of the options available can be found on the ImageMagick site


Example 1

public function renderJPG($extent)
{
    $image_path = $this->setOptionControl->getImagePath();
    $imagick = new \Imagick(realpath($image_path));
    $imagick->setImageFormat('jpg');
    $imagick->setOption('jpeg:extent', $extent);
    header("Content-Type: image/jpeg");
    echo $imagick->getImageBlob();
}

Example 2

public function renderPNG($format)
{
    $image_path = $this->setOptionControl->getImagePath();
    $imagick = new \Imagick(realpath($image_path));
    $imagick->setImageFormat('png');
    $imagick->setOption('png:format', $format);
    header("Content-Type: image/png");
    echo $imagick->getImageBlob();
}

Example 3

public function renderCustomBitDepthPNG()
{
    $image_path = $this->setOptionControl->getImagePath();
    $imagick = new \Imagick(realpath($image_path));
    $imagick->setImageFormat('png');
    $imagick->setOption('png:bit-depth', '16');
    $imagick->setOption('png:color-type', 6);
    header("Content-Type: image/png");
    $crash = true;
    if ($crash) {
        echo $imagick->getImageBlob();
    } else {
        $tempFilename = tempnam('./', 'imagick');
        $imagick->writeimage(realpath($tempFilename));
        echo file_get_contents($tempFilename);
    }
}

Example 4

public function renderBlackPoint()
{
    // This
    $image_path = $this->setOptionControl->getImagePath();
    $imagick = new \Imagick();
    $imagick->setOption('black-point-compensation', 0.25 * \Imagick::getQuantum());
    $imagick->readImage(realpath($image_path));
    $imagick->setImageFormat('png');
    header("Content-Type: image/png");
    echo $imagick->getImageBlob();
}