Examples
Example - 1: Python snippet for auto resize and align
import requests
import os
# Set your API key as an environment variable
API_KEY = os.getenv('CROP_PHOTO_API_KEY')
# URL prefix and API endpoint
URL_PREFIX = 'https://us1-api.crop.photo/v1'
API_ENDPOINT = '/crop/auto-resize-align'
# Define the method to send the API request
def send_auto_resize_request(image_url):
# Full URL (URL_PREFIX + API_ENDPOINT)
url = f"{URL_PREFIX}{API_ENDPOINT}"
# Define the request headers
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Bearer {API_KEY}'
}
# Define the request payload (body)
data = {
"image_url": image_url,
"background": "#FFFFFF", # White background
"preserve_edge_contact": True, # Maintain edge contact
"output_format": "jpeg", # Output in JPEG format
"output_dimension": "2000x3000", # Specify output dimensions
"response_type": "url", # Return response as a URL
"margins": {
"minimum_margin": 8 # Set minimum margin
}
}
# Send the POST request to the API
response = requests.post(url, json=data, headers=headers)
# Return both JSON response and status code
return response.json(), response.status_code
# Example usage of the method
if __name__ == "__main__":
image_url = "https://images.pexels.com/photos/28836959/pexels-photo-28836959/free-photo-of-minimalist-overhead-italian-coffee-on-blue-surface.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2"
json_response, status_code = send_auto_resize_request(image_url)
print(f"Status Code: {status_code}")
print(f"Response: {json_response}")
Example - 2: Node.js snippet for auto resize and align
// Set your API key directly here
const API_KEY = 'your_api_key_here'; // Replace with your actual API key
// Function to send the API request
async function sendAutoResizeRequest(imageUrl) {
const url = 'https://us1-api.crop.photo/v1/crop/auto-resize-align'; // Full URL
const data = {
image_url: imageUrl,
background: "#FFFFFF", // White background
preserve_edge_contact: true,
output_format: "jpeg",
output_dimension: "2000x3000",
response_type: "url",
margins: {
minimum_margin: 8,
},
};
try {
// Send the POST request using fetch
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${API_KEY}`,
},
body: JSON.stringify(data),
});
// Check if the response is ok (status code 200-299)
if (!response.ok) {
const errorData = await response.json(); // Parse the error response
console.error('Error:', errorData); // Log the error
return;
}
const jsonResponse = await response.json(); // Parse the JSON response
console.log('Response:', jsonResponse); // Log the response
} catch (error) {
console.error('Error:', error); // Log any other errors
}
}
// Example usage of the function
const imageUrl = "https://images.pexels.com/photos/28836959/pexels-photo-28836959/free-photo-of-minimalist-overhead-italian-coffee-on-blue-surface.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2";
sendAutoResizeRequest(imageUrl);
Last modified: a month ago