小言_互联网的博客

【Photoshop API 】五、 实际案例:PSD文件转换生成新的图像格式

238人阅读  评论(0)

Sample 4.1: A single file input

一、定义

示例4.1:单个文件输入

此示例API调用将从Example.psd输入中请求两个不同的输出表示形式:

  • Example.jpeg 是一个新的JPEG格式,宽度为512像素
  • Example.png 是新的全尺寸PNG格式

  
  1. curl -X POST \
  2. https://image.adobe.io/pie/psdService/renditionCreate \
  3. -H 'Authorization: Bearer <auth_token>' \
  4. -H 'Content-Type: application/json' \
  5. -H 'x-api-key: <YOUR_API_KEY>' \
  6. -d '{
  7. "inputs":[
  8. {
  9. "href":" <SIGNED_GET_URL>",
  10. "storage":"external"
  11. }
  12. ],
  13. "outputs":[
  14. {
  15. "href":" <SIGNED_POST_URL1>",
  16. "width": 512,
  17. "storage":"external",
  18. "type":"image/jpeg"
  19. },
  20. {
  21. "href":" <SIGNED_POST_URL2>",
  22. "storage":"external",
  23. "type":"image/png"
  24. }
  25. ]
  26. }'
关于<SIGNED_GET_URL>怎么传我目前还不清楚.

Input and Output file storage

Clients can use assets stored on one of the following storage types: External: AWS S3: By using a presigned GET/PUT URL Azure: By generating a SAS (Shared Access Signature) for upload/download Dropbox: Generate temporary upload/download links using https://dropbox.github.io/dropbox-api-v2-explorer/

 

