Pdf convert to image . Imagick not working when pdf convert to image giving error exceptions.
$imagick = new \Imagick($pdfPath);
$imagick->setResolution(300,300);
$imagick->setImageFormat('jpg'); //Set format
file_put_contents($newImagePath, $imagick);
ImagickException (415)
FailedToExecuteCommand `"gs" -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72" -dPrinted=false "-sOutputFile=magick-oFQPj3LUdB8KYtAfJLlOVAUlj6bYnxvF%d" "-fmagick-WYK7RdvbmnaZd85vXgzVGG07o6GdgWlD" "-fmagick-feVTdEJzQaO75F1cGnCofbyC3lurnCq1"' (The system cannot find the file specified. ) @ error/delegate.c/ExternalDelegateCommand/516
What does this give you?
dd(file_exists($pdfPath));
// This is image upload dir path after convert to image
$newImagePath = 'C:\xampp\htdocs\app/images/floorplan_images/2022/10/4573980-1664914949.jpg';
// This is pdf file dir path
$pdfPath = 'C:\xampp\htdocs\app/upload/floor_plan/3290-683626239.pdf';
You shouldn't be using file_put_contents here but instead `$imagick->writeImage('thumb.jpg');
// instantiate Imagick
$imagick = new Imagick($pdfPath);
$imagick->setResolution(300, 300);
$imagick->setImageFormat('jpg');
$imagick->writeImage('image.jpg');
$imagick->clear();
It's giving to false @sinnbeck
dd(file_exists($pdfPath));
@Mahaveer so the file does not exist and the error says that the file does not exist :) make sure that the file actually e exists before proceeding
@Sinnbeck File exits issue fixed. but still giving same error.
@Mahaveer so the dd gives true but exact same error with
The system cannot find the file specified
@Sinnbeck But, When i upload image instead pdf. so it worked fine.
How is possible?
Also, note that if your PDF contains multiple pages you need to select which page you want to covert to an image
$imagick = new Imagick($pdfPath);
$pages = $im->getNumberImages();
for ($i = 0; $i < $pages; $i++) {
// Create new instance for each page
$imagick = new Imagick($pdfPath . '[' . $i . ']');
$imagick->setResolution(300, 300);
$imagick->setImageFormat('jpg');
$imagick->writeImage('image' . $i . '.jpg');
}
@bobbybouwmann , I have already check with this example. But giving same error.
@Mahaveer Please share your code, we can't help you without context
Hey there! I have the same problem... somehow imagick is not able to execute gs from within laravel "runtime" if I use php artisan tinker, everything works fine. The error is this:
[2024-04-26 08:20:24] local.ERROR: ImagickException: FailedToExecuteCommand `'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r72x72' -dPrinted=false '-sOutputFile=/var/tmp/magick-wylNd26c0oMLm8Z7eqDBMi3yqZNgAki0%d' '-f/var/tmp/magick-e5JddLznjSeKeu-nn-uJXQUOmqJJem7V' '-f/var/tmp/magick-G7lnpXKs6YNqwtn1f7zsFZjbQPkcB7rU'' (32512) @ error/ghostscript-private.h/ExecuteGhostscriptCommand/74 in /Users/.......ommitted....../vendor/spatie/pdf-to-image/src/Pdf.php:44
It happens when:
$pdf = new Pdf($pdfPath);
Where $pdfPath is a valid pdf path.
Any ideas?
Please sign in or create an account to participate in this conversation.