ottaviane
3 months ago
636
16
Laravel

Converting PDF to JPG with Imagick [RESOLVED]

Posted 3 months ago by ottaviane

thanks to wingly this was resolved!!! it was solved modifying my Imagick policy.xml infact in the file /etc/Imagick-6/policy.xml we edited this: <policy domain="coder" rights="read|write" pattern="PDF" />

and this is the working code of my controller function:

 public function get_temp_pdf(Request $req){    
        $store=Storage::disk('dispos');    
        $req->validate(['file' => 'required|mimes:pdf|max:2048']);
        $file = $req->file('file');
        $file_info['orig_path']=$file->getClientOriginalName();//->getClientOriginalName(); 
        $file_info['orig_ext']=$file->getClientOriginalExtension();   
        $file_info['real_path']=$file->getRealPath();
        $file_info['size']=$file->getSize();
        $file_info['mime']=$file->getMimeType();       

        $temp_path=$store->putFile('temp',$file);

        $im = new Imagick;
        syslog(LOG_NOTICE,"DisposizioniController: new Imagick created.");

        $im->readImageBlob(Storage::disk('dispos')->get($temp_path));
        syslog(LOG_NOTICE,"DisposizioniController: Blob loaded");

        $im->setImageFormat('jpg');
        syslog(LOG_NOTICE,"DisposizioniController: Image format setted");

        $im->setResolution(300,300);
        syslog(LOG_NOTICE,"DisposizioniController: resolution setted");

        $to=$temp_path.'.jpeg'; //destination jpeg file
        syslog(LOG_NOTICE,"DisposizioniController: writing image to $to");

        $temp_path=$store->put($to,$im->getImageBlob());
        syslog(LOG_NOTICE,"DisposizioniController: file jpg saved");

        return response()->json(['file_info' => $file_info , 'temp_path' => $temp_path],200);
    }

Thanks to wingly very very nice.

This is the begin of the problem: Hi all, I have this function in my controller:

 public function get_temp_pdf(Request $req){    
        $store=Storage::disk('dispos');    
        $req->validate(['file' => 'required|mimes:pdf|max:2048']);
        $file = $req->file('file');     
        
        $temp_path=$store->putFile('temp',$file);
        syslog(LOG_NOTICE,"DisposizioniController: file: $file  tempfile: $temp_path");
        $im=new Imagick( $temp_path);
        syslog(LOG_NOTICE,"DisposizioniController: imagick created");
        $im->setImageFormat('jpg');
        $im->setResolution(300,300);
        $temp_path=$store->putFile('temp',$im);
        return response()->json(['file_info' => $file_info , 'temp_path' => $temp_path],200);
    }

When it trying to instantiate new Imagick it crashes. Why? I already installed "sudo apt install imagemagick php-imagick php-dev ghostscript" and in controller I use

use Imagick;

Can you help me please?

Please sign in or create an account to participate in this conversation.