Delivering Remote Images
Astro Cloudinary provides two powerful methods for fetching, optimizing, and delivering remote images through Cloudinary’s transformation pipeline: the Fetch API and Auto Upload Mapping. These features are particularly useful when you want to fetch and optimize external images without explicitly storing them in your Cloudinary account.
Method 1: Using the Fetch API
The Fetch API allows you to deliver remote images on-the-fly through Cloudinary, applying transformations as needed. This method is ideal for dynamically accessing and transforming images from external sources.
Basic Usage
To fetch a remote image, you need to include the full URL of the image in the src
prop and set deliveryType="fetch"
.
This will result in a URL similar to:
Advanced Usage with Transformations
You can apply Cloudinary transformations to fetched images just like you would with uploaded images. This allows you to resize, crop, apply effects, and more to remote images on-the-fly.
This will result in a URL similar to:
How It Works
When you set deliveryType="fetch"
and provide a full URL as the src
prop, Astro Cloudinary constructs a special URL that tells Cloudinary to:
- Fetch the image from the specified URL
- Apply any specified transformations
- Deliver the optimized image
This process happens on-the-fly, allowing you to apply Cloudinary’s transformations and optimizations to any image on the web.
Security Considerations
To prevent abuse and ensure secure usage of the Fetch API:
-
Configure your Cloudinary account to only fetch images from trusted domains:
- Log in to your Cloudinary Console
- Navigate to Settings > Security
- In the “Allowed fetch domains” section, add the domains you want to allow
- Save your changes
-
Use signed URLs for added security:
- Enable signed URLs in your Cloudinary account settings
- Generate signed URLs using Cloudinary’s SDKs or API
-
Be cautious when fetching images from user-provided URLs to prevent potential security risks
Best Practices for Fetch API
-
Use
f_auto
andq_auto
: AddfetchFormat="auto"
andquality="auto"
to your image requests for optimal format and quality. -
Cache-Control: Cloudinary sets a Cache-Control header of 1 year by default for fetched images. Adjust this using the
cacheControl
option if needed. -
Error Handling: Cloudinary returns a placeholder image if the remote image can’t be fetched. Customize this behavior using the
defaultImage
option. -
URL Encoding: Ensure that special characters in remote URLs are properly encoded to avoid issues.
-
Responsive Images: Use the
sizes
prop along withwidth
andheight
to create responsive images that adapt to different screen sizes.
By leveraging the Fetch API, you can easily optimize and transform remote images without the need to upload them to your Cloudinary account, providing flexibility and efficiency in your image delivery workflow.
Method 2: Auto Upload Mapping
Auto Upload Mapping allows you to automatically upload remote images to your Cloudinary account when they’re first accessed, making them available for future use with all of Cloudinary’s features. This method is particularly useful when you have a large number of images hosted on another server that you want to gradually migrate to Cloudinary.
How It Works
When you access an image using the configured folder, Cloudinary will:
- Check if the image exists in your account
- If not, it will fetch the image from the mapped URL
- Upload the image to your Cloudinary account
- Apply any specified transformations
- Deliver the optimized image
This process happens only on the first request. Subsequent requests will use the uploaded image directly from your Cloudinary account, providing faster delivery and full access to Cloudinary’s transformation capabilities.
Configuration
To set up Auto Upload Mapping:
- Log in to your Cloudinary Console
- Navigate to Settings > Upload
- Find the “Auto upload mapping” section
- Click “Add mapping”
- Set up your folder and URL prefix:
- Folder: e.g., “remote_images” (this will be the folder in your Cloudinary account)
- URL prefix: The base URL of your remote images (e.g., “https://example.com/images/”)
- Click “Save”
Here’s a video tutorial demonstrating the process:
Usage
Once configured, you can use the images as if they were already in your Cloudinary account:
Benefits of Auto Upload Mapping
- Gradual Migration: Easily move your images to Cloudinary without the need for a bulk upload.
- Automatic Optimization: Images are automatically optimized when they’re first accessed.
- Seamless Integration: Use your existing URL structure while leveraging Cloudinary’s features.
- On-Demand Processing: Images are only uploaded when they’re actually needed, saving storage space.
Best Practices for Auto Upload Mapping
- Folder Structure: Choose a logical folder structure that mirrors your existing setup for easy management.
- Backup: Ensure you maintain a backup of your original images, even though Cloudinary will store a copy.
- Monitoring: Keep an eye on your Cloudinary usage to manage costs as images are automatically uploaded.
- Security: Only map trusted sources to prevent unauthorized uploads to your account.
Choosing Between Fetch API and Auto Upload Mapping
-
Use Fetch API when:
- You need to dynamically access and transform images.
- You want to optimize third-party images that you don’t own or control.
- You need to apply transformations to images on-the-fly without permanent storage.
-
Use Auto Upload Mapping when:
- You want to gradually migrate a large number of images to Cloudinary.
- You need to maintain your existing URL structure while using Cloudinary.
- You want to automatically optimize and store remote images in your Cloudinary account.
Both methods offer powerful ways to work with remote images, allowing you to leverage Cloudinary’s extensive transformation and optimization capabilities.