简体   繁体   English

PHP:获取optipng的输出

[英]PHP: Get output of optipng

I'm using jpegoptim and optipng to optimize images from a php-script which is called in the browser. 我正在使用jpegoptim和optipng来优化浏览器中调用的php脚本中的图像。 I'm doing this: 我这样做:

$s = exec('/myoptimizer/jpegoptim -m60 -o --strip-all /httpd/images/myimage.jpg');
print $s;

This works fine and outputs: 这工作正常,输出:

/httpd/images/myimage.jpg 600x90 24bit P JFIF [OK] 14572 --> 14542 bytes (0.21%), optimized. 

Now the same with optipng: 现在和optipng一样:

$s = exec('/myoptimizer/optipng -o7 -preserve -strip all /httpd/images/myimage.png /httpd/images/myimage.png', $aOutput, $ret);

var_dump($s);
var_dump($a);
var_dump($ret);

But:
$s is empty.
$a is empty array.
$ret is 0.

Doing the same command on the console, I get: 在控制台上执行相同的命令,我得到:

** Processing: /httpd/images/myimage.png
60x18 pixels, 4x8 bits/pixel, RGB+alpha
Input IDAT size = 2235 bytes
Input file size = 2292 bytes

Trying:
  zc = 9  zm = 9  zs = 0  f = 5         IDAT size = 2235
  zc = 9  zm = 8  zs = 0  f = 5         IDAT size = 2235
  zc = 8  zm = 9  zs = 0  f = 5         IDAT size = 2235
  zc = 8  zm = 8  zs = 0  f = 5         IDAT size = 2235

/httpd/images/myimage.png is already optimized.

And: 和:

$s = exec('/myoptimizer/optipng -h', $aOutput, $ret);
print $s;

works. 作品。

How can I get the output in PHP? 如何在PHP中获得输出? I tried shell_exec, passthru and system as well, but: No output at all. 我也尝试过shell_exec,passthru和system,但是:根本没有输出。

The 2nd argument of the exec() method captures the standard output of the executed command. exec()方法的第二个参数捕获已执行命令的标准输出。 In your case, optipng does not output it's result to the standard output BUT to the standard error. 在您的情况下,optipng不会将其结果输出到标准输出BUT到标准错误。 You can redirect the std error message to the std output by adding 2>&1 to the end of your executing program, in this case your code will look like this: 您可以通过在执行程序的末尾添加2>&1将std错误消息重定向到std输出,在这种情况下,您的代码将如下所示:

<?php
$imagePath = '/httpd/images/myimage.png';
$comand    = '/myoptimizer/optipng -o7 -preserve -strip all';

exec("$command $imagePath 2>&1", $outout);

var_dump($output);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM