Image transformations in Supabase - Time & Space Complexity
When we transform images using Supabase, we want to know how the time it takes changes as we work with more images or bigger images.
We ask: How does the number of image transformations affect the total time?
Analyze the time complexity of the following operation sequence.
const { data, error } = await supabase.storage
.from('images')
.createSignedUrl('photo.jpg', 60);
const transformedUrl = supabase.storage
.from('images')
.getPublicUrl('photo.jpg?width=300&height=200&resize=contain');
// Use transformedUrl to display the resized image
This sequence creates a temporary URL for an image and then requests a resized version of that image with specific width and height.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Requesting image transformation via URL parameters.
- How many times: Once per image transformation request.
Each image transformation request triggers processing for that image size.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 images | 10 transformation requests |
| 100 images | 100 transformation requests |
| 1000 images | 1000 transformation requests |
Pattern observation: The number of transformation operations grows directly with the number of images processed.
Time Complexity: O(n)
This means the time to transform images grows linearly with the number of images you transform.
[X] Wrong: "Transforming multiple images at once takes the same time as transforming one image."
[OK] Correct: Each image transformation requires separate processing, so time adds up with more images.
Understanding how image transformations scale helps you design systems that handle many images smoothly and predict performance as demand grows.
"What if we cached transformed images instead of transforming on every request? How would the time complexity change?"
Practice
Solution
Step 1: Understand image transformation concept
Image transformations modify the appearance of images dynamically without changing the original stored file.Step 2: Identify the purpose in Supabase
Supabase uses transformations to resize, crop, or add effects on the fly, keeping the original image intact.Final Answer:
To change how images look without modifying the original file -> Option AQuick Check:
Image transformations = change appearance only [OK]
- Thinking transformations delete or overwrite original images
- Confusing transformations with file format conversion
- Assuming transformations speed up upload automatically
Solution
Step 1: Recall Supabase image resize syntax
Supabase uses the parameterwto specify width in pixels for resizing images.Step 2: Match correct parameter
The correct query parameter to resize width to 300 pixels is?w=300.Final Answer:
?w=300 -> Option DQuick Check:
Resize width = ?w=number [OK]
- Using ?width= or ?resize= which are invalid
- Confusing size with width parameter
- Omitting the question mark in URL
https://example.supabase.co/storage/v1/object/public/images/photo.jpg?w=200&h=100&fit=crop?Solution
Step 1: Analyze URL parameters
The URL includesw=200(width),h=100(height), andfit=cropwhich crops the image to fit exactly the given dimensions.Step 2: Understand transformation behavior
Supabase will resize the image to 200 pixels wide and 100 pixels tall, cropping parts if needed to maintain aspect ratio.Final Answer:
The image will be resized to 200x100 pixels and cropped to fit exactly -> Option AQuick Check:
Resize + crop = exact fit size [OK]
- Assuming fit=crop stretches image without cropping
- Ignoring height parameter h
- Thinking parameters cause errors
?w=abc in the URL but it did not work. What is the likely problem?Solution
Step 1: Check parameter value type
The width parameterwexpects a numeric value representing pixels, not letters.Step 2: Identify error cause
Usingabcas value is invalid and causes the resize to fail.Final Answer:
The width parameter must be a number, not letters -> Option BQuick Check:
Width value must be numeric [OK]
- Using text instead of numbers for size
- Changing parameter name incorrectly
- Assuming resizing needs extra fit parameter
Solution
Step 1: Set size and cropping parameters
Usew=150andh=150to set width and height, andfit=cropto crop the image to exact size centered.Step 2: Apply grayscale effect correctly
Supabase usesgrayscale=1to apply grayscale effect, nottrue.Final Answer:
?w=150&h=150&fit=crop&grayscale=1 -> Option CQuick Check:
Crop + grayscale=1 for exact grayscale thumbnail [OK]
- Using true instead of 1 for grayscale
- Using fit=contain instead of fit=crop
- Using wrong parameter names like width or effect
Ready to prove it?
Pass the test and this pattern moves unknown
