I'm trying to convert a PDF file to JPG files.
The PDF is created using Prince, and immediately after, I call the function that calls ImageMagick. Here's the content of the said function:
if (!file_exists(Settings::getPDFFilePath())) {
Log::l(true, "ERROR: File \"" . Settings::getPDFFilePath() . "\" doesn't exist.");
throw new SeverityException(SeverityException::MISSINGPDFFILE);
}
if ((!file_exists(Settings::getPreviewJPGDirectory())) && (!mkdir(Settings::getPreviewJPGDirectory()))){
Log::l(true, "ERROR: Preview JPG directory couldn't be created.");
throw new SeverityException(SeverityException::UNWRITABLE_BOOK_DIRECTORY);
} elseif (!chmod(Settings::getPreviewJPGDirectory(), 0777)) {
/** Files might be modified by other script/user. */
Log::l(true, "WARNING: Access rights could not be modified for Preview JPG directory. Any further modification might become impossible.");
Settings::submitException(new SeverityException(SeverityException::JPG_DIR_RIGHTS_UNMODIFIABLE));
}
$convert = "/usr/local/bin/convert -quality 100 -density 100x100 /path/to/pdf/file.pdf /path/to/jpg/file.jpg 2>&1";
exec($convert, $output, $res);
Here's the thing:
When I call ImageMagick from a command-line with my user or the user _www, it works.
When I call the php script from a command-line, using my user or the user _www, it works.
But when I call the php script from a browser (ImageMagick is then called by the user _www, I've checked) I get this error:
convert: no images defined `/path/to/jpg/file.jpg\' # error/convert.c/ConvertImageCommand/3187.'
The pdf file permissions are 666 and the jpg's destination folder's permissions are 777.
I doubt that the problem comes from Prince, and it seems obvious to me that it's not access-rights related either.
Both command-line mode and Apache use the same php.ini file (/etc/php.ini).
I may have missed something, but I really don't know what...
Edit: Oh, and I'm using MacOS Maverick, but I don't think that's relevant.
Edit2: I just tried with pdftopng (look XPDF up for more info) and it works fine. So the problem definitely comes from ImageMagick.
Related
I have a web app that uses MVC(yii2) running on Ubuntu/lampp. When the script attempts to run the identify method .
It returns nothing, but the error_log is having some lines stating the fact that a module is missing. This error is only on PNG image format, on JPEG/JPG it runs perfectly.
Already tried reinstalling imagemagick and identify, but no result. Also the script is running perfectly when called from the command line.
The model function( getFullPath returns the correct value, it's not the problem here)
public function identify()
{
$cmd = "identify -verbose " . $this->getFullPath();
$cmd = str_replace(['(', ')'], ['\\(', '\\)'], $cmd);
exec($cmd, $o);
return $o;
}
The script that runs perfectly(it's just a png downloaded from google, my files are not saved there)
<?php
$cmd = "identify -verbose /home/timurmengazi/Downloads/O-Academy-rgbrev.png";
exec($cmd, $o);
print_r($o);
?>
Also, the lines from error_log
identify-im6.q16: unable to load module `/usr/lib/x86_64-linux-gnu/ImageMagick-6.9.7//modules-Q16/coders/png.la': file not found # error/module.c/OpenModule/1302.
identify-im6.q16: no decode delegate for this image format `PNG' # error/constitute.c/ReadImage/504.
identify: unable to load module `/usr/lib/x86_64-linux-gnu/ImageMagick-6.9.7//modules-Q16/coders/png.la': file not found # error/module.c/OpenModule/1302.
identify: no decode delegate for this image format `PNG' # error/constitute.c/ReadImage/504.
I am using this command to get image metadata, like compression type, quality of compression and other stuff.
I used this and this to check a PDF file is password protected or not!
i test this methods:
exec("$shFunction withPassword.pdf",$result);
OR
shell_exec('sh ' . $shFunction . ' withPassword.pdf');
i set ghostscript in $shFunction
password.pdf and php file are in same folder
but i get error:
Failed to invoke gs
Well, Ghostscript isn't a PDF password checker, so that might be a problem.
Also the Ghostscript executable is not called ghostscript. The executable name varies depending on the platform and word size. However, I see that the error is 'failed to invoke gs' which suggests that you are at least getting the name correct.
The most likely sources of problems are:
1) Ghostscript isn't installed
2) The $PATH environment variable for the user which the process is running as does not include the folder containing Ghostscript binary gs.
I have a webpage where I let users to upload files to the account folder. Exactly PDF and JPG files only. I want to count the number of pages inside each PDF uploaded to show it to the users.
To do this, I was using PDFINFO linux library, part of XPDF proyect.
This is the man page of the binary file: http://linuxcommand.org/man_pages/pdfinfo1.html
You can download the .zip with the binaries there: http://www.foolabs.com/xpdf/download.html
My code (this worked perfectly, but yesterday it failed):
function getNumPagesInPDF($document){
if(!file_exists($document))return null;
$cmd = "pdfinfo";
// Open the document
exec($cmd." '".$document."'", $output);
// Browse the data
$pagecount = 0;
foreach($output as $op){
// Extrac number of pages
if(preg_match("/Pages:\s*(\d+)/i", $op, $matches) === 1){
$pagecount = intval($matches[1]);
break;
}
}
return $pagecount;
}
I can run the command in SSH, and it works in the server. Now, this code doesn't work in PHP, but nothing changed the code.
AH! a little addition: I checked exec works in my PHP using:
function exec_enabled() {
$disabled = explode(',', ini_get('disable_functions'));
return !in_array('exec', $disabled);
}
if (exec_enabled()){
echo "exec funciona";
}
Another addition: PHP didn't shows any error related with that and I have the error logging enabled to a log file (including warnings). My host recently activated mod_security.
TASK1: Try $document variable: the path is ok, relative to the place where the php code file is placed. The path exists and the file too.
TASK2: Check if $output variable has anything: NO, $output array is empty! Why? cannot understand.
TASK3: Check the $cmd." '".$document."'" : it's ok, and copied the "result" to ssh works. I'm lost.
As per the comment discussion, we've seen that running a binary using a bare filename does not always work. This is as true on the console as it is inside a system command like exec().
When you run pdfinfo in either environment, the system will search through the environment variable PATH to discover which directories to find it in. This variable is nearly always different between your user account and the Apache environment, which is why it is important to always specify the fully-qualified filename when running a binary programmatically.
As far as I know, exec() does not regard the folder containing the current PHP script as the current working directory. Even if it did, the current directory . would need to be in the Apache user's PATH in order for this to be found. Thus, I am not sure why this used to work for you, but it emphasises the importance of the above lesson: always use the full path.
You should also read the path from a settings file, rather than hardwiring it in code. This will help you as you move from local, test, staging and live environments of your app, which may store this binary in different locations.
My environment is: MacOSX 10.8.3, PHP 5.3.17, ImageMagick 6.8.0, Apache 2.2.22
I have installed ImageMagick and PHP-Imagick module by using macports.
ImageMagick works as expected, for example I can convert a JPG to GIF by using the console command convert /my-path/file.jpg file.gif.
However when I create a new Imagick instance by using a sample code as follow:
<?php
$m = new Imagick('/my-path/file.jpg');
I get the exception:
NoDecodeDelegateForThisImageFormat `/my-path/file.jpg' #
error/constitute.c/ReadImage/550
The format of the file doesnt change the result; I tested different JPG, GIF and PNG images however same exception occurs.
So I suspect, the PHP/Apache - ImageMagick integration part is somewhat faulty.
I saw this post on SO and applied the given resolution (adding the environment variable "MAGICKCODERMODULE_PATH" with the value "/opt/local/lib/ImageMagick-6.8.0/modules-Q16/coders") however that didn't make sense for my case. (I confirmed the existence of this environment variable from my phpinfo)
Outputs of some commands on my system:
identify -list format: http://pastebin.com/tiE7Jr1m
identify -list configure: http://pastebin.com/Zt8yiRhj
I will appreciate any suggestion.
Solution
chmod 755 /opt
Insight and Thoughts
At last. I managed to solve the problem.
As there were no answers and suggestions on SO; I'm not suprised that something not expected was causing it!..
#sathia suggested to define an environment variable to call ImageMagick binaries. However in my case, I need to use phpImagick extension and access ImageMagick through its API, so this suggestion didn't make sense.
But I wanted to give it a try and call ImageMagick's convert binary by using PHP's system function. After calling it, I got a return value of "126"; and 126 means, binary is found however it's not executable!..
I checked the binary in /opt/local/bin/convert and it has appropriate execute permissions, then I checked /opt/local/bin and /opt/local directories as well and they were executable too. However the catch was /opt folder's permission was 700!
I managed to execute chmod 755 /opt and magically error has gone.
I've been there too, this is how I solved it:
<?php
define("IMAGE_MAGICK_PATH","/usr/local/bin/");
$in = '/my-path/file.jpg';
$out = '/my-path/file.gif';
$convertString = IMAGE_MAGICK_PATH." convert ".$yourfile." ".$out;
exec($convertString);
hope it helps
when I try to convert via php my svg file to ex. png file i get this error:
$filename = '507e6221c9f0f.svg';
$content = file_get_contents($filename);
$im = new Imagick();
$im->readImageBlob($content);
Fatal error: Uncaught exception 'ImagickException' with message 'no decode delegate for this image format `' # blob.c/BlobToImage/347' in..
however when I do this via commend line everything is ok. I would use php exec() command, but I'm using PHP + IIS server, so I don't wanna to grant permission to use cmd.exe by IUSR user (the IIS user)
can anyone tell me how to fix this problem?
That error means that imagemagick does not know how to handle that file. Some things you can check:
1) check the svg file manually.
2) check if you are using inkscape or rsvg.
3) try a shell command (assuming you are on linux) like 'convert 507e6221c9f0f.svg image.jpg'
May be post output here so ther someone can check it out.