How do I get metadata ( width and height) of a cloudinary resource in Django

jikdo
jikdo Member Posts: 4

I'm using cloudinaryfield to upload images and videos in my app. I need a way to get the width abd height of the images abd videos uploaded. I can't find any attribute on CloudinaryResource class that can help. And calling the api for the metadata makes my server slow and I get rate limited. Does cloudinaryfield store the metadata of width and height after upload and where can I find it?

Tagged:

Answers

  • Cloudinary_John_H
    Cloudinary_John_H Cloudinary Staff Posts: 47

    Hi there,

    A couple of options come to mind.

    • On upload, the height and width are returned as part of the response. Sample response:
    {
      "asset_id": "b5e6d2b39ba3e0869d67141ba7dba6cf",
      "public_id": "eneivicys42bq5f2jpn2",
      "api_key": "312924996162147",
      "version": 1570979139,
      "version_id": "98f52566f43d8e516a486958a45c1eb9",
      "signature": "abcdefghijklmnopqrstuvwxyz12345",
      "width": 1000,
      "height": 672,
      "format": "jpg",
      "resource_type": "image",
      "created_at": "2017-08-11T12:24:32Z",
      "tags": [],
      "pages": 1,
      "bytes": 350749,
      "type": "upload",
      "etag": "5297bd123ad4ddad723483c176e35f6e",
      "placeholder": false,
      "url": "http://res.cloudinary.com/demo/image/upload/v1570979139/eneivicys42bq5f2jpn2.jpg",
      "secure_url": "https://res.cloudinary.com/demo/image/upload/v1570979139/eneivicys42bq5f2jpn2.jpg",
      "access_mode": "public",
      "original_filename": "sample",
      "eager": [
        { "transformation": "c_pad,h_300,w_400",
          "width": 400,
          "height": 300,
          "url": "http://res.cloudinary.com/demo/image/upload/c_pad,h_300,w_400/v1570979139/eneivicys42bq5f2jpn2.jpg",
          "secure_url": "https://res.cloudinary.com/demo/image/upload/c_pad,h_300,w_400/v1570979139/eneivicys42bq5f2jpn2.jpg" },
        { "transformation": "c_crop,g_north,h_200,w_260",
          "width": 260,
          "height": 200,
          "url": "http://res.cloudinary.com/demo/image/upload/c_crop,g_north,h_200,w_260/v1570979139/eneivicys42bq5f2jpn2.jpg",
          "secure_url": "https://res.cloudinary.com/demo/image/upload/c_crop,g_north,h_200,w_260/v1570979139/eneivicys42bq5f2jpn2.jpg" }],
      "media_metadata": {
        "PerspectiveHorizontal": "0",
        "RedHue": "0",
        "Exposure": "0.0",
        ...
        ...
        ...
        "ExposureTime": "1/320" 
    


    • Alternatively, you can make an Admin API call to get the details of a single resource. In the response will be the height and width. The response is similar to what you would get after uploading. Covered in the docs here: https://cloudinary.com/documentation/admin_api#get_details_of_a_single_resource_by_public_id


  • jikdo
    jikdo Member Posts: 4

    Using djangorest where can I catch the upload response using cloudinaryfield

  • Wissam
    Wissam Member, Cloudinary Staff Posts: 103

    Hi @jikdo ,

    You can integrate Cloudinary's uploading capabilities into your forms and models using Cloudinary's helper classes. For example, you can define a model class with a CloudinaryField for image uploads. When a file is uploaded via this field, you can catch the upload response in your view or form handling the upload.

    Here's a sample Django model with a CloudinaryField:

    from cloudinary.models import CloudinaryField
    
    class MyModel(models.Model):
        image = CloudinaryField('image')
    

    In your view handling the form submission, you can catch the upload response like this:

    def upload(request):
        if request.method == 'POST':
            form = MyForm(request.POST, request.FILES)
            if form.is_valid():
                instance = form.save()
                # The upload response is now in instance.image
    

    Please refer to the Cloudinary Django documentation for more details.

    Best Regards,

    Wissam

  • jikdo
    jikdo Member Posts: 4
    edited January 9

    I'm using Django Rest, so I'm dealing with serializers and not forms. Can I get an example for using a Django Rest Serializer?


    serializer.save() or form.save() returns the model object instance, accessing the image attribute returns CloudinaryResource


    I try to access the metadata attribute it always returns None

  • jikdo
    jikdo Member Posts: 4
    edited January 9

    https://github.com/cloudinary/pycloudinary/blob/3a745bd0db98d4f4cef5463ea71af0f66e1edc63/cloudinary/models.py#L89

    This function that adds the metadata when creating a new CloudinaryResource in CloudinaryField,

    function is from_db_value it calls parse_cloudinary_resource , which gets it's information from the url, since the url doesn't have the metadata information , it never returns it.

  • Tom
    Tom Member, Cloudinary Staff Posts: 103

    Hi @jikdo ,

    Thanks for waiting.

    Can you please try this: https://github.com/cloudinary/pycloudinary/blob/3a745bd0db98d4f4cef5463ea71af0f66e1edc63/django_tests/test_cloudinaryField.py#L83

    i.e. when initializing CloudinaryField you can specify the names of width_field and height_field that will keep corresponding values.

    Hope this helps.

    Thomas