Exemplo n.º 1
0
    def _generate_srcset_attribute(self,
                                   breakpoints,
                                   transformation=None,
                                   **options):
        """
        Helper function. Generates srcset attribute value of the HTML img tag.

        :param breakpoints: A list of breakpoints.
        :param transformation: Custom transformation
        :param options:     Additional options

        :return:  Resulting srcset attribute value

        :raises ValueError: In case of invalid or missing parameters
        """
        if not breakpoints:
            return None

        if transformation is None:
            transformation = dict()

        return ", ".join([
            "{0} {1}w".format(
                utils.cloudinary_scaled_url(self.public_id, w, transformation,
                                            options), w) for w in breakpoints
        ])
Exemplo n.º 2
0
    def _fetch_breakpoints(self, srcset_data=None, **options):
        """
        Helper function. Retrieves responsive breakpoints list from cloudinary server

        When passing special string to transformation `width` parameter of form `auto:breakpoints{parameters}:json`,
        the response contains JSON with data of the responsive breakpoints

        :param srcset_data: A dictionary containing the following keys:
                                min_width   Minimal width of the srcset images
                                max_width   Maximal width of the srcset images
                                bytes_step  Minimal bytes step between images
                                max_images  Number of srcset images to generate
        :param options: Additional options

        :return: Resulting breakpoints
        """
        if srcset_data is None:
            srcset_data = dict()

        min_width = srcset_data.get("min_width", 50)
        max_width = srcset_data.get("max_width", 1000)
        bytes_step = srcset_data.get("bytes_step", 20000)
        max_images = srcset_data.get("max_images", 20)
        transformation = srcset_data.get("transformation")

        kbytes_step = int(ceil(float(bytes_step) / 1024))

        breakpoints_width_param = "auto:breakpoints_{min_width}_{max_width}_{kbytes_step}_{max_images}:json".format(
            min_width=min_width, max_width=max_width, kbytes_step=kbytes_step, max_images=max_images)
        breakpoints_url = utils.cloudinary_scaled_url(self.public_id, breakpoints_width_param, transformation, options)

        return _http_client.get_json(breakpoints_url).get("breakpoints", None)