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

Sample 4.1: A single file input

一、定义

示例4.1:单个文件输入

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

  • Example.jpeg 是一个新的JPEG格式,宽度为512像素
  • Example.png 是新的全尺寸PNG格式
curl -X POST \
  https://image.adobe.io/pie/psdService/renditionCreate \
  -H 'Authorization: Bearer <auth_token>' \
  -H 'Content-Type: application/json' \
  -H 'x-api-key: <YOUR_API_KEY>' \
  -d '{
  "inputs":[
    {
      "href":"<SIGNED_GET_URL>",
      "storage":"external"
    }
  ],
  "outputs":[
    {
      "href":"<SIGNED_POST_URL1>",          
      "width": 512,
      "storage":"external",
      "type":"image/jpeg"      
    },
    {
      "href":"<SIGNED_POST_URL2>",
      "storage":"external",
      "type":"image/png"
    }
  ]
}'
关于<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/

 

二、代码

#include <iostream>
#include <curl/curl.h>
#include <json/json.h>
#pragma warning(disable:4996)
using namespace std;


wstring AsciiToUnicode(const string& str)
{
	// 预算-缓冲区中宽字节的长度  
	int unicodeLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0);
	// 给指向缓冲区的指针变量分配内存  
	wchar_t *pUnicode = (wchar_t*)malloc(sizeof(wchar_t)*unicodeLen);
	// 开始向缓冲区转换字节  
	MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, pUnicode, unicodeLen);
	wstring ret_str = pUnicode;
	free(pUnicode);
	return ret_str;
}

string UnicodeToUtf8(const wstring& wstr)
{
	// 预算-缓冲区中多字节的长度  
	int ansiiLen = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
	// 给指向缓冲区的指针变量分配内存  
	char *pAssii = (char*)malloc(sizeof(char)*ansiiLen);
	// 开始向缓冲区转换字节  
	WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, pAssii, ansiiLen, nullptr, nullptr);
	string ret_str = pAssii;
	free(pAssii);
	return ret_str;
}


string AsciiToUtf8(const string& str)
{
	return UnicodeToUtf8(AsciiToUnicode(str));
}
 
/* 不考虑任何异常的情况, 简单的get请求 */
/***
 *  buffer 		接收数据所在的缓冲区
 * 	size		要读取的字节数
 *  count		读写size长度的数据count次
 *  response	用户自定义文件指针
 */
size_t getUrlResponse(char* buffer, size_t size, size_t count, string* response) {
	size_t recv_size = size * count;
	response->clear();
	response->append(buffer);
	return recv_size;
}

