Php webservice,通过POST获取JSON并回送映像 [英] Php webservice that takes JSON via POST and spits back an image

查看:296
本文介绍了Php webservice,通过POST获取JSON并回送映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(序言:这似乎是一个想做的典型事情,我很惊讶我没有立即找到关于它的例子和教程。
所以我认为这样做很有价值一个StackOverflow问题。
指向相关示例和教程的指针肯定会得到欢迎答案。)

为了使这个具体,目标是webservice通过POST请求接受JSON格式的数据。
数据只是一个单位整数数组,例如 [3,2,1]

To make this concrete, the goal is a webservice that accepts data in JSON format via a POST request. The data is simply an array of single-digit integers, e.g., [3, 2, 1].

在服务器上有名为0.png,1.png,2.png等的图像。
webservice获取与JSON数组中指定的图像相对应的图像,并将它们组合成蒙太奇,使用标准的ImageMagick命令行工具。
例如,

On the server are images named 0.png, 1.png, 2.png, etc. The webservice takes the images corresponding to those specified in the JSON array and composes them into a montage, using the standard ImageMagick command line tool. For example,

montage 3.png 2.png 1.png 321.png

创建一个新的单个图像,321.png,由3.png,2.png和1.png组成,全部在一行。

creates a new single image, 321.png, composed of 3.png, 2.png, and 1.png, all in a row.

接受的答案将采用实现上述功能的完整PHP代码的形式。
(如果没人打败我,我会写的。)

The accepted answer will be in the form of complete PHP code that implements the above. (I'll write it if no one beats me to it.)

推荐答案

感谢oezi 提供所有部分。
这是完整的PHP程序:

Thanks to oezi for providing all the pieces. Here's the complete PHP program:

<?php
$nums = json_decode($_REQUEST['nums']);

# Lambda functions are a little less ridiculous in php 5.3 but this is the best
# way I know how to do this in php 5.2:
function f($x) { return "$x.png"; }
$cmd = "montage " . implode(" ", array_map("f", $nums)) . " tmp.png";

exec($cmd);

header('Content-type: image/png');
readfile('tmp.png');
?>

尝试如下:

http://yootles.com/nmontage/go.php?nums=[2,4,6]

你应该得到这个:

246 http://yootles.com/nmontage/246.png

(当然这是GET而不是POST,但是php程序接受。)

(That's GET instead of POST of course, but the php program accepts either.)

这篇关于Php webservice,通过POST获取JSON并回送映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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