内容而不是外壳中的路径 [英] content instead of path in shell

查看:99
本文介绍了内容而不是外壳中的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pngquant对于php具有以下示例

Pngquant has the following example for php

// '-' makes it use stdout, required to save to $compressed_png_content variable
    // '<' makes it read from the given file path
    // escapeshellarg() makes this safe to use with any path
    $compressed_png_content = shell_exec("pngquant --quality=$min_quality-$max_quality - < ".escapeshellarg(    $path_to_png_file));

我想用实际内容替换$path_of_file.

这将避免在将文件从一种格式转换为png并对其进行优化时浪费I/O

This will avoid wasting I/O when converting a file from one format to png and then optimize it

在这种情况下,新的shell_exec()命令是什么

What will be the new shell_exec() command in that situation

推荐答案

我不是PHP专家,但是我相信您正在寻找通往另一进程的2通管道(读写),以便可以写入数据到其 stdin 并从其 stdout 中读取数据.因此,我认为这意味着您需要proc_open(),它在此处中进行了描述

I am no PHP expert, but I believe you are looking for a 2-way pipe (write and read) to another process, so that you can write data to its stdin and read data from its stdout. So, I think that means you need proc_open() which is described here.

它看起来像这样(未经测试):

It will look something like this (untested):

$cmd = 'pngquant --quality ... -';

$spec = array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w"));

$process = proc_open($cmd, $spec, $pipes);

if (is_resource($process)) 
{

    // write your data to $pipes[0] so that "pngquant" gets it
    fclose($pipes[0]);

    $result=stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    proc_close($process);
}

这篇关于内容而不是外壳中的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