How To Install Imagick To Convert PDF File To Image In Laravel

Hello Artisan,

In this tutorial, I will show you how to install imagick in Laravel, and then using this library, we will see how we can convert a pdf file to an image file. So if you don't know how to install imagick in Laravel then this example is for you.

From this tutorial, you will learn laravel create thumbnail from pdf using imagick library.  To convert PDF to Image file, I will use laravel pdf to image converter. if you would like to see example of how to convert pdf to image in laravel then you are a right place.

So to convert PDF to image you can use php imagick extension to convert pdf to image file. Before using it, you must install imagick extension for pdf and need to give permission. 

 

Let's start how to convert a pdf file to an image in Laravel tutorial:

Step 1: Download Laravel

Before starting this example, I am going to start from scratch. So download a fresh Laravel project by the following command:

composer create-project --prefer-dist laravel/laravel blog

 

Step 2: Install php-imagick

Now in this, we have to install php-imagick to convert pdf to image in any php based application. So install it by the following steps:

sudo apt install php-imagick

//then

sudo apt list php-magick -a

 

Now we need to restart our apache server. So restart it like:

sudo systemctl restart apache2

 

Or if you are running Nginx then restart it like:

sudo systemctl restart nginx

 

Now we need to check whether our Imagick is successfully installed or not. We can check it like:

php -m | grep imagick

 

If you see the output of this following command like below

imagick

 

Then we can say that Imagick is installed successfully on our server. Now we need to change the file script like below of this following path:

vim /etc/ImageMagick-6/policy.xml

 

Now change the following output like below:

Before:

 

Having changed:

 

Step 3: Create Route

In this step, we need to create one route as below.

routes/web.php

use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\DemoController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('convert-pdf-to-image', [DemoController::class, 'index']);

 

Step 4: Create Controller

To implement the index method, we need a demo controller. So create it like:

app/Http/Controllers/DemoController.php

namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Imagick;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $imagick = new Imagick();
  
        $imagick->readImage(public_path('__put_your_pdf_file_path_here'));
  
        $saveImagePath = public_path('converted.jpg');
        $imagick->writeImages($saveImagePath, true);
  
        return response()->file($saveImagePath);
    }
}

 

Recommended: Best Practice to Use API Resource Collection in Laravel

 

Hope this pdf to image conversion tutorial will help you.

 

#laravel #laravel-8x #image-imagick #php-imagick