二、代码


  
  1. #include <iostream>
  2. #include <curl/curl.h>
  3. #include <json/json.h>
  4. #pragma warning(disable:4996)
  5. using namespace std;
  6. wstring AsciiToUnicode(const string& str)
  7. {
  8. // 预算-缓冲区中宽字节的长度
  9. int unicodeLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0);
  10. // 给指向缓冲区的指针变量分配内存
  11. wchar_t *pUnicode = ( wchar_t*) malloc( sizeof( wchar_t)*unicodeLen);
  12. // 开始向缓冲区转换字节
  13. MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, pUnicode, unicodeLen);
  14. wstring ret_str = pUnicode;
  15. free(pUnicode);
  16. return ret_str;
  17. }
  18. string UnicodeToUtf8(const wstring& wstr)
  19. {
  20. // 预算-缓冲区中多字节的长度
  21. int ansiiLen = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
  22. // 给指向缓冲区的指针变量分配内存
  23. char *pAssii = ( char*) malloc( sizeof( char)*ansiiLen);
  24. // 开始向缓冲区转换字节
  25. WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, pAssii, ansiiLen, nullptr, nullptr);
  26. string ret_str = pAssii;
  27. free(pAssii);
  28. return ret_str;
  29. }
  30. string AsciiToUtf8(const string& str)
  31. {
  32. return UnicodeToUtf8(AsciiToUnicode(str));
  33. }
  34. /* 不考虑任何异常的情况, 简单的get请求 */
  35. /***
  36. * buffer 接收数据所在的缓冲区
  37. * size 要读取的字节数
  38. * count 读写size长度的数据count次
  39. * response 用户自定义文件指针
  40. */
  41. size_t getUrlResponse(char* buffer, size_t size, size_t count, string* response) {
  42. size_t recv_size = size * count;
  43. response->clear();
  44. response->append(buffer);
  45. return recv_size;
  46. }
  47. string get(const string& url) {
  48. // 请求数据
  49. string response;
  50. // easy handle声明
  51. CURL *handle;
  52. // 初始化handle
  53. handle = curl_easy_init();
  54. // 设置url
  55. curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
  56. //设置curl的请求头
  57. struct curl_slist* header_list = NULL;
  58. header_list = curl_slist_append(header_list, "Content-Type:application/json");
  59. header_list = curl_slist_append(header_list, "Authorization:Bearer eyJhbGciOiJSUzI1NiIsIng1dSI6Imltc19uYTEta2V5LTEuY2VyIn0.eyJpZCI6IjE2MjA4NzA4Nzk3MzVfMjc1MmRkZWMtYjlhZS00ZTg1LTk2YzItYWY3ZDZhNDBhNmM1X3VlMSIsImNsaWVudF9pZCI6ImUyMDk2MTc3ZTEzZjRhZDg5YzliZGE0ODliNzgyNGUxIiwidXNlcl9pZCI6IjUwODc3NjgxNjA4Njg2M0EwQTQ5NUM3MUB0ZWNoYWNjdC5hZG9iZS5jb20iLCJ0eXBlIjoiYWNjZXNzX3Rva2VuIiwiYXMiOiJpbXMtbmExIiwiYWFfaWQiOiI1MDg3NzY4MTYwODY4NjNBMEE0OTVDNzFAdGVjaGFjY3QuYWRvYmUuY29tIiwiZmciOiJWTjJGRkxZWUZMUDU1SEVDU01aTFZIUUFYST09PT09PSIsIm1vaSI6ImE0NmE5ZDJhIiwiZXhwaXJlc19pbiI6Ijg2NDAwMDAwIiwic2NvcGUiOiJvcGVuaWQsQWRvYmVJRCxyZWFkX29yZ2FuaXphdGlvbnMiLCJjcmVhdGVkX2F0IjoiMTYyMDg3MDg3OTczNSJ9.R4421dBBIbACk7lIfHC8U8JNAZ8_A3CdUrAYHp77dARJ6NfvnjXLofRz5PktjbYXPfmG1jWI0GDDQ3EAhvuphqCIJ1Oz2yVoGxIoGgJFp769r6vowkA4ou--ptnamkETu8OqD5ua40WSRWeI74Ew6qNVMuTH3AnH0W891nTi3FxcF1ohLlHpaFNBSMcNvYBc1Nf_o6D-mj2FDEh_uoJav_nXL_tnWIZCJgVKDkgwDDHN4XZ7Yv7B05D78W2eUswajGOq-Z3nE6OgJHscfxOUlCEUSinlTwqw8YbHWNzRORaiyql3ItPTC6Ot8fdb1atK08FAlsWenW3u_IkTs8-mjg");
  60. header_list = curl_slist_append(header_list, "x-api-key:e2096177e13f4ad89c9bda489b7824e1");
  61. curl_easy_setopt(handle, CURLOPT_HTTPHEADER, header_list);
  62. //设置ssl验证
  63. curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, false);
  64. curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, false);
  65. // 构建json数组
  66. Json::Value obj;
  67. Json::Value arrayinputs;
  68. Json::Value arrayoutputs;
  69. Json::Value inputsroot;
  70. Json::Value outputsroot;
  71. Json::Value inputs;
  72. Json::Value outputs;
  73. Json::FastWriter writer;
  74. //https://www.dropbox.com/s/zn8gj1kzktnmp4t/Example.psd?dl=0
  75. //https://github.com/greless/photoshop-api-docs-pre-release/raw/master/sample_files/Example.psd
  76. //https://dl-web.dropbox.com/account_photo/get/dbaphid%3AAABz1l-fZg8hPvIe63x3-neHllYtVpQPxNQ?size=128x128&vers=1620873384663
  77. inputs[ "href"] = "https://www.dropbox.com/s/zn8gj1kzktnmp4t/Example.psd?dl=0"; //
  78. inputs[ "storage"] = "external";
  79. inputsroot.append(inputs);
  80. //obj["inputs"] = Json::Value(inputsroot);
  81. obj[ "inputs"] = inputsroot;
  82. outputs[ "href"] = "https://www.dropbox.com/s/zn8gj1kzktnmp4t/Example.psd?dl=0"; //输出图片地址应该怎么填?
  83. outputs[ "storage"] = "external";
  84. outputs[ "type"] = "image/png";
  85. outputsroot.append(outputs);
  86. obj[ "outputs"] = outputsroot;
  87. // 子节点挂到根节点上
  88. //array["array"] = Json::Value(root);
  89. //obj.append(arrayinputs);
  90. //obj.append(arrayoutputs);
  91. string data = writer.write(obj);
  92. cout << "json数据:" << endl << data << endl;
  93. cout << "格式化json数据:" << endl << obj.toStyledString() << endl;
  94. // std::string jsonout = item.toStyledString();
  95. //data = AsciiToUtf8(data);
  96. //设置post请求的参数
  97. curl_easy_setopt(handle, CURLOPT_POSTFIELDS, data.c_str());
  98. // 注册回调函数
  99. curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, &getUrlResponse);
  100. // 获取信息
  101. curl_easy_setopt(handle, CURLOPT_WRITEDATA, &response);
  102. // 执行请求
  103. curl_easy_perform(handle);
  104. // 释放
  105. curl_easy_cleanup(handle);
  106. return response;
  107. }
  108. int main() {
  109. // 使用前初始化libcurl, 只需初始化一次
  110. curl_global_init(CURL_GLOBAL_DEFAULT);
  111. // 执行请求
  112. cout << endl<< "返回:" <<get( "https://image.adobe.io/pie/psdService/renditionCreate") << endl;;
  113. //cout << get("https://image.adobe.io/pie/psdService/hello");
  114. // 释放libcurl相关资源
  115. curl_global_cleanup();
  116. getchar();
  117. return 0;
  118. }

三、返回

调用此API将初始化一个异步作业并返回一个包含href的响应。使用href中的值来轮询作业的状态。

为什么令牌无效?重新生成的,Welcome to the Photoshop API! 访问也是正常的

{"error_code":"401013","message":"Oauth token is not valid"}

 

 


转载:https://blog.csdn.net/greless/article/details/116742328
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场