Headless Umbraco - Crop Zoom
# help-with-umbraco
s
Is it possible to get the crop zoom when fetching images in a headless app (SvelteKit) from umbraco (v13)? Check the images for: 1. My crop settings 2. What the result looks like on the page 3. The helper that generates the rxy from the coordinates. So to summarize it looks like the x and y might be fine, but the zoom does not seem to tag along, or am I missing something here or am I doing it wrong? https://cdn.discordapp.com/attachments/1288857518520533076/1288857519200141355/image.png?ex=66f6b59a&is=66f5641a&hm=bc74561a53cad69695b0213df579259509a3352d1e872ce1cfb98fae242d5dcb& https://cdn.discordapp.com/attachments/1288857518520533076/1288857519921303612/image.png?ex=66f6b59a&is=66f5641a&hm=cd4f63c428142e584f7a57d69e68f949d627356526691b0e2448025e8ff1dec2& https://cdn.discordapp.com/attachments/1288857518520533076/1288857520219230359/image.png?ex=66f6b59a&is=66f5641a&hm=37bc40f46a229fe35872642cc28fbb8d5805cc639a0939fa60206f81d155d432&
k
Not sure how you serve your data in your api, but why aren't you simply using the GetCropUrl() to get the url right away, without the need to parse it on the front end, thus saving a back and forth?
s
Not sure what you mean, the images are from the official Content Delivery API, not an API I've built myself. If I want to get the cropUrl all images have to make an additional api call to fetch the crop url? What we basically do is we get the URL from the image picker etc. through the content api, which is then a relative path, so we append the umbraco URL to that url along with any crops / params we want to apply like quality etc. But for some reason the crop only works for width and height, and not for zooming etc.
b
I was looking into a very similar issue, and found that once you have an image cropped for the specific crop the URL you get back from GetCropURL actually changes to something like this:
/media/mnfnj2kd/test.jpg?cc=0,0.5783864731237323,0.0006197881451430714,0&width=1920&height=1080&v=1da516670ffb050
The values you would need for
cc
are what you get as a
coordinates
object in the content delivery, basically the coordinates of the cropped portion of the image. What you would give as
rxy
are just the focal point, which will point to the correctly selected focal point for the image but it will not know what portion is cut. So in short, you have 3 cases: - nothing is set (focal point or crop for the given crop alias): you can do what you have done and set the focal point to the middle - focal points is set: use the
rxy
version of the url, with height and width or any other options - coordinates are set for the given crop: use the
cc
version, with width, height or any other options I've also ran into another issue on a personal project, that cause issues with images, described here and the linked other post: https://our.umbraco.com/forum/using-umbraco-and-getting-started/113411-query-string-image-resize-not-working-on-umbraco-12 Hope this helps.
s
Thank you @Balázs Kerper , I'll give this a try asap 🙂
This worked great @Balázs Kerper, I've made a little helper that takes in the url and an optional desired crop, and handles it accordingly if anyone needs it 🙂
Copy code
function buildCropUrl (baseImageUrl: string, crop: Crop | undefined, imageWidth: number | null = 0) : string {
    const url = new URL(baseImageUrl);

    // Determine width and height values from imageWidth or crop dimensions.
    const width = imageWidth && imageWidth > 0 ? imageWidth : crop?.width;
    const height = crop?.height;

    // Initialize an array to hold custom query parameters.
    const customParams: string[] = [];

    // Handle cases for cropping and focal points.
    if (crop?.coordinates) {
        const { x1, y1, x2, y2 } = crop.coordinates;
        const cc = `${x1.toFixed(4)},${y1.toFixed(4)},${x2.toFixed(4)},${y2.toFixed(4)}`;
        customParams.push(`cc=${cc}`);
    }

    // Add width and height after the cc.
    if (width) customParams.push(`width=${width}`);
    if (height) customParams.push(`height=${height}`);

    // Construct the full URL with parameters in the correct order.
    const queryString = customParams.join('&');
    return `${url.origin}${url.pathname}?${queryString}`;
}
b
great, good to hear that it worked 🙂
32 Views