douwen3083
2018-05-15 15:47
浏览 59
已采纳

不能将Imagemagick与PHP的exec()中的PDF一起使用,从CLI可以使用

I'm trying to manage some PDFs using Imagemagick from PHP, using exec(). I've simplified down my test case, and found that while PDFs are fine from the command line, they're not working in PHP.

From the command line:

$ identify /var/tmp/doc-98563.png 
/var/tmp/doc-98563.png PNG 2550x3300 2550x3300+0+0 8-bit Gray 256c 1.24178MiB 0.000u 0:00.009

$ identify /var/tmp/doc-35765.pdf
/var/tmp/doc-35765.pdf[0] PDF 419x595 419x595+0+0 16-bit ColorSeparation CMYK 997321B 0.120u 0:00.119
/var/tmp/doc-35765.pdf[1] PDF 419x595 419x595+0+0 16-bit ColorSeparation CMYK 997321B 0.110u 0:00.109

Great! Correctly identifies my files, PNG or PDF.

I can also confirm permissions are just fine on the files:

$ ls -la /var/tmp/doc*
-rw-r--r--  1 _www  wheel   1.6M 15 May 10:05 /var/tmp/doc-35765.pdf
-rw-r--r--  1 _www  wheel   1.2M 15 May 10:01 /var/tmp/doc-98563.png

Running the equivalent from PHP...

exec('identify /var/tmp/doc-98563.png', $output, $exitcode);
var_dump($output);
var_dump($exitcode);

unset($output);

exec('identify /var/tmp/doc-35765.pdf', $output, $exitcode);
var_dump($output);
var_dump($exitcode);

...produces different results:

array(1) {
    [0]=>
    string(93) "/var/tmp/doc-98563.png PNG 2550x3300 2550x3300+0+0 8-bit Gray 256c 1.24178MiB 0.000u 0:00.000"
}
int(0)
array(0) {
}
int(1)

So the PNG works identically from both places (as is expected), but the PDF just... doesn't.

I've checked convert -version and convert -list format on both, and both are identical (ImageMagick 7.0.7-32, PDF PDF rw+ Portable Document Format). I've also specific the full path /usr/local/bin/identify ... to verify it's definitely running the same instance of IM. File permissions are identical for everything I can see related to PDF vs PNG, so don't think it's that.

Running gs DOES work fine from PHP with the same file, but gs doesn't do what I need.

Anything else I can try?

For reference, I'm on OSX using PHP7.2, and both it and ImageMagick (and Ghostscript) are installed with Brew.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用Imagemagick从PHP管理一些PDF,使用 exec() 。 我已经简化了我的测试用例,并发现尽管PDF在命令行中运行良好,但它们并不适用于PHP。

从命令行: \ n

  $ identify /var/tmp/doc-98563.png 
 / var / tmp / doc-98563.png PNG 2550x3300 2550x3300 + 0 + 0 8位灰色256c 1.24178MiB 0.000u 0  :00.009 
 
 $ identify /var/tmp/doc-35765.pdf
/var/tmp/doc-35765.pdf[0] PDF 419x595 419x595 + 0 + 0 16位ColorSeparation CMYK 997321B 0.120u 0:  00.119 
 / var / tmp / doc-35765.pdf [1] PDF 419x595 419x595 + 0 + 0 16位ColorSeparation CMYK 997321B 0.110u 0:00.109 
   
 
 

大! 正确识别我的文件,PNG或PDF。

我还可以确认文件的权限正常:

  $ ls -la  / var / tmp / doc * 
-rw-r  -  r-- 1 _www wheel 1.6M 5月15日10:05 /var/tmp/doc-35765.pdf
-rw-r--r-- 1  _www wheel 1.2M 15 May 10:01 /var/tmp/doc-98563.png
nn

从PHP中运行相当于...... exec('识别/var/tmp/doc-98563.png',$ output,$ exitcode); var_dump($ output); var_dump($ exitcode); unset( $ output); exec('识别/var/tmp/doc-35765.pdf',$ output,$ exitcode); var_dump($ output); var_dump($ exitcode);

...产生不同的结果:

  array(1){
 [0] => 
 string(93  )“/ var / tmp / doc-98563.png PNG 2550x3300 2550x3300 + 0 + 0 8位灰色256c 1.24178MiB 0.000u 0:00.000”
} 
int(0)
array(0){
} \  nint(1)
   
 
 

因此,PNG在两个地方的工作方式相同(如预期的那样),但PDF只是...不会。

我检查了 convert -version convert -list格式,两者都是相同的(ImageMagick 7.0.7-32, PDF PDF rw + Portable Document Format )。 我还指定了完整路径 / usr / local / bin / identify ... 来验证它肯定运行相同的IM实例。 对于我可以看到的与PDF与PNG相关的所有内容,文件权限都是相同的,所以不要认为是这样。

运行 gs 从PHP运行正常 相同的文件,但gs没有做我需要的。

我还能尝试其他什么吗?

作为参考,我在OSX上使用 PHP7.2,它和ImageMagick(和Ghostscript)都与Brew一起安装。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doupiai5597 2019-02-14 23:45
    已采纳

    This is a common issue. It seems that the PHP environment typically cannot find Ghostscript. One way to solve that is to edit your installed delegates.xml file to put the full path to ghostscript, i,e., gs in all the entries for command=""gs". This would be for PDF, PS, EPS. Another way is to modify your PHP environment variables to put GS in your PHP PATH.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题