Fatal error: Uncaught exception 'ImagickException' with message 'NoDecodeDelegateForThisImageFormat - php

I'm unable to make imagick works with local xampp.
Error
Fatal error: Uncaught exception 'ImagickException' with message 'NoDecodeDelegateForThisImageFormat
Code
$im = new imagick();
$im->setResolution(300, 300);
$im->readimage($base_dir . 'files/PDF/test.pdf');
$im->setIteratorIndex($i);
$im->setImageFormat('jpeg');
$newimgname = time();
$im->resizeImage(500, 500, Imagick::FILTER_LANCZOS, 1);
$im->cropImage(100, 100, 0, 0);
$thumbnail = $im->getImageBlob();

Imagick calls the ImageMagick library to do all it's processing of images. The Image Magick library does not actually handle PDFs itself, it calls GhostScript to process them and generate a PNG or Jpeg which Image Magick then reads.
The NoDecodeDelegateForThisImageFormat is saying that Image Magick is unable to call the delegate program that it thinks it should be delegating the decoding to i.e. GhostScript.
The solution is to install GhostScript through yum or apt, and it 'should' work.
If it still doesn't work you should check what is in the delegates file for Image Magick (http://www.imagemagick.org/source/delegates.xml) for the PDF entry and make sure that it is callable from a command prompt - i.e. to check that Image Magick will also be able to find it.

From my Server admin:
You may want to try GraphicsMagick as an alternative to Image Magick.
Graphics Magick Homepage

Even after installing GhostScript, we could not get the imagick code to work after switching from AWS to Azure, getting the Delegate error. We ended up converting the PHP code to Image Magick command line, using the function execInBackground to run the command (PHP.net Exec() Page)
NOTE: The command line did not work using exec alone. The php script would not terminate normally.
//from PHP.net
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
//create a thumbnail from the first page of PDF
//old php code
/*
$image_magick = new imagick();
$image_magick->setbackgroundcolor('white');
$image_magick->readImage($file_path . "[0]");
$image_magick = $image_magick->flattenImages();
$image_magick->setResolution(300,300);
$image_magick->thumbnailImage(102, 102, true);
$image_magick->setImageFormat('jpg');
$image_magick->writeImage($thumbnail_path);
*/
//command line syntax
$cmd = "magick convert " . chr(34) . $file_path . "[0]" . chr(34) . " -background white -flatten -resample " . chr(34) . "300x300" . chr(34) . " -thumbnail " . chr(34) . "102x102" . chr(34) . " -format jpg -write " . chr(34) . $thumbnail_path . chr(34);
execInBackground($cmd);

Related

PHP convert EPS file to PNG/SVG

I have an eps file and I want it to convert svg or png format.
I have installed PHP Imagick extension in my localhost and also installed the GhostScript.
I have tried the following code
for eps to svg code
$file_path = realpath('./sample.eps');
$dest_path = getcwd() . '/sample.svg';
$command = "inkscape --file=$file_path --export-plain-svg=$dest_path";
$output = shell_exec($command);
var_dump($output);
var_dump(is_file('./sample.svg'));
this code return null and bool(false)
for eps to png code
$path = 'http://localhost/Sample.eps';
$save_path = 'http://localhost/imprint_option_2E_c.png';
$image = new Imagick();
$image->setResolution(300,300);
$image->readimage($path);
$image->setBackgroundColor(new ImagickPixel('transparent'));
$image->scaleImage(600, 270);
$image->setImageFormat("png");
$image->writeImage($save_path);*/
error is : Uncaught ImagickException: Failed to read the file

Create PDF thumbnail with Imagick and write to file

I'm trying to create a pdf thumbnail with Imagick and save it on the server in the same location as the pdf. The code below works fine as is. The problem is that I don't want to echo the image. But if I remove the echo statement, the resulting jpg file contains errors and is unreadable. How can I create the thumbnail and write to a file without sending it to the browser?
$pdfThumb = new \imagick();
$pdfThumb->setResolution(10, 10);
$pdfThumb->readImage($filePath . $fileName . $fileExt . '[0]');
$pdfThumb->setImageFormat('jpg');
header("Content-Type: image/jpeg");
echo $pdfThumb;
$fp = fopen($filePath . $fileName . '.jpg', "x");
$pdfThumb->writeImageFile($fp);
fclose($fp);
DaGhostman Dimitrov provided some helpful code on #16606642, but it doesn't work for me for some reason.
I would try:
$pdfThumb = new imagick();
$pdfThumb->setResolution(10, 10);
$pdfThumb->readImage($filePath . $fileName . $fileExt . '[0]');
$pdfThumb->setImageFormat('jpg');
$fp = $filePath . $fileName . '.jpg';
$pdfThumb->writeImage($fp);
Bonzo's answer requires imagick on the webserver. If imagick is not on the webserver you can try to execute imagemagick from the commandline by php command exec():
exec('convert -thumbnail "178^>" -background white -alpha remove -crop 178x178+0+0 my_pdf.pdf[0] my_pdf.png')
And if you like to convert all pdfs in one step from same folder where your script is located try this:
exec('for f in *.pdf; do convert -thumbnail "178^>" -background white -alpha remove -crop 178x178+0+0 "$f"[0] "${f%.pdf}.png"; done');
In this examples I create a png thumbnail 178x178 pixel from the first page (my_pdf.pdf[0] the 0 means first pdf page).

