We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to use function Imagick?

Hi pro, I want to use Image Optimize. I read tutorial at https://cloudinary.com/blog/image_optimization_in_php I using Imagick but error with function: optimizeImageLayers(), setImageCompression(), setImageCompressionQuality() in Phalcon imagick error: Fatal error: Call to undefined method Phalcon\Image\Adapter\Imagick::optimizeImageLayers() in

$im = new \Phalcon\Image\Adapter\Imagick(APP_PATH . '/public/userfiles/goc.png');
        // Optimize the image layers
        $im->optimizeImageLayers(); 

        // Compression and quality
        $im->setImageCompression(Imagick::COMPRESSION_JPEG);
        $im->setImageCompressionQuality(25);

        // Write the image back
        $im->writeImages(APP_PATH . '/public/userfiles/new.png', true);

        $this->view->disable();

Hi @thanhansoft you have to use $im->getInternalImInstance() to get Imagick instance and make what you want

Good luck

Thank for reply. still bug Error: Fatal error: Call to undefined method Phalcon\Image\Adapter\Imagick::optimizeImageLayers() Please help me

$im = new \Phalcon\Image\Adapter\Imagick(APP_PATH . '/public/userfiles/goc.png');
        $im->getInternalImInstance(); 
        // Optimize the image layers
        $im->optimizeImageLayers(); 
        $this->view->disable();


32.2k
Accepted
answer
$im = new \Phalcon\Image\Adapter\Imagick(APP_PATH . '/public/userfiles/goc.png');
$im->getInternalImInstance()->optimizeImageLayers();    

Thank pro, I'm work success. This is full code

public function indexAction() {
        $im = new \Phalcon\Image\Adapter\Imagick(APP_PATH . '/public/userfiles/1.jpg');
        // Optimize the image layers
        $im->getInternalImInstance()->optimizeImageLayers();

        // Compression and quality
        $im->getInternalImInstance()->setImageCompression(\Imagick::COMPRESSION_JPEG);
        $im->getInternalImInstance()->setImageCompressionQuality(80);

        // Write the image back
        $im->getInternalImInstance()->writeImages(APP_PATH . '/public/userfiles/new.jpg', true);

        $this->view->disable();
    }