Search code examples
phpsymfonyimagick

Attempted to load class "Imagick" from the global namespace in symfony3


So I'm currently using "Imagick" extension for a symfony project, but I get this error:

ClassNotFoundException: "Attempted to load class "Imagick" from the global namespace. Did you forget a "use" statement?"

Strangely, it's working fine in all native php script, but not working in symfony project!

I did some research and I found that I need to enable imagick for CLI also .. but I didn't find any method explain how to make it.

So, I verified Imagick installation by this code snippet:

<?php

header('Content-type: image/jpeg');

$image = new imagick("C:/wamp64/www/test/image.jpg");
$image->thumbnailImage(100,0);

echo $image;

and I can see the image loaded correctly.

Also, when I run this script from the browser:

<?php

if (extension_loaded('imagick')){
    echo 'imagick is installed';
} else {
    echo 'imagick not installed';
}

?>

I get :

imagick is installed

But when I execute this file from command-line interface, I get :

imagick not installed

In the Symfony project I get this error log:

Uncaught PHP Exception Symfony\Component\Debug\Exception\ClassNotFoundException: "Attempted to load class "Imagick" from the global namespace. Did you forget a "use" statement?" 

Symfony function

private function createThumbnail($path, $dest, $width, $height)
    {
        $im = new \Imagick();
        $im->pingImage($path);
        $im->readImage($path);
        $im->thumbnailImage($width, $height);
        $white=new \Imagick();
        $white->newImage($width, $height, "white");
        $white->compositeImage($im, \Imagick::COMPOSITE_OVER, 0, 0);
        $white->setImageFormat('jpg');
        $white->writeImage($dest);
        $im->destroy();
        $white->destroy();
    }

Environment

PHP Version => 7.1.9

System => Windows 10

Server => Wamp64

Symfony version => 3.1

Compiler => MSVC14 (Visual C++ 2015)

Architecture => x64

imagick module version => 3.4.3

ImageMagick version => ImageMagick 6.9.3-7 Q16 x64 2016-03-27


Solution

  • I found the solution, I put the answer for someone who have the same issue:

    you should add the following lines

    ;on Windows:
    extension=php_imagick.dll
    
    ;on UNIX:
    extension = imagick.so
    

    to C:\wamp64\bin\php\phpx.x.x\php.ini

    not to C:\wamp64\bin\apache\apachex.x.x\bin\php.ini

    because the first php.ini is the file used by PHP CLI, so by the Symfony Local Web Server.

    Happy coding!