error converting image by imagemagick - php

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.

Related

Failed to load module when script called by webapp, perfectly running when running from cli

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.

Why i get "Failed to invoke gs" for ghostscript PDF password checker in php

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.

Getting mimetype from URL in PHP on Windows

I try to write a script that would fetch some external resource and count it's size. To do so I need to detect if the given URL provides some binary file (i.e. graphics) or some HTML document. I thought of checking mimetype but when I run following code:
$finfo = new \finfo(FILEINFO_MIME);
return $finfo->file($this->url);
I get the information Warning: finfo::file(): Failed identify data 0:no magic files loaded in. I've already uncommented the line extension=php_fileinfo.dll in my php.ini but for some reason there's no mimetypes definition. I run the code on standard xampp instalation.
How can I fix it? Or maybe you have some other ideas for such detection?

ImageMagick getting uncaught Fatal error after doing 5-7 sets of PDF to JPG

I'm on as following
Ubuntu 12.04 LTS
PHP 5.3.10-1ubuntu3.18 with Suhosin-Patch (cli)
ImageMagick 6.6.9-7
GPL Ghostscript 9.05 (2012-02-08)
my gs is under /usr/bin
I'm converting continuously - PDF to JPG each pdf page to single image. It worked first time for 1-30 sets of PDF but now I'm getting error after 7 sets of PDF being complete
running the PHP script in command line. After converting 5-7 sets of PDF to JPG separate images (about 1000 JPG) i'm getting the following error
PHP Fatal error: Uncaught exception 'ImagickException' with message 'Postscript delegate failed `../books/58/58.pdf': # error/pdf.c/ReadPDFImage/663' in /var/www/mysite/public_html/admin/lib.php:10
Stack trace:
#0 /var/www/mysite/public_html/admin/lib.php(10): Imagick->__construct('../books/58/58....')
#1 /var/www/mysite/public_html/admin/test.php(118): include('/var/www/stagin...')
#2 {main}
thrown in /var/www/mysite/public_html/admin/lib.php on line 10
lib.php has the script to convert the PDF to JPG - code as below
<?php
$file_name = "../books/$book_id/$book_id".'.pdf';
mkdir("../books/$book_id/pages");
// Strip document extension
$file_name = basename($file_name, '.pdf');
// Convert this document
// Each page to single image
$img = new imagick("../books/$book_id/$book_id".'.pdf');
// Set image resolution
// Determine num of pages
$img->setResolution(300,300);
$num_pages = $img->getNumberImages();
// Convert PDF pages to images
for($i = 0;$i < $num_pages; $i++) {
// Set iterator postion
$img->setIteratorIndex($i);
// Set image format
$img->setImageFormat('jpeg');
// Write Images to temp 'upload' folder
$img->writeImage("../books/$book_id/pages/$i".'.jpg');
}
$img->destroy();
?>
After getting the error it stop the converting, but when I run the script again it works for another 5-7 sets of PDF and stop again.
Can anyone please tell me what i'm missing here.
I appreciate your kind help.
Thank you in advance.
As #KenS suggested, this is probably a case where calling GhostScript directly would be better, as it would be both faster, and not crash.
I would suspect that the issue that you're seeing is that some resource is being held open by ImageMagick, and that at some point the processing just starts failing because there is no more of that resource available.
If you wanted to investigate that, strace would be the tool to do it with. However just calling GS directly would allow you to get on to more important stuff.

I can't make ImageMagick work from a browser

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.

Categories