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.

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效