Thursday, 14 December 2023

Laravel Class Imagick not found

If you see the error "Class 'Imagick' not found" in Laravel, it usually means that your server does not have the Imagick PHP extension installed or turned on. ImageMagick is a library for manipulating images that gives you advanced tools for working with images. This is how you can fix this problem:

Why Does Laravel Give the Error "Class 'Imagick' Not Found"?

This error happens when the Imagick extension is either

  • Not installed on the server yet.
  • Installed but not turned on in php.ini.
  • Installed for a version of PHP that is not the one that Laravel is using.
  • Installed wrong on Windows (wrong DLL file).
  • Not loaded because of a misconfiguration after upgrading PHP or the server.
Add the Imagick PHP Extension.

You need to install the Imagick PHP extension on your server first. Depending on your operating system, the steps for installation may be different.

Install ImageMagick on Ubuntu or Debian

If your server is based on Ubuntu or Debian, use these commands:

sudo apt-get refresh
sudo apt-get install php-imagick

After you install PHP, it will automatically find the extension, but you may still need to turn it on or restart the server.
Put ImageMagick on CentOS or RHEL.

For Red Hat or CentOS servers:

sudo yum install php-imagick

If the yum repository does not include Imagick, make sure the EPEL or REMI repositories are enabled.

Install ImageMagick on Windows

People who use Windows need to do a few things by hand:

  1. Get the right DLL file for your version of PHP from either PECL or windows.php.net.

  2. Unzip the DLL file and put it in the PHP directory.

  3. Add the following line to your file:
    extension=php_imagick.dll


    Ensure you download the DLL version matching:

    • Your PHP version (7.x, 8.x)

    • Thread Safety (NTS / TS)

    • System architecture (x64 or x86).

Enable the Imagick Extension

You need to turn on the Imagick extension in your PHP settings after you install it. Open the php.ini file. Add this line:

extension=imagick

usually arises if the tokens do not match in both sessions and are transmitted and received in requests. 

Restart Your Web Server

After installing and enabling Imagick, restart your web server to apply the changes.


For Apache:

sudo service apache2 restart

Verify Installation

You can check if Imagick is installed and turned on by making a PHP file with the following code and opening it in your web browser:

<?php phpinfo(); ?>

Look for the Imagick section in the phpinfo() output. If it's listed, then ImageMagick is successfully installed and enabled.

5. Restarting your web server

After installing and enabling Imagick, restart your server to load the extension.

Restart Apache
sudo service apache2 restart

Restart Nginx with PHP-FPM

sudo service php8.x-fpm restart
sudo service nginx restart

Restart Windows Services

If using XAMPP or WAMP:

Stop Apache.
Start Apache again.

You need to restart your computer so that PHP can find the new extension.

Conclusion:

The "Class 'Imagick' not found" error in Laravel happens when the Imagick PHP extension is not installed or is turned off. You can easily fix this problem by installing Imagick, turning it on in your php.ini file, restarting your server, and checking that it is working. Imagick is a powerful tool that is necessary for advanced image processing tasks. If you set it up correctly, your Laravel app will be able to handle image manipulation quickly and easily.

If you do everything in this guide, your Laravel project will work perfectly with Imagick, which will speed things up and cut down on mistakes when working with images.

No comments:

Post a Comment

.htaccess not working even though allowoverride is enabled

You're not the only one who has had the annoying problem with Apache where your file doesn't work even after you enable it. You'...