Code Monkey home page Code Monkey logo

imgix-swift's Introduction

imgix logo

imgix-swift is a client library for generating image URLs with imgix. Written in Swift, but can be used with Objective-C codebases as well.

CocoaPods Carthage compatible Build Status Supported Platforms License FOSSA Status


Installation

// add the following to your Package.swift manifest, within the dependencies array

.package(url: "https://github.com/imgix/imgix-swift.git", from: "1.0.0")

// add the package name "ImgixSwift" to the list of named dependencies in your project target

.target(
    name: "YourSwiftProject",
    dependencies: ["ImgixSwift"]),

As of version 1.0.0 imgix Swift client is compatible with Swift 5.0.

The latest version compatible with Swift 4.0 is 0.4.3.

The latest version compatible with Swift 3.0 is 0.3.0.

Usage

Swift

// Import the framework
import ImgixSwift

// Set up an ImgixClient
let client = ImgixClient.init(host: "assets.imgix.net")

// Build a basic URL
client.buildUrl("dog.jpg") // => https://assets.imgix.net/dog.jpg

// Add some parameters
client.buildUrl("dog.jpg", params: [
  "w": 300,
  "h": 300,
  "fit": "crop"
]) // => https://assets.imgix.net/dog.jpg?fit=crop&h=300&w=300

Objective-C

If your project doesn't contain any other Swift code, make sure to set your target's Build Settings > Build Options > Embedded Content Contains Swift Code to YES.

// Import the framework
#import <ImgixSwift/ImgixSwift.h>

// Set up an ImgixClient
ImgixClient *client = [[ImgixClient alloc] initWithHost:@"assets.imgix.net"];

// Build a basic URL
[client buildUrl:@"dog.jpg"]; // => https://assets.imgix.net/dog.jpg

// Add some parameters
[client buildUrl:@"dog.jpg", params:@{
  @"w": @300,
  @"h": @300,
  @"fit": @"crop",
}]; // => https://assets.imgix.net/dog.jpg?fit=crop&h=300&w=300

Advanced Usage

Automatic Signing

If you're using a source that requires signed URLs, imgix-swift can automatically build and sign them for you.

let signedClient = ImgixClient.init(
  host: "imgix-library-secure-test-source.imgix.net",
  secureUrlToken: "EHFQXiZhxP4wA2c4"
)

signedClient.buildUrl("dog.jpg", params: [
  "bri": 50
]) // => https://imgix-library-secure-test-source.imgix.net/dog.jpg?bri=50&s=3b293930d9c288fb788657fd9ed8164f

Automatic Base64 Encoding

imgix-swift will automatically Base64-encode any parameter key ending in 64, according to the requirements of imgix's Base64 variant parameters.

let client = ImgixClient.init(host: "assets.imgix.net")

client.buildUrl("dog.jpg", params: [
  "w": 640,
  "txt64": "๐Ÿถ Puppy!",
  "txtfont64": "Avenir Next Demi,Bold",
  "txtalign": "center,top",
  "txtpad": 50,
  "txtshad": 10,
  "txtclr": "fff",
  "txtfit": "max",
  "txtsize": 50
]) // => https://assets.imgix.net/dog.jpg?txt64=8J-QtiBQdXBweSE&txtalign=center%2Ctop&txtclr=fff&txtfit=max&txtfont64=QXZlbmlyIE5leHQgRGVtaSxCb2xk&txtpad=50&txtshad=10&txtsize=50&w=640

URL Reconstruction

You can reconstruct existing URLs by using the ImgixClient#reconstruct method. Existing parameters on the input URL will be merged and/or overridden by passed params.

let client = ImgixClient.init(host: "assets.imgix.net")
let inputUrl = URL.init(string: "https://paulstraw.imgix.net/pika.jpg?w=300")!

client.reconstruct(originalURL: inputUrl, params: [
  "h": 300,
  "fit": "crop"
]) // => https://paulstraw.imgix.net/pika.jpg?fit=crop&h=300&w=300

What is the ixlib param

For security and diagnostic purposes, we default to signing all requests with the language and version of library used to generate the URL. This can be disabled by setting client.includeLibraryParam = false.

License

FOSSA Status

imgix-swift's People

Contributors

atlawrie avatar dependabot[bot] avatar fossabot avatar frederickfogerty avatar hashknot avatar ikait avatar luqven avatar paulstraw avatar pedroaguilar avatar shimastripe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

imgix-swift's Issues

Allow the imgix swift library to have initializer that accepts no `host` parameter

Description:
When using the Swift Library to construct imgix URLs, you must include a host to initialize the imgix client. An image of the code block is included below.
https://assetmanager.imgix.net/Testing-%F0%9F%98%83.png

The suggestion would be to allow these URLs to be built using an input URL. Similar to what is being used in our current URL reconstruction library.
https://assetmanager.imgix.net/URlRecon.png

Problem:
Currently, Grailed is trying to use our library to build the URLs they will be listing in their IOS app using the Swift library. They are building these URLs using custom domains. So when building the host URL they would rather the client builder use the host that's already in the URL.

Suggested Solution:
To allow the URLs to accept a no-host input and grab the host name from the input URL.

Describe alternatives you've considered
For now, they can just use the imgixClient instance or use the URL reconstructor to ensure they get the correct URL.

Build issue with pod

I am using both Objective C and Swift languages in my project and have created a bridging header. My project had been working from a year but upon installing Imgix pod it's giving me build error.

