dousilie9522 2010-10-15 06:33
浏览 40

通过POST获取JSON并吐出图像的Php Web服务

(Preamble: This seems like such a typical thing to want to do that I was surprised I didn't immediately find examples and tutorials about it. So I thought it would be valuable to have as a StackOverflow question. Pointers to related examples and tutorials will certainly be welcome answers.)

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].

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

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

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.)

  • 写回答

2条回答 默认 最新

  • doukang7486 2010-10-15 06:47
    关注

    some hints, i won't write the complete code for you:

    • to get your array back on php-side, there is json_decode. ise it like this:

      $images = json_decode($_POST['whatever']);
    • to get the command for montage, do something like this (note: you should valitate all input you get via post, i'm going to leave this out and focus on th "complicated" parts):

      $cmd = "montage";
      foreach($images as $image){
        $cmd .= " ".$image.".png";
      }
      $cmd .= " temp.png";
    • now you can execute your command using exec or one of his friends:

      exec($cmd);
    • at least, set a png-header and use readfile or something similar to get you "tmp.png"

    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退