duai4512 2016-12-13 19:43
浏览 72

如何检测图像字段的API帖子是文件,base64字符串还是图像URL?

I have a PHP Laravel app which is being used as an API for my frontend JavaScript App.

On my image fields I currently simply store an image URL in my database which would require the frontend app to handle uploading the image before posting to save it to my API.

My goal is to make all my API image fields accept any of these:

  • Binary file upload
  • Base64 Data for an image
  • URL of a remote image

My API would then detect which of the 3 types an incoming image field post is and handle it as needed. Binary file would upload the file and save the local image URL to the database, Base64 image would also save to a local image and save the URL to the DB, a remote image URL would download the image using something like cURL and save the local image URL to the DB.

What I described that I want to do is what API's like the one imgur.com has. Below are the API specs for creating a new image through their API:

Upload a new image with imgur.com API

Method  POST
Route   https://api.imgur.com/3/image

Parameters
Key        Required      Description
------------------------------------
image      required      A binary file, base64 data, or a URL for an image. (up to 10MB)

So my question now is, using PHP: How can I detect if a POST for my image field is an image file, a Base64 string, or an image URL string?

  • 写回答

2条回答 默认 最新

  • douxing9641 2016-12-13 20:01
    关注

    I suppose a real image file would come to you as a multi-part attachment. As such, you would detect this by sniffing out the $_FILES array: if there's an image there, a real image file was uploaded to your API. Specifically, in Laravel, you'd use $request->file

    Otherwise, then you would have a string value in the request's data. You could sniff out the value: if it starts with data:image it is an encoded image.

    Finally, you could look for strings starting with http or be smarter and look for a domain format, something like:

    /http(?:s)?:\/\/(?:[\w-]+\.)*([\w-]{1,63})(?:\.(?:\w{3}|\w{2}))(?:$|\/)/i
    

    Hope this helps!

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP