Created
October 3, 2024 13:24
-
-
Save shiyuugohirao/b5de670fdebe788e5d84a55cf9cd1681 to your computer and use it in GitHub Desktop.
tiny methods for UnsplashAPI with openFrameworks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofURLFileLoader.h" | |
#include "ofFileUtils.h" | |
#include "ofUtils.h" | |
namespace ofxUnsplash { | |
string AccessKey; | |
inline void setupAccessKey(const string& accessKey) { | |
AccessKey = accessKey; | |
} | |
inline bool setupAccessKeyFromFile(const string& path) { | |
ofBuffer buffer = ofBufferFromFile(path); | |
AccessKey = buffer.getLines().begin().asString(); | |
return !AccessKey.empty(); | |
} | |
/// query : join with '&' (https://unsplash.com/documentation#search) | |
/// | |
/// imageParameters : https://unsplash.com/documentation#dynamically-resizable-images | |
/// example) "w=512&h=512" "fit=crop&crop=entropy" "crop=faces,edges" | |
/// w, h : for adjusting the width and height of a photo | |
/// crop : for applying cropping to the photo | |
/// fm : for converting image format | |
/// auto=format : for automatically choosing the optimal image format depending on user browser | |
/// q : for changing the compression quality when using lossy file formats | |
/// fit : for changing the fit of the image within the specified dimensions | |
/// dpr : for adjusting the device pixel ratio of the image | |
inline bool fetchUnsplashImage(ofImage& image, const string& query = "", const string& imageParameters = "") { | |
if (AccessKey.empty()) { | |
ofLogError("ofxUnsplash", "Not Assign AccessKey... First set AccessKey with ofxUnsplash::setupAccessKey()."); | |
return false; | |
} | |
float start = ofGetElapsedTimef(); | |
string searchURL = "https://api.unsplash.com/photos/random?count=1&client_id=" + AccessKey; | |
if (!query.empty()) searchURL += "&query=" + query; | |
ofStringReplace(searchURL, " ", "%20"); | |
auto searchRes = ofLoadURL(searchURL); | |
/* wait */ | |
if (searchRes.status == 200) { | |
ofLogVerbose("ofxUnsplash", "searchURL(%s)", searchURL.c_str()); | |
try { | |
ofJson result = ofJson::parse(searchRes.data); | |
cout << result.dump(4) << endl; | |
string imageURL = result[0]["urls"]["regular"]; | |
imageURL = ofSplitString(imageURL, "?")[0] + "?"; | |
if (image.getWidth() > 0 && image.getHeight() > 0) | |
imageURL += ofVAArgsToString("&w=%.0f&h=%.0f", image.getWidth(), image.getHeight()); | |
if (!imageParameters.empty()) | |
imageURL += (imageParameters[0] == '&' ? imageParameters : "&" + imageParameters); | |
ofStringReplace(imageURL, " ", "%20"); | |
if (ofLoadImage(image, imageURL) /* wait */) { | |
if (image.isUsingTexture()) image.update(); | |
ofLogNotice("ofxUnsplash", "Success to fetch image(%dx%d) from URL(%s)", | |
image.getPixels().getWidth(), image.getPixels().getHeight(), imageURL.c_str()); | |
} else { | |
ofLogError("ofxUnsplash", "Failed to fetch image from URL(%s)", imageURL.c_str()); | |
return false; | |
} | |
} catch (const std::exception& e) { | |
ofLogError("ofxUnsplash", "JSON parsing error: %s", e.what()); | |
return false; | |
} | |
} else { | |
ofLogError("ofxUnsplash", "Failed to search image...\n status:%d \n reason:%s", | |
searchRes.status, searchRes.error.c_str()); | |
return false; | |
} | |
ofLogVerbose("ofxUnsplash", "%s\ttook : %.4fsec", __FUNCTION__, ofGetElapsedTimef() - start); | |
return true; | |
} | |
} // namespace ofxUnsplash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment