### Start Cloudex Application Source: https://github.com/smeevil/cloudex/blob/master/README.md This code demonstrates how to ensure the Cloudex application is started when your Elixir application boots. It's added to the `applications` list within the `application` function in `mix.exs`. ```elixir def application do [applications: [:logger, :cloudex], ] end ``` -------------------------------- ### Start Cloudex.Settings GenServer with settings in Elixir Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.Settings.html Starts the Cloudex.Settings GenServer with provided settings. This function directly initializes the GenServer with custom Cloudinary credentials passed as a parameter. ```elixir Cloudex.Settings.start(settings) ``` -------------------------------- ### Start Cloudex.Settings GenServer from supervisor in Elixir Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.Settings.html Called by the supervisor to start the GenServer using settings defined in config.exs or environment variables. Accepts an atom and list as parameters for supervisor-based initialization. ```elixir Cloudex.Settings.start(atom, list) ``` -------------------------------- ### Uploaded Image Struct Example Source: https://github.com/smeevil/cloudex/blob/master/README.md This is an example of the `%Cloudex.UploadedImage{}` struct, which is returned upon a successful image upload. It contains details such as image dimensions, format, URLs, public ID, and more. ```elixir %Cloudex.UploadedImage{ bytes: 22659, created_at: "2015-11-27T10:02:23Z", etag: "dbb5764565c1b77ff049d20fcfd1d41d", format: "jpg", height: 167, original_filename: "test", public_id: "i2nruesgu4om3w9mtk1z", resource_type: "image", secure_url: "https://d1vibqt9pdnk2f.cloudfront.net/image/upload/v1448618543/i2nruesgu4om3w9mtk1z.jpg", signature: "77b447746476c82bb4921fdea62a9227c584974b", source: "http://example.org/test.jpg", tags: [], type: "upload", url: "http://images.cloudassets.mobi/image/upload/v1448618543/i2nruesgu4om3w9mtk1z.jpg", version: 1448618543, width: 250 } ``` -------------------------------- ### Get child specification for Cloudex.Settings supervisor in Elixir Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.Settings.html Returns a specification to start the Cloudex.Settings module under a supervisor. Complies with the Supervisor behavior specification for proper supervision tree integration. ```elixir Cloudex.Settings.child_spec(init_arg) ``` -------------------------------- ### Initialize GenServer for Cloudex.Settings in Elixir Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.Settings.html Required callback function by GenServer behavior that initializes the Cloudex.Settings module with provided arguments. Handles the initial setup of the GenServer state. ```elixir Cloudex.Settings.init(args) ``` -------------------------------- ### Start Cloudinary GenServer (Elixir) Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.html Manually starts the GenServer responsible for managing Cloudinary API settings. This function is used to initialize the Cloudex service with necessary credentials before performing operations. It requires a map containing API key, secret, and cloud name. ```elixir start(settings) # Example usage: start(%{api_key: "your_api_key", secret: "your_secret", cloud_name: "your_cloud_name"}) ``` -------------------------------- ### Restart Cloudex.Settings GenServer with new settings in Elixir Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.Settings.html Helper function to start or restart the GenServer when already started with given settings. Handles error scenarios during GenServer initialization or restart. ```elixir Cloudex.Settings.do_start(error) ``` -------------------------------- ### Upload Image with Tags (Array) Source: https://github.com/smeevil/cloudex/blob/master/README.md This example demonstrates how to upload an image and assign tags to it using a list of strings. The tags are passed as an option within a map argument to the `Cloudex.upload/2` function. ```elixir # as array Cloudex.upload("test/assets/test.jpg", %{tags: ["foo", "bar"]}) # as comma-separated string Cloudex.upload("test/assets/test.jpg", %{tags: "foo,bar"}) # result {:ok, %Cloudex.UploadedImage{ bytes: 22659, created_at: "2015-11-27T10:02:23Z", etag: "dbb5764565c1b77ff049d20fcfd1d41d", format: "jpg", height: 167, original_filename: "test", public_id: "i2nruesgu4om3w9mtk1z", resource_type: "image", secure_url: "https://d1vibqt9pdnk2f.cloudfront.net/image/upload/v1448618543/i2nruesgu4om3w9mtk1z.jpg", signature: "77b447746476c82bb4921fdea62a9227c584974b", source: "http://example.org/test.jpg", tags: ["foo", "bar"], type: "upload", url: "http://images.cloudassets.mobi/image/upload/v1448618543/i2nruesgu4om3w9mtk1z.jpg", version: 1448618543, width: 250 }} ``` -------------------------------- ### Configure Cloudex with Cloudinary Credentials Source: https://context7.com/smeevil/cloudex/llms.txt Sets up Cloudex library with required Cloudinary authentication credentials and optional configuration. Supports both static configuration and environment variables for API key, secret, and cloud name. Includes dependency setup for Elixir projects. ```elixir config :cloudex, api_key: "your_api_key", secret: "your_secret_key", cloud_name: "your_cloud_name" config :cloudex, :json_library, Jason def deps do [ {:cloudex, "~> 1.4.0"} ] end def application do [applications: [:logger, :cloudex]] end ``` -------------------------------- ### Upload List of Images (Files or URLs) Source: https://github.com/smeevil/cloudex/blob/master/README.md This example shows how to upload multiple image sources (a mix of local file paths and URLs) in a single call. The function returns a list of results, where each element is either an `:ok` tuple with uploaded image details or an error tuple. ```elixir iex> Cloudex.upload(["/non/existing/file.jpg", "http://example.org/test.jpg"]) [{:error, "File /non/existing/file.jpg does not exist."}, {:ok, %Cloudex.UploadedImage{...}}] ``` -------------------------------- ### Configure Custom JSON Library Source: https://github.com/smeevil/cloudex/blob/master/README.md This configuration example shows how to instruct Cloudex to use a different JSON library than the default (Jason). Replace `YourLibraryOfChoice` with the actual module name of your preferred JSON library. ```elixir config :cloudex, :json_library, YourLibraryOfChoice ``` -------------------------------- ### Upload Image by URL Source: https://github.com/smeevil/cloudex/blob/master/README.md This example demonstrates how to upload an image to Cloudinary using its URL. The `Cloudex.upload/1` function takes the URL as an argument and returns an `:ok` tuple with the uploaded image details or an error tuple. ```elixir iex> Cloudex.upload("http://example.org/test.jpg") {:ok, %Cloudex.UploadedImage{...}} ``` -------------------------------- ### Generate Cloudinary URL with crop and zoom in Elixir Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.Url.html Generate a Cloudinary image URL with crop and zoom transformations applied. This example demonstrates using the Cloudex.Url.for/2 function with parameters for cropping to face region and zooming at 1.3x scale. The function accepts a public ID string and a map of transformation options, returning a formatted Cloudinary URL string. ```elixir Cloudex.Url.for("a_public_id", %{zoom: 1.3, face: true, crop: "crop", version: 1471959066}) "//res.cloudinary.com/my_cloud_name/image/upload/c_crop,g_face,z_1.3/v1471959066/a_public_id" ``` -------------------------------- ### Generate Cloudinary URL with aspect ratio in Elixir Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.Url.html Generate a Cloudinary image URL that retains a specific aspect ratio while specifying width and height dimensions. This example uses Cloudex.Url.for/2 with aspect ratio, width, height, and version parameters to create a responsive image URL. The resulting URL maintains the 2.5:1 aspect ratio with the specified dimensions. ```elixir Cloudex.Url.for("a_public_id", %{aspect_ratio: 2.5, width: 400, height: 300, version: 1471959066}) "//res.cloudinary.com/my_cloud_name/image/upload/ar_2.5,h_300,w_400/v1471959066/a_public_id" ``` -------------------------------- ### Get Cloudinary credentials as map in Elixir Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.Settings.html Retrieves all stored Cloudinary credentials as a map containing api_key, secret, and cloud_name. This function queries the GenServer to fetch the complete credential set without parameters. ```elixir iex> Cloudex.Settings.get %{api_key: "my_key", secret: "my_secret", cloud_name: "my_cloud_name"} ``` -------------------------------- ### Get specific Cloudinary credential by key in Elixir Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.Settings.html Retrieves a specific Cloudinary credential value by providing a key atom such as :secret, :api_key, or :cloud_name. Returns nil if the key does not exist in the credentials map. ```elixir iex> Cloudex.Settings.get(:secret) "my_secret" iex> Cloudex.Settings.get(:bogus) nil ``` -------------------------------- ### Upload File or URL to Cloudinary Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.CloudinaryApi.html Handles the upload of either a file path or a URL to Cloudinary. The `opts` map can specify `resource_type` (e.g., 'video') for non-image uploads. On success, it returns {:ok, %UploadedFile{}}, containing detailed information about the uploaded asset. On failure, it returns {:error, 'reason'}. ```elixir upload(item, opts \\ %{}) ``` -------------------------------- ### Upload File or URL to Cloudinary (Elixir) Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.CloudinaryApi.html This function facilitates the upload of a file or a URL to Cloudinary. Optional parameters can be provided via the 'opts' map, such as specifying 'resource_type' for video uploads. The function returns a tuple containing either {:ok, %UploadedFile{}} with Cloudinary's response data or {:error, "reason"} if the upload fails. ```elixir def upload(item, opts \\ %{}) :: {:ok, [%Cloudex.UploadedImage.t()]} | {:error, any()} when is_list(item) or item in [String.t(), {:ok, [String.t()]}]. when is_map(opts) url(resource_type) :: {:ok, String.t()} | {:error, any()} when is_atom(resource_type) or is_binary(resource_type) ``` -------------------------------- ### POST /cloudinary/upload Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.CloudinaryApi.html Uploads a file or URL to Cloudinary. Supports resource type specification for videos. ```APIDOC ## POST /cloudinary/upload ### Description Uploads either a file or url to cloudinary. The `opts` parameter can specify `resource_type: "video"` to enable video uploads. ### Method POST ### Endpoint /cloudinary/upload ### Parameters #### Query Parameters - **item** (string) - Required - The file path or URL to upload. - **opts** (map) - Optional - A map for additional options, e.g., `%{resource_type: "video"}`. ### Request Body (Not applicable for this endpoint, parameters are passed via query) ### Request Example (Example would depend on how the client is making the request, typically form-data for files or JSON for URLs) ### Response #### Success Response (200) - **ok** (tuple) - Returns `{:ok, %Cloudex.UploadedFile{}}` containing Cloudinary's response data upon successful upload. #### Error Response - **error** (tuple) - Returns `{:error, "reason"}` if the upload fails. ``` -------------------------------- ### Generate URL with Overlays - Elixir Source: https://context7.com/smeevil/cloudex/llms.txt Demonstrates how to add image overlays, such as watermarks or badges, to Cloudinary URLs. This involves specifying the overlay asset, its transformations, positioning, and placement on the base image. ```elixir # Add overlay with positioning url = Cloudex.Url.for("base_image", [ %{crop: "fill", width: 470, height: 246, quality: 80, radius: 5, border: "5px_solid_rgb:c22c33"}, %{overlay: "logo_watermark", crop: "scale", gravity: "south_east", width: 128, x: 5, y: 15} ]) # => "//res.cloudinary.com/my_cloud_name/image/upload/bo_5px_solid_rgb:c22c33,c_fill,h_246,q_80,r_5,w_470/c_scale,g_south_east,l_logo_watermark,w_128,x_5,y_15/base_image" # Multiple transformation layers url = Cloudex.Url.for("product_photo", [ %{width: 800, height: 600, crop: "fill"}, %{overlay: "sale_badge", gravity: "north_west", x: 10, y: 10, width: 100} ]) ``` -------------------------------- ### Add Cloudex Dependency to mix.exs Source: https://github.com/smeevil/cloudex/blob/master/README.md This snippet shows how to add the Cloudex library as a project dependency in the `mix.exs` file. This is typically done within the `deps` function. ```elixir defp deps do [ {:cloudex, "~> 1.3.0"}, ] end ``` -------------------------------- ### Phoenix Image Tag Helper with Cloudex Elixir Source: https://github.com/smeevil/cloudex/blob/master/README.md Create a Phoenix helper function `cl_image_tag` to easily render images with Cloudex transformations. This helper generates an `` tag with the correct Cloudinary URL and allows for custom options, including transformation parameters. ```elixir defmodule MyApp.CloudexImageHelper do import Phoenix.HTML.Tag def cl_image_tag(public_id, options \ []) do transformation_options = %{} if Keyword.has_key?(options, :transforms) do transformation_options = Map.merge(%{}, options[:transforms]) end image_tag_options = Keyword.delete(options, :transforms) defaults = [ src: Cloudex.Url.for(public_id, transformation_options), width: picture.width, height: picture.height, alt: "image with name #{public_id}" ] attributes = Keyword.merge(defaults, image_tag_options) tag(:img, attributes) end end ``` ```elixir cl_image_tag(public_id, class: "thumbnail", transforms: %{opacity: "50", quality: "jpegmini", sign_url: true}) ``` -------------------------------- ### Upload Image with Perceptual Hash to Cloudinary (Elixir) Source: https://context7.com/smeevil/cloudex/llms.txt Uploads an image to Cloudinary and enables perceptual hash generation for duplicate detection. The 'phash' option is set to 'true'. The resulting %Cloudex.UploadedImage{} struct includes the generated perceptual hash. ```elixir # Enable phash for duplicate detection {:ok, image} = Cloudex.upload("path/to/image.jpg", %{phash: "true"}) # Result includes perceptual hash %Cloudex.UploadedImage{ public_id: "image_123", phash: "d3a4c1b2e5f6789a", url: "http://res.cloudinary.com/demo/image/upload/v1234567890/image_123.jpg" } # Use phash to detect similar images across your application ``` -------------------------------- ### Initialize Night Mode from Local Storage Source: https://github.com/smeevil/cloudex/blob/master/doc/404.html This JavaScript snippet checks if 'night-mode' is set in local storage and applies the 'night-mode' class to the body if it exists. It includes basic error handling for localStorage access. ```javascript try { if(localStorage.getItem('night-mode')) document.body.className += ' night-mode'; } catch (e) { } ``` -------------------------------- ### Upload Local Image File to Cloudinary (Elixir) Source: https://context7.com/smeevil/cloudex/llms.txt Uploads a local image file to Cloudinary using the Cloudex library. It returns an {:ok, uploaded_image} tuple on success or an {:error, reason} tuple on failure. The uploaded_image is a %Cloudex.UploadedImage{} struct containing metadata about the resource. ```elixir # Upload a local file {:ok, uploaded_image} = Cloudex.upload("path/to/image.jpg") # Result structure %Cloudex.UploadedImage{ bytes: 22659, created_at: "2015-11-27T10:02:23Z", etag: "dbb5764565c1b77ff049d20fcfd1d41d", format: "jpg", height: 167, original_filename: "test", public_id: "i2nruesgu4om3w9mtk1z", resource_type: "image", secure_url: "https://res.cloudinary.com/demo/image/upload/v1448618543/i2nruesgu4om3w9mtk1z.jpg", signature: "77b447746476c82bb4921fdea62a9227c584974b", source: "path/to/image.jpg", tags: [], type: "upload", url: "http://res.cloudinary.com/demo/image/upload/v1448618543/i2nruesgu4om3w9mtk1z.jpg", version: 1448618543, width: 250 } # Handle errors case Cloudex.upload("/non/existing/file.jpg") do {:ok, image} -> IO.puts("Uploaded: #{image.public_id}") {:error, reason} -> IO.puts("Error: #{reason}") end # => {:error, "File /non/existing/file.jpg does not exist."} ``` -------------------------------- ### Generate Signed URL - Elixir Source: https://context7.com/smeevil/cloudex/llms.txt Creates signed URLs for Cloudinary resources to ensure secure access and prevent unauthorized transformations. Signatures are generated based on transformations and resource details. ```elixir # Generate URL with signature for security url = Cloudex.Url.for("my_image", %{ width: 400, height: 300, sign_url: true }) # => "//res.cloudinary.com/my_cloud_name/image/upload/s--jwB_Ds4w--/h_300,w_400/my_image" # Signed URL with multiple transformations url = Cloudex.Url.for("secure_image", %{ crop: "fill", quality: 80, width: 500, height: 400, sign_url: true }) # => "//res.cloudinary.com/my_cloud_name/image/upload/s--signature--/c_fill,h_400,q_80,w_500/secure_image" ``` -------------------------------- ### Batch Upload Multiple Files/URLs to Cloudinary (Elixir) Source: https://context7.com/smeevil/cloudex/llms.txt Uploads multiple local files or URLs to Cloudinary in a single operation, utilizing parallel processing for efficiency. The function accepts a list of file paths or URLs and returns a list of results, where each result is either an {:ok, uploaded_resource} or {:error, reason}. ```elixir # Upload multiple files at once results = Cloudex.upload([ "path/to/image1.jpg", "path/to/image2.jpg", "https://example.com/image3.jpg" ]) ``` -------------------------------- ### Upload Image with Cloudinary API (Elixir) Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.CloudinaryApi.Live.html Handles the uploading of an image to Cloudinary. It accepts the image item (as a string or an {:ok, String.t()} tuple) and optional configuration options in a map. Upon successful upload, it returns a %Cloudex.UploadedImage{} struct containing details about the image. It also catches errors if the upload is called without a valid string argument. ```elixir upload(item :: String.t(), opts \\ %{}) :: %Cloudex.UploadedImage{} upload({:ok, String.t()}, opts \\ %{}) :: %Cloudex.UploadedImage{} upload(any, opts :: map) :: {:error, [String.t()]} ``` -------------------------------- ### Initialize Cloudex.DeletedImage Struct Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.DeletedImage.html Demonstrates the creation of a Cloudex.DeletedImage struct, which is designed to hold the public identifier of a deleted image response. This struct is part of the cloudex library. ```elixir iex(1)> %Cloudex.DeletedImage{public_id: "some_id"} %Cloudex.DeletedImage{public_id: "some_id"} ``` -------------------------------- ### Configure Cloudex API Credentials Source: https://github.com/smeevil/cloudex/blob/master/README.md This snippet illustrates two ways to configure Cloudinary API credentials for Cloudex: using environment variables or directly in `config.exs`. It sets the `api_key`, `secret`, and `cloud_name` for the Cloudex application. ```elixir config :cloudex, api_key: "my-api-key", secret: "my-secret", cloud_name: "my-cloud-name" ``` -------------------------------- ### Merge Environment Variables with a Map Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.EnvOptions.html The `merge` function takes a map and merges system environment variables into it. It uses the keys of the input map to match with system variables. The output is the updated map, potentially including values from environment variables. ```Elixir Cloudex.EnvOptions.merge %{my_env_key: nil} > %{my_env_key: "value from env"} ``` -------------------------------- ### Generate URL with Named Transformation - Elixir Source: https://context7.com/smeevil/cloudex/llms.txt Applies predefined transformations configured in the Cloudinary account to generated URLs. This simplifies complex transformation chains and promotes consistency across assets. ```elixir # Apply named transformation url = Cloudex.Url.for("my_image", %{transformation: "thumbnail_preset"}) # => "//res.cloudinary.com/my_cloud_name/image/upload/t_thumbnail_preset/my_image" # Named transformation with resource type url = Cloudex.Url.for("my_video", %{ resource_type: "video", transformation: "hd_optimized" }) # => "//res.cloudinary.com/my_cloud_name/video/upload/t_hd_optimized/my_video" ``` -------------------------------- ### Generate URL with Face Detection - Elixir Source: https://context7.com/smeevil/cloudex/llms.txt Generates Cloudinary URLs that utilize face detection for intelligent cropping and focusing on faces within an image. This is useful for ensuring portraits or group photos are framed correctly. ```elixir # Crop to face with zoom url = Cloudex.Url.for("portrait_photo", %{ width: 400, height: 300, face: true, zoom: 1.3, crop: "crop" }) # => "//res.cloudinary.com/my_cloud_name/image/upload/c_crop,g_face,h_300,w_400,z_1.3/portrait_photo" # Simple face-focused crop url = Cloudex.Url.for("group_photo", %{ width: 200, height: 200, face: true }) # => "//res.cloudinary.com/my_cloud_name/image/upload/g_face,h_200,w_200/group_photo" ``` -------------------------------- ### Generate Basic Image URL - Elixir Source: https://context7.com/smeevil/cloudex/llms.txt Generates basic Cloudinary URLs for uploaded images or videos using their public ID. It supports specifying the resource type and desired format for the output URL. ```elixir # Basic URL without transformations url = Cloudex.Url.for("my_image_id") # => "//res.cloudinary.com/my_cloud_name/image/upload/my_image_id" # URL for video resource video_url = Cloudex.Url.for("my_video_id", %{resource_type: "video"}) # => "//res.cloudinary.com/my_cloud_name/video/upload/my_video_id" # URL with specific format png_url = Cloudex.Url.for("my_image_id", %{format: "png"}) # => "//res.cloudinary.com/my_cloud_name/image/upload/my_image_id.png" ``` -------------------------------- ### Convert Cloudinary JSON to UploadedImage Struct (Elixir) Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.CloudinaryApi.html This function takes the JSON result from a Cloudinary upload and converts it into a Cloudex.UploadedImage struct. It accepts a string or a tuple containing a string and options, and returns either {:ok, %UploadedImage{}} on success or {:error, reason} on failure. The 'opts' map can include :resource_type to specify video uploads. ```elixir def upload(item, opts \\ %{}) do # Function implementation details would go here end ``` -------------------------------- ### Upload Image with Tags to Cloudinary (Elixir) Source: https://context7.com/smeevil/cloudex/llms.txt Uploads an image to Cloudinary and assigns tags for organization. Tags can be provided as a list of strings or a comma-separated string. The resulting %Cloudex.UploadedImage{} struct will contain the assigned tags. ```elixir # Upload with tags as an array {:ok, tagged_image} = Cloudex.upload("path/to/image.jpg", %{tags: ["landscape", "nature"]}) # Upload with tags as comma-separated string {:ok, tagged_image} = Cloudex.upload("path/to/image.jpg", %{tags: "landscape,nature"}) # Result includes tags %Cloudex.UploadedImage{ public_id: "xyz789", tags: ["landscape", "nature"], url: "http://res.cloudinary.com/demo/image/upload/v1234567890/xyz789.jpg" } ``` -------------------------------- ### Upload Image File Source: https://github.com/smeevil/cloudex/blob/master/README.md This code snippet shows how to upload a local image file to Cloudinary using its file path. The function returns an `:ok` tuple containing the uploaded image information or an error tuple. ```elixir iex> Cloudex.upload("test/assets/test.jpg") {:ok, %Cloudex.UploadedImage{...}} ``` -------------------------------- ### Generate URL with Transformations - Elixir Source: https://context7.com/smeevil/cloudex/llms.txt Applies various image transformations, such as resizing, cropping, optimization, effects, and radius adjustments, to Cloudinary URLs. These transformations allow for dynamic image manipulation directly within the URL. ```elixir # Resize image to specific dimensions url = Cloudex.Url.for("my_image", %{width: 400, height: 300}) # => "//res.cloudinary.com/my_cloud_name/image/upload/h_300,w_400/my_image" # Crop and optimize image url = Cloudex.Url.for("my_image", %{ crop: "fill", width: 300, height: 254, quality: "jpegmini", fetch_format: "auto", flags: "progressive" }) # => "//res.cloudinary.com/my_cloud_name/image/upload/c_fill,f_auto,fl_progressive,h_254,q_jpegmini,w_300/my_image" # Apply effects and radius url = Cloudex.Url.for("portrait", %{ width: 200, height: 200, crop: "fill", radius: "max", effect: "sepia", gravity: "face" }) # => "//res.cloudinary.com/my_cloud_name/image/upload/c_fill,e_sepia,g_face,h_200,r_max,w_200/portrait" ``` -------------------------------- ### Night Mode Toggle with localStorage in JavaScript Source: https://github.com/smeevil/cloudex/blob/master/doc/api-reference.html Client-side script that checks localStorage for a 'night-mode' preference and applies the corresponding CSS class to the document body. Wrapped in a try-catch block to handle potential localStorage access errors in restricted environments. ```javascript try { if(localStorage.getItem('night-mode')) document.body.className += ' night-mode'; } catch (e) { } ``` -------------------------------- ### Upload Image from URL to Cloudinary (Elixir) Source: https://context7.com/smeevil/cloudex/llms.txt Uploads an image to Cloudinary directly from a URL (HTTP, HTTPS, or S3) without requiring a local download. The function returns a %Cloudex.UploadedImage{} struct with asset metadata, similar to local file uploads. ```elixir # Upload from HTTP/HTTPS URL {:ok, uploaded_image} = Cloudex.upload("https://example.com/photo.jpg") # Upload from S3 URL (for private buckets) {:ok, uploaded_image} = Cloudex.upload("s3://my-bucket/folder/image.jpg") # Both return the same UploadedImage struct with metadata %Cloudex.UploadedImage{ public_id: "abc123xyz", url: "http://res.cloudinary.com/demo/image/upload/v1234567890/abc123xyz.jpg", secure_url: "https://res.cloudinary.com/demo/image/upload/v1234567890/abc123xyz.jpg", width: 800, height: 600, format: "jpg" } ``` -------------------------------- ### Generate Cloudinary Image URL with Transformations (Elixir) Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.Url.html Generates a Cloudinary image URL for a given public ID. It accepts an optional map of options to apply various transformations like resizing, cropping, format changes, and signatures. This function is crucial for dynamic image serving and optimization. ```elixir iex> Cloudex.Url.for("a_public_id") "//res.cloudinary.com/my_cloud_name/image/upload/a_public_id" iex> Cloudex.Url.for("a_public_id", %{sign_url: true}) "//res.cloudinary.com/my_cloud_name/image/upload/s--MXxhpIBQ--/a_public_id" iex> Cloudex.Url.for("a_public_id", %{width: 400, height: 300}) "//res.cloudinary.com/my_cloud_name/image/upload/h_300,w_400/a_public_id" iex> Cloudex.Url.for("a_public_id", %{crop: "fill", fetch_format: 'auto', flags: 'progressive', width: 300, height: 254, quality: "jpegmini", sign_url: true}) "//res.cloudinary.com/my_cloud_name/image/upload/s--jwB_Ds4w--/c_fill,f_auto,fl_progressive,h_254,q_jpegmini,w_300/a_public_id" iex> Cloudex.Url.for("a_public_id", %{transformation: "my_transformation"}) "//res.cloudinary.com/my_cloud_name/image/upload/t_my_transformation/a_public_id" iex> Cloudex.Url.for("a_public_id", %{version: 1471959066}) "//res.cloudinary.com/my_cloud_name/image/upload/v1471959066/a_public_id" iex> Cloudex.Url.for("a_public_id", %{width: 400, height: 300, version: 1471959066}) "//res.cloudinary.com/my_cloud_name/image/upload/h_300,w_400/v1471959066/a_public_id" iex> Cloudex.Url.for("a_public_id", %{format: "png"}) "//res.cloudinary.com/my_cloud_name/image/upload/a_public_id.png" iex> Cloudex.Url.for("a_public_id", %{resource_type: "video"}) "//res.cloudinary.com/my_cloud_name/video/upload/a_public_id" iex> Cloudex.Url.for("a_public_id", %{resource_type: "video", transformation: "my_transformation"}) "//res.cloudinary.com/my_cloud_name/video/upload/t_my_transformation/a_public_id" iex> Cloudex.Url.for("a_public_id", [ ...> %{border: "5px_solid_rgb:c22c33", radius: 5, crop: "fill", height: 246, width: 470, quality: 80}, ...> %{overlay: "my_overlay", crop: "scale", gravity: "south_east", width: 128 ,x: 5, y: 15} ...> ]) "//res.cloudinary.com/my_cloud_name/image/upload/bo_5px_solid_rgb:c22c33,c_fill,h_246,q_80,r_5,w_470/c_scale,g_south_east,l_my_overlay,w_128,x_5,y_15/a_public_id" iex> Cloudex.Url.for("a_public_id", %{width: 400, height: 300, face: true}) "//res.cloudinary.com/my_cloud_name/image/upload/g_face,h_300,w_400/a_public_id" iex> Cloudex.Url.for("a_public_id", %{resource_type: "video", video_codec: "h265"}) "//res.cloudinary.com/my_cloud_name/video/upload/vc_h265/a_public_id" ``` -------------------------------- ### Upload Video File to Cloudinary (Elixir) Source: https://context7.com/smeevil/cloudex/llms.txt Uploads a video file to Cloudinary by specifying the `resource_type` option as 'video'. The function returns a %Cloudex.UploadedImage{} struct containing video-specific metadata along with standard upload details. ```elixir # Upload a video with resource_type option {:ok, uploaded_video} = Cloudex.upload("path/to/video.mp4", %{resource_type: "video"}) # Result includes video-specific metadata %Cloudex.UploadedImage{ public_id: "video_abc123", resource_type: "video", format: "mp4", url: "http://res.cloudinary.com/demo/video/upload/v1234567890/video_abc123.mp4", secure_url: "https://res.cloudinary.com/demo/video/upload/v1234567890/video_abc123.mp4", bytes: 5242880 } ``` -------------------------------- ### Handle Image Upload Results Source: https://context7.com/smeevil/cloudex/llms.txt Demonstrates how to process the results of an image upload operation, which can include successful uploads with public IDs or errors with descriptive messages. This function is useful for batch operations where some files might fail to upload. ```elixir # Returns list of results (success or error for each) [ {:ok, %Cloudex.UploadedImage{public_id: "img1"}}, {:ok, %Cloudex.UploadedImage{public_id: "img2"}}, {:ok, %Cloudex.UploadedImage{public_id: "img3"}} ] # Mixed results with errors results = Cloudex.upload([ "existing_file.jpg", "/non/existing/file.jpg", "https://example.com/valid.jpg" ]) [ {:ok, %Cloudex.UploadedImage{public_id: "abc"}}, {:error, "File /non/existing/file.jpg does not exist."}, {:ok, %Cloudex.UploadedImage{public_id: "xyz"}} ] ``` -------------------------------- ### Generate Cloudinary Image URLs with Cloudex Elixir Source: https://github.com/smeevil/cloudex/blob/master/README.md Generate Cloudinary image URLs using the Cloudex.Url.for/1 and Cloudex.Url.for/2 functions. These functions accept a public ID and optional transformation options, returning the corresponding image URL. Transformations can include width, height, crop, format, quality, and more. ```elixir Cloudex.Url.for("a_public_id") "//res.cloudinary.com/my_cloud_name/image/upload/a_public_id" ``` ```elixir Cloudex.Url.for("a_public_id", %{width: 400, height: 300}) "//res.cloudinary.com/my_cloud_name/image/upload/h_300,w_400/a_public_id" ``` ```elixir Cloudex.Url.for("a_public_id", %{crop: "fill", fetch_format: 'auto', flags: 'progressive', width: 300, height: 254, quality: "jpegmini", sign_url: true}) "//res.cloudinary.com/my_cloud_name/image/upload/s--jwB_Ds4w--/c_fill,f_auto,fl_progressive,h_254,q_jpegmini,w_300/a_public_id" ``` ```elixir Cloudex.Url.for("a_public_id", [ %{border: "5px_solid_rgb:c22c33", radius: 5, crop: "fill", height: 246, width: 470, quality: 80}, %{overlay: "my_overlay", crop: "scale", gravity: "south_east", width: 128 ,x: 5, y: 15} ]) "//res.cloudinary.com/my_cloud_name/image/upload/bo_5px_solid_rgb:c22c33,c_fill,h_246,q_80,r_5,w_470/c_scale,g_south_east,l_my_overlay,w_128,x_5,y_15/a_public_id" ``` -------------------------------- ### Delete Images by Prefix Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.CloudinaryApi.html Deletes multiple images from Cloudinary that share a common prefix in their public IDs. The function returns {:ok, [String.t]} with a list of deleted public IDs on success, or an {:error, reason} tuple upon failure. It takes the prefix string and optional Cloudinary options as arguments. ```elixir delete_prefix(prefix, opts \\ %{}) :: {:ok, [String.t]} | {:error, any} ``` -------------------------------- ### Enable Night Mode with JavaScript Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.DeletedImage.html This JavaScript snippet checks for a 'night-mode' item in localStorage and applies the 'night-mode' class to the document body if found. This is often used for theme toggling. ```javascript try { if (localStorage.getItem('night-mode')) { document.body.className += ' night-mode'; } } catch (e) {} ``` -------------------------------- ### Generate Cloudinary Image URL Source: https://github.com/smeevil/cloudex/blob/master/README.md This snippet shows how to generate a Cloudinary URL for an image using its public ID. Additional options can be passed as a second argument to transform the image (e.g., cropping, resizing, effects). ```elixir Cloudex.image_url(public_id, [width: 100, crop: :fill]) ``` -------------------------------- ### Upload Images to Cloudinary (Elixir) Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.html Uploads a list of image files or URLs to Cloudinary. This function is the core feature of the Cloudex library. It accepts a list of image sources and optional configuration, returning an upload result. ```elixir upload(list, options \\ %{}) # Example usage (assuming 'list' contains image file paths or URLs): upload(["path/to/image.jpg", "http://example.com/image.png"], %{folder: "my_images"}) ``` -------------------------------- ### Upload Image with Context Metadata to Cloudinary (Elixir) Source: https://context7.com/smeevil/cloudex/llms.txt Uploads an image to Cloudinary and associates custom key-value context metadata. This metadata is stored with the asset and can be retrieved later. The context is provided as a map within the upload options. ```elixir # Upload with context (key-value pairs) {:ok, image} = Cloudex.upload("path/to/image.jpg", %{ context: %{ caption: "Sunset at the beach", photographer: "John Doe", location: "California" } }) # Context is stored and returned %Cloudex.UploadedImage{ public_id: "beach_sunset", context: %{ "caption" => "Sunset at the beach", "photographer" => "John Doe", "location" => "California" } } ``` -------------------------------- ### Merge Missing Environment Variables into a Map Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.EnvOptions.html The `merge_missing_settings` function is similar to `merge`, but it only merges system environment variables if the corresponding value in the input map is `nil`. This ensures that existing values in the map are not overwritten by environment variables. It returns the modified map. ```Elixir Cloudex.EnvOptions.merge_missing_settings(options :: map()) :: map() ``` -------------------------------- ### Generate URL with Version - Elixir Source: https://context7.com/smeevil/cloudex/llms.txt Includes the specific version number of a Cloudinary asset in the generated URL. This is crucial for cache control, enabling rollbacks, and ensuring users always access the intended asset version. ```elixir # URL with version number url = Cloudex.Url.for("my_image", %{version: 1471959066}) # => "//res.cloudinary.com/my_cloud_name/image/upload/v1471959066/my_image" # Version with transformations url = Cloudex.Url.for("my_image", %{ width: 400, height: 300, version: 1471959066 }) # => "//res.cloudinary.com/my_cloud_name/image/upload/h_300,w_400/v1471959066/my_image" ``` -------------------------------- ### Stop Cloudex.Settings GenServer in Elixir Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.Settings.html Helper function to gracefully stop the running Cloudex.Settings GenServer process. Terminates the service and cleans up resources. ```elixir Cloudex.Settings.stop() ``` -------------------------------- ### Generate Cloudinary URL Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.CloudinaryApi.html Generates a URL for a Cloudinary resource. This function allows for specifying the resource type (e.g., 'image', 'video') to construct the appropriate URL. It is a utility function for accessing assets stored on Cloudinary. ```elixir url(resource_type) ``` -------------------------------- ### Delete Multiple Images in Batch with Elixir Source: https://context7.com/smeevil/cloudex/llms.txt Deletes multiple images in a single batch operation by providing a list of public IDs. Returns a list of tuples containing deletion results for each image, supporting error handling with pattern matching. ```elixir results = Cloudex.delete(["image_id_1", "image_id_2", "image_id_3"]) [ {:ok, %Cloudex.DeletedImage{public_id: "image_id_1"}}, {:ok, %Cloudex.DeletedImage{public_id: "image_id_2"}}, {:ok, %Cloudex.DeletedImage{public_id: "image_id_3"}} ] Enum.each(results, fn {:ok, deleted} -> IO.puts("Deleted: #{deleted.public_id}") {:error, reason} -> IO.puts("Failed: #{reason}") end) ``` -------------------------------- ### Convert Cloudinary JSON Result to Struct (Elixir) Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.CloudinaryApi.Live.html Converts a JSON result obtained from Cloudinary into a %Cloudex.UploadedImage{} struct. This function takes the JSON result (as a map) and a source identifier as input. The resulting struct contains detailed information about the uploaded image, such as its URL, public ID, dimensions, and more. ```elixir json_result_to_struct(result, source) :: %Cloudex.UploadedImage{bytes: term, created_at: term, etag: term, format: term, height: term, original_filename: term, public_id: term, resource_type: term, secure_url: term, signature: term, source: term, tags: term, type: term, url: term, version: term, width: term} ``` -------------------------------- ### Delete Images by Prefix (Elixir) Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.html Deletes multiple images from Cloudinary that share a common prefix in their filenames. This is efficient for batch deletion of related images. It takes a string representing the prefix to match. ```elixir delete_prefix(prefix) # Example usage: delete_prefix("images/batch_") ``` -------------------------------- ### Delete Images by Prefix Pattern in Elixir Source: https://context7.com/smeevil/cloudex/llms.txt Removes all images from Cloudinary whose public IDs match a specified prefix pattern. Useful for bulk deletion of related images organized with common naming conventions like temporary uploads or dated folders. ```elixir {:ok, prefix} = Cloudex.delete_prefix("temp_uploads") {:ok, prefix} = Cloudex.delete_prefix("thumbnails/2024") ``` -------------------------------- ### DELETE /cloudinary/delete_prefix Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.CloudinaryApi.html Deletes multiple images from Cloudinary based on a common prefix in their public IDs. ```APIDOC ## DELETE /cloudinary/delete_prefix ### Description Deletes images given their prefix. ### Method DELETE ### Endpoint /cloudinary/delete_prefix ### Parameters #### Query Parameters - **prefix** (string) - Required - The prefix of the public IDs for images to delete. - **opts** (map) - Optional - A map for additional options. ### Request Body (Not applicable for this endpoint, parameters are passed via query) ### Request Example (Example would depend on how the client is making the request) ### Response #### Success Response (200) - **ok** (tuple) - Returns `{:ok, [string]}` containing a list of public IDs of the deleted images. #### Error Response - **error** (tuple) - Returns `{:error, any}` if the deletion fails. ``` -------------------------------- ### Utility Function: json_result_to_struct Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.CloudinaryApi.html Converts the JSON result from Cloudinary into a structured %UploadedImage{}. ```APIDOC ## Utility Function: json_result_to_struct ### Description Converts the JSON result obtained from Cloudinary into a structured `Cloudex.UploadedImage` struct. ### Method (N/A - This is a utility function, not an HTTP endpoint) ### Endpoint (N/A) ### Parameters #### Path Parameters - **result** (map) - Required - The JSON response from Cloudinary. - **source** (string) - Required - The source of the result (e.g., "cloudinary"). ### Request Body (Not applicable) ### Request Example ```elixir cloudinary_response = %{"public_id" => "my_image", "url" => "http://example.com/my_image.jpg"} struct_data = Cloudex.CloudinaryApi.json_result_to_struct(cloudinary_response, "cloudinary") ``` ### Response #### Success Response - **%Cloudex.UploadedImage{}** - A struct containing details of the uploaded image, including: - `bytes` (term) - `context` (term) - `created_at` (term) - `etag` (term) - `format` (term) - `height` (term) - `moderation` (term) - `original_filename` (term) - `phash` (term) - `public_id` (term) - `resource_type` (term) - `secure_url` (term) - `signature` (term) - `source` (term) - `tags` (term) - `type` (term) - `url` (term) - `version` (term) - `width` (term) #### Error Response (Not applicable for this utility function, errors would typically be handled during the conversion process itself.) ``` -------------------------------- ### Delete Image with Cloudinary API (Elixir) Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.CloudinaryApi.Live.html Handles the deletion of an image from Cloudinary using its public ID. It returns an {:ok, %Cloudex.DeletedImage{}} tuple on successful deletion or an {:error, any} tuple if the public ID is invalid or another error occurs. This function is part of the Cloudex.CloudinaryApi module. ```elixir delete(item :: String.t()) :: {:ok, %Cloudex.DeletedImage{public_id: term}} | {:error, any} delete(any) :: {:error, [String.t()]} ``` -------------------------------- ### Convert Cloudinary JSON to Struct Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.CloudinaryApi.html Transforms the JSON response from Cloudinary into a structured %Cloudex.UploadedImage{} Elixir struct. This is useful for easily accessing uploaded image details such as URL, dimensions, and format. It requires the JSON result (as a map) and the source from which the result was obtained. ```elixir json_result_to_struct(result, source) :: %Cloudex.UploadedImage{bytes: any, context: any, created_at: any, etag: any, format: any, height: any, moderation: any, original_filename: any, phash: any, public_id: any, resource_type: any, secure_url: any, signature: any, source: any, tags: any, type: any, url: any, version: any, width: any} ``` -------------------------------- ### Delete Image by Public ID Source: https://github.com/smeevil/cloudex/blob/master/doc/Cloudex.CloudinaryApi.html Deletes an image from Cloudinary using its public ID. This function returns an {:ok, %Cloudex.DeletedImage{}} tuple on success, containing details of the deleted image, or an {:error, reason} tuple if the deletion fails. It accepts the public ID as a string and optional Cloudinary parameters. ```elixir delete(item, opts \\ %{}) :: {:ok, %Cloudex.DeletedImage{public_id: any}} | {:error, any} ``` -------------------------------- ### Delete Images from Cloudinary using Cloudex Elixir Source: https://github.com/smeevil/cloudex/blob/master/README.md Delete images from Cloudinary using the Cloudex.delete/1 function. This function accepts a single public ID or a list of public IDs to delete. It returns a tuple indicating success or failure for each deletion request. ```elixir iex> Cloudex.delete("public-id-1") {:ok, %Cloudex.DeletedImage{public_id: "public-id-1"}} ``` ```elixir iex> Cloudex.delete(["public-id-1", "public-id-2"]) [ {:ok, %Cloudex.DeletedImage{public_id: "public-id-1"}}, {:ok, %Cloudex.DeletedImage{public_id: "public-id-2"}} ] ```