2012-08-21 16:16
浏览 77

imagemagick convert命令在php脚本中无法正常工作,但可以从命令行工作

A system call to convert in php worked for years but quit working early this year. All commands that I am using work correctly if issued from the command line using Putty.

A test php file I created to debug this is

$string = "convert  -verbose -resize 200x200  \"startline/main_menu.png\" \"image/temp222_main_menu.png\" ";

<IMG SRC='image/temp222_main_menu.png '  alt='main_menu'>

The output file is not created. I get this response

startline/main_menu.png PNG 600x1024 600x1024+0+0 8-bit DirectClass 92.1KB 0.020u 0:00.020

If I replace my string with this

$string = "convert  -version -resize 200x200  \"startline/main_menu.png\" \"image/temp222_main_menu.png\" ";

The output file is created but not resized.

I get this response:

Version: ImageMagick 6.6.0-4 2012-04-26 Q16 http://www.imagemagick.org Copyright: Copyright (C) 1999-2010 ImageMagick Studio LLC Features: OpenMP

The interesting thing is that this worked from 2008 until early this year. That may have been when I switched to PHP5 and a new server, not sure. This is running at 1and1.com

Tech support at 1and1 said the problem was the outfile didn't exist and closed the case. That was not very helpful.

I am wondering if there is something I need to put in my php.ini file or something.

I also tried this: exec("/usr/bin/convert -resize 200x200 image/temp222_main_menu.png"); ?>

Just a note. I put the verbose in so that I could be sure that I was in fact running the program, that there was some output returned. It also shows that it is loading the correct file. That is also why I used passthru instead of exec or system. This is just a test script. The script I was using before it broke did not use passthru but had too much going on to post. The key is that the string that is executed does not work in php but does work on the command line so it is correct.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doubaisui2526
    doubaisui2526 2012-08-25 00:13

    I just went through this exact same problem. The really odd thing I noticed is that if the image happened to be the exact size I was trying to resize to, then it worked. Anywawy, It seems to be an issue with memory usage and can be fixed by a config setting in PHP. After browsing other forums I found this and it fixed it.

    Place this line at the top of your PHP script that calls imagemagick:

    点赞 评论
  • dongpu2727
    dongpu2727 2012-08-21 19:37

    You should try these steps to debug the problem.

    First, verify if the setup works at all, by providing full paths to all filenames and commands:

    exec("which convert");
    exec("/path/to/convert  /path/to/a.png  -resize 200x200  /tmp/a200x200.png");

    (Obviously, you'll have to adapt paths to your situation.)

    Then, check if the user account your PHP is running under has even the privilege to write to your target directory:

    exec("touch $(pwd)/image/temp222_main_menu.png");

    I would change your code to this to get it working at all:

    exec("/usr/bin/convert -resize 200x200  image/temp222_main_menu.png");
    <IMG SRC='image/temp222_main_menu.png'  alt='main_menu'>


    1. ...I removed the -verbose part (it only generates an additional line on stderr startline/main_menu.png PNG 600x1024 600x1024+0+0 8-bit DirectClass 92.1KB 0.020u 0:00.020 and is probably the cause why the convert command never completes the conversion.

    2. ...I didn't use your -version modification, because it doesn't make sense in your context (and it also leads to skipping the -resize part).

    3. ...I removed a space in your <IMG SRC=.... line.

    4. ...I switched to exec(...)

    If you insist to add some noise to stderr output, you can try to add the -monitor parameter instead of the -verbose...

    点赞 评论