string get(const string& url) {
	// 请求数据
	string response;
	// easy handle声明
	CURL *handle;
	// 初始化handle
	handle = curl_easy_init();
	// 设置url
	curl_easy_setopt(handle, CURLOPT_URL, url.c_str());

	//设置curl的请求头
	struct curl_slist* header_list = NULL;
	header_list = curl_slist_append(header_list, "Content-Type:application/json");
	header_list = curl_slist_append(header_list, "Authorization:Bearer eyJhbGciOiJSUzI1NiIsIng1dSI6Imltc19uYTEta2V5LTEuY2VyIn0.eyJpZCI6IjE2MjA4NzA4Nzk3MzVfMjc1MmRkZWMtYjlhZS00ZTg1LTk2YzItYWY3ZDZhNDBhNmM1X3VlMSIsImNsaWVudF9pZCI6ImUyMDk2MTc3ZTEzZjRhZDg5YzliZGE0ODliNzgyNGUxIiwidXNlcl9pZCI6IjUwODc3NjgxNjA4Njg2M0EwQTQ5NUM3MUB0ZWNoYWNjdC5hZG9iZS5jb20iLCJ0eXBlIjoiYWNjZXNzX3Rva2VuIiwiYXMiOiJpbXMtbmExIiwiYWFfaWQiOiI1MDg3NzY4MTYwODY4NjNBMEE0OTVDNzFAdGVjaGFjY3QuYWRvYmUuY29tIiwiZmciOiJWTjJGRkxZWUZMUDU1SEVDU01aTFZIUUFYST09PT09PSIsIm1vaSI6ImE0NmE5ZDJhIiwiZXhwaXJlc19pbiI6Ijg2NDAwMDAwIiwic2NvcGUiOiJvcGVuaWQsQWRvYmVJRCxyZWFkX29yZ2FuaXphdGlvbnMiLCJjcmVhdGVkX2F0IjoiMTYyMDg3MDg3OTczNSJ9.R4421dBBIbACk7lIfHC8U8JNAZ8_A3CdUrAYHp77dARJ6NfvnjXLofRz5PktjbYXPfmG1jWI0GDDQ3EAhvuphqCIJ1Oz2yVoGxIoGgJFp769r6vowkA4ou--ptnamkETu8OqD5ua40WSRWeI74Ew6qNVMuTH3AnH0W891nTi3FxcF1ohLlHpaFNBSMcNvYBc1Nf_o6D-mj2FDEh_uoJav_nXL_tnWIZCJgVKDkgwDDHN4XZ7Yv7B05D78W2eUswajGOq-Z3nE6OgJHscfxOUlCEUSinlTwqw8YbHWNzRORaiyql3ItPTC6Ot8fdb1atK08FAlsWenW3u_IkTs8-mjg");
	header_list = curl_slist_append(header_list, "x-api-key:e2096177e13f4ad89c9bda489b7824e1");
	curl_easy_setopt(handle, CURLOPT_HTTPHEADER, header_list);

	//设置ssl验证
	curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, false);
	curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, false);

 

	// 构建json数组
	Json::Value obj;
	Json::Value arrayinputs;
	Json::Value arrayoutputs;
	Json::Value inputsroot;
	Json::Value outputsroot;
	Json::Value inputs;
	Json::Value outputs;
	Json::FastWriter writer;
	//https://www.dropbox.com/s/zn8gj1kzktnmp4t/Example.psd?dl=0
	//https://github.com/greless/photoshop-api-docs-pre-release/raw/master/sample_files/Example.psd
	//https://dl-web.dropbox.com/account_photo/get/dbaphid%3AAABz1l-fZg8hPvIe63x3-neHllYtVpQPxNQ?size=128x128&vers=1620873384663

	inputs["href"] = "https://www.dropbox.com/s/zn8gj1kzktnmp4t/Example.psd?dl=0";//
	inputs["storage"] = "external"; 
	inputsroot.append(inputs);
	//obj["inputs"] = Json::Value(inputsroot);
	obj["inputs"] = inputsroot;
	 
	outputs["href"] = "https://www.dropbox.com/s/zn8gj1kzktnmp4t/Example.psd?dl=0";//输出图片地址应该怎么填?
	outputs["storage"] = "external";
	outputs["type"] = "image/png";
	 outputsroot.append(outputs);
	obj["outputs"] = outputsroot;

	// 子节点挂到根节点上
	//array["array"] = Json::Value(root); 
	//obj.append(arrayinputs);
	//obj.append(arrayoutputs); 
	string data = writer.write(obj);

	cout <<"json数据:" << endl << data << endl;
	cout << "格式化json数据:" << endl << obj.toStyledString() << endl;
	// std::string jsonout = item.toStyledString();
	 //data = AsciiToUtf8(data);

	//设置post请求的参数
	curl_easy_setopt(handle, CURLOPT_POSTFIELDS, data.c_str());

	// 注册回调函数
	curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, &getUrlResponse);
	 
	// 获取信息
	curl_easy_setopt(handle, CURLOPT_WRITEDATA, &response);
	// 执行请求
	curl_easy_perform(handle);
	// 释放
	curl_easy_cleanup(handle);

	return response;
}
int main() {
	// 使用前初始化libcurl, 只需初始化一次
	curl_global_init(CURL_GLOBAL_DEFAULT);
	// 执行请求
 cout << endl<<"返回:" <<get("https://image.adobe.io/pie/psdService/renditionCreate") << endl;;
	//cout << get("https://image.adobe.io/pie/psdService/hello");
	// 释放libcurl相关资源
	curl_global_cleanup();
	getchar();
	return 0;
}

三、返回

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

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

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值