Imagick Failed to read the file PDF

I'm using Imagick and trying to convert a pdf to a png. It fails. My error_log says "Failed to read the file".
Example code:
$fileone = $_SERVER['DOCUMENT_ROOT'] . '/' . 'test.pdf';
$image = new Imagick($fileone);
$image->readImage($fileone);
$image->thumbnailImage(300, 0);
echo '<img src="data:image/png;base64,' . base64_encode($image->getimageblob()) . '" />';
Thoughts?
You need to install ghostscript
sudo apt-get install ghostscript
I would first use realpath() to check your file path and then see if the file is readable.
$fileone = realpath('test.pdf');
if (!is_readable($fileone)) {
echo 'file not readable';
}
Then if it is a multiple page pdf try this
$image = new Imagick($fileone.'[0]');

File upload error while editing the file using exec command

I am using exec command to create a thumbnail of pdf.
At the time of adding code works fine:
File is uploading in folder
Added in db.
But with the following error message.
Warning
Warning: Failed to move file!
Error
Error moving file
At the time of edit:
File is uploading in folder
Not updated in db.
And getting error message with the following error message:
Warning
Warning: Failed to move file!
Error
Error moving file
Code:
$uploadPath = JPATH_ADMINISTRATOR
. '/components/com_ets_fast_track/assets/buildings/'
. $filename;
$fileTemp = $file['tmp_name'];
if(!JFile::exists($uploadPath)){
if (!JFile::upload($fileTemp, $uploadPath)){
JError::raiseWarning(500, 'Error moving file');
return false;
} else {
//name the thumbnail image the same as the pdf file
$pdfWithPath = $uploadPath;
$thumbDirectory = JPATH_ADMINISTRATOR
. '/components/com_ets_fast_track/assets/buildings/';
$thumb = basename($filename, ".pdf");
//add the desired extension to the thumbnail
$thumb = $thumb.".jpg";
$cmd="convert \"{$uploadPath}[0]\" -geometry 227x295 -density 222x294 -quality 100 -channel RGBA -bordercolor white -border 1x1 -fill none -draw matte 0,0 floodfill $thumbDirectory$thumb";
exec($cmd);
}
}

Image creation from pdf

I am creating a cron job in which I copy files from one directroy to another. The cron job works fine it copies the files to the import directory. The structure of import directory is like this.
import
folder1
pdf_file1
folder2
pdf_file2
I am trying to create a thumbnail image for each pdf file that is copied to the folders, I installed ImageMagik and my php code to create a thumnail image is
if ($preview != "") {
$copy_res = ftp_get("files/upload/" . $ftp_upload["path"] . "/" . $preview, "files/import/" . $ftp_upload["preview"] . "/" . $preview);
$md5_checksum = md5("files/import/" . $ftp_upload["path"] . "/" . $preview);
} else {
//$pdf_file = 'files/import/folder1/pdf_file1';
$pdf_file = 'files/import/' . $ftp_upload["path"]."/".$filename_new;
if (file_exists($pdf_file)){
echo 'I am here';
exec("convert -density 300 " . $pdf_file . "[0]" . $filename_new . ".jpg");
}
}
When I run the code it comes to the else statement and echo the line but its not executing the exec command.
I checked the convert command from Command Prompt for that I navigated to
C:\wamp\www\project\files\import\folder1
and then I run this command
exec("convert -density 300 test.pdf[0] test.jpg");
From the command prompt it worked but in my code it s not working .
Is there any issue with the path ? because the file is alreday copied when I try to create the thumbnail for it.
The ImageMagik is installed in
C:\Program Files(x86)\ImageMagik
Any ideas what I am doing wron ? and is there any other faster way to create thumbail image for pdf ?
Thanks in advance
I now write my code seperating the code form the exec() so that you can display the information you are sending to Imagemagick.
I would also try setting the filenames outside the exec()
$input = $pdf_file . "[0]";
$output = $filename_new . ".jpg"
$cmd = " -density 300 $input -thumbnail 200x300 $output ";
// echo $cmd."<br>";
exec("convert $cmd");
Added a resize but it will keep the image proportions - if you need an exact size you could crop the image or pad it with a colour.
There seems to be a missing space that separates the path to the pdf file and the new name for the image file.
exec("convert -density 300 " . $pdf_file . "[0]" . $filename_new . ".jpg");
Should be:
exec("convert -density 300 " . $pdf_file . "[0] " . $filename_new . ".jpg");
-----------------------------------------------^

Categories