Error: In an objective c file -> projectName-Swift.h file not found.
Can you please help me?

Accept NSURL or NSURLCompontents

It would be convenient if there was a way to build an imgix formatted image from an nsurl.

UseCase:

  • Our Api returns full image urls for all entities (i.e. https://domain.com/images123.png?possible_query=stuff).
    It would be nice to have a function on ImgixClient which could somehow take this in as a NSURL or NSURLComponents object, along with an imgix extra parameters formatted dictionary, and then build the final url preserving the original information from the passed in information.

example:
imgixClient.reconstruct(url:NSURL, params:[String:AnyObject]) -> NSURL?

I have some code which does this in our project, happy to open a PR if you think it's a useful feature.

sigabort crash

We got the crash report below. Any ideas what could've caused it?

Crashed: com.apple.main-thread
SIGABRT ABORT 0x0000000189f72014

Crashed: com.apple.main-thread
0  libsystem_kernel.dylib         0x189f72014 __pthread_kill + 8
1  libsystem_pthread.dylib        0x18a03a450 pthread_kill + 112
2  libsystem_c.dylib              0x189ee63e0 abort + 140
3  libsystem_malloc.dylib         0x189fb6a38 _nano_vet_and_size_of_live + 330
4  libsystem_malloc.dylib         0x189fb7db8 nano_free + 220
5  CoreFoundation                 0x18af29060 __CFCharacterSetDeallocate + 108
6  CoreFoundation                 0x18af51a74 _CFRelease + 216
7  CoreFoundation                 0x18af290a0 __CFCharacterSetDeallocate + 172
8  CoreFoundation                 0x18af51a74 _CFRelease + 216
9  libswiftFoundation.dylib       0x100acd914 _TFE10FoundationSS50stringByAddingPercentEncodingWithAllowedCharactersfCSo14NSCharacterSetGSqSS_ + 136
10 ImgixSwift                     0x1003a08cc String.ixEncodeUriComponent() -> String (String+ImgixSwift.swift:34)
11 ImgixSwift                     0x10039f958 specialized ImgixClient.buildUrl(String, params : NSDictionary) -> NSURL (ImgixClient.swift:70)
12 ImgixSwift                     0x10039dd88 ImgixClient.buildUrl(String, params : NSDictionary) -> NSURL (ImgixClient.swift:39)
13 shoptanga                      0x10003a4e4 specialized static ImageHelper.resizedImageURL(String, width : CGFloat, height : CGFloat)

This is the calling code:

import Foundation
import UIKit
import ImgixSwift

class ImageHelper {
  class func resizedImageURL(sourceURL: String, width:CGFloat, height:CGFloat) -> NSURL {
    let client = ImgixClient.init(host: "tanga1.imgix.net", secureUrlToken: "XXXXXXX")
    return client.buildUrl(
      sourceURL,
      params: [
        "width":    width,
        "height":   height,
        "format":   "auto",
        "lossless": "true",
        "cs": "strip"])
  }
}

invalid signatures for comma-separated values without base64 encoding

I'm trying to use the Imgix-Swift library to generate signed imgix URLs in an objective-c iPhone application.

Unsigned, my URL looks like this:

https://host/94BAB140-493B-453F-97CE-739C2685B88A.jpg?or=1&w=488&h=488&fit=max&auto=redeye&rect=1626,652,848,848

When I try to build a signed URL with ImgixClient -buildURL:params:, I get this

https://host/94BAB140-493B-453F-97CE-739C2685B88A.jpg?ixlib=swift-0.1.2&h=488&rect=1626%2C652%2C848%2C848&or=1&w=488&auto=redeye&fit=max&s=f3a7f97bfa05a0fe584c5151fe69e87a

This results in a 401.

If instead, I use the base-64 parameters, I get this:

https://host/94BAB140-493B-453F-97CE-739C2685B88A.jpg?auto64=cmVkZXll&rect64=MTYyNiw2NTIsODQ4LDg0OA&ixlib=swift-0.1.2&w64=NDg4&or64=MQ&fit64=bWF4&h64=NDg4&s=871421f2c35975e750211f222037c58e

This works just fine.

I adjusted my input, and narrowed the problematic parameter down to rect, which has a comma-separated value.

feat: allow `buildUrl` to use pre-encoded URLs

Describe the feature

If the buildUrl function builds a Web Proxy URL with a pre-encoded URL, the URL will be double encoded. This returns a 400 error in the Rendering API.

To Reproduce

  1. Create a Web Proxy Source in imgix
  2. Use this Swift code (use your own secureUrlToken):
let signedClient = ImgixClient.init(
  host: "imgix-web-proxy-test-source.imgix.net",
  secureUrlToken: "EHFQXiZhxP4wA2c4"
)

// This will return a `400` error
signedClient.buildUrl("http%3A%2F%2Fassets.imgix.net%2Fexamples%2Fcat.jpg") // => https://imgix-library-secure-test-source.imgix.net/http%253A%252F%252Fassets.imgix.net%252Fexamples%252Fcat.jpg?s=5751ef13a5ca325fab9c4038b080cd80

iOS 9.0

I'm getting the following error when building the latest release with carthage:
Module file's minimum deployment target is ios9.3 v9.3.

My guess is that this occurs because the minimum deployment target for my app is iOS 9.0 which is lower than the deployment target for the imgix-swift sdk(9.3). Is it possible to have the deployment target lowered for imgix-swift?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.