1. Products
  2.   Image
  3.   Swift
  4.   SwiftGD
 
  

Open Source Swift API for server-Side Image Processing

Swift Library for included support for image loading, saving, and manipulating. It allows image creation with custom width and height, image resizing & cropping a specific part of the image

SwiftGD is a small size wonderful library that has included support for server-side Swift image manipulation. It is a fantastic Swift wrapper for libgd, allowing the creation of images and drawing shapes in places where Core Graphics functionality isn’t available. The library is very simple to use and can easily handle image loading, saving, and manipulating using Swift code. One great feature of the library is that it manages GD resources for users, so the underlying memory is released when the images are destroyed.

The library has included support for several important features related to image handling such as PNGs & JPEGs loading from disk, PNGs & JPEGs images saving to disk, image creation with custom width and height, image resizing support, cropping a specific part of the image, drawing shapes and lines, color filing from coordinates, horizontal or vertical image flipping, image stroking support, drawing or text filling and many more. The library has also included several important effects such as pixelate, blur, colorize, de-saturate, and so on. . The library is open source and is available under the MIT License.

Previous Next

Getting Started with SwiftGD

Clone the latest sources using the following command.

Install SwiftGD via Github 

 $ git clone https://github.com/twostraws/SwiftGD.git

Creating New Images using Swift

The open source Swift library SwiftGD enables software developers to create new images with just a couple of lines of Swift code. Developers can easily create an image from scratch by providing the width and height of the image. It also supports creating images from data instances.  It also generates Images while users perform a resize or crop operation, which means the original image will be untouched. You can also apply some basic effects on images with ease.

Creating New Images via Swift Library

 import Foundation
import SwiftGD

// figure out where to save our file
let currentDirectory = URL(fileURLWithPath: FileManager().currentDirectoryPath)
let destination = currentDirectory.appendingPathComponent("output-1.png")

// attempt to create a new 500x500 image
if let image = Image(width: 500, height: 500) {
    // flood from from X:250 Y:250 using red
    image.fill(from: Point(x: 250, y: 250), color: Color.red)

    // draw a filled blue ellipse in the center
    image.fillEllipse(center: Point(x: 250, y: 250), size: Size(width: 150, height: 150), color: Color.blue)
        
    // draw a filled green rectangle also in the center
    image.fillRectangle(topLeft: Point(x: 200, y: 200), bottomRight: Point(x: 300, y: 300), color: Color.green)

    // remove all the colors from the image
    image.desaturate()
        
    // now apply a dark red tint
    image.colorize(using: Color(red: 0.3, green: 0, blue: 0, alpha: 1))
        
    // save the final image to disk
    image.write(to: destination)
}

Shapes Drawing using Swift

The SwiftGD library makes it easy for software developers to draw and manipulate shapes inside their Swift applications. The library has provided several methods that can be used to draw into your images, such as applying flood fill from one point to the other, drawing a line from one point to the other, pixel setting a specific point, filling an ellipse at the center, drawing an empty ellipse at the center, empty rectangle drawing from one side to the other and so on.

Draws Rectangles via Swift API

 import Foundation
import SwiftGD

let currentDirectory = URL(fileURLWithPath: FileManager().currentDirectoryPath)
let destination = currentDirectory.appendingPathComponent("output-2.png")

if let image = Image(width: 500, height: 500) {
    var counter = 0
        
    for i in stride(from: 0, to: 250, by: 10) {
        let drawColor: Color
        
        if counter % 2 == 0 {
            drawColor = .blue
        } else {
            drawColor = .white
        }
        
        image.fillRectangle(topLeft: Point(x: i, y: i), bottomRight: Point(x: 500 - i, y: 500 - i), color: drawColor)
        counter += 1
    }

    image.blur(radius: 10)
    image.write(to: destination)
}

Image Manipulation inside Swift Apps

The open source Swift library SwiftGD allows computer programmers to manipulate images with ease inside Swift apps. The library has provided several methods which can be used to apply effects to images such as applying a Gaussian blur effect, applying image tint, rendering your image greyscale, flipping your image horizontally as well as vertically, simplifying your image too large pixels, and much more.

Create Gradient via Swift API

 import Foundation
import SwiftGD

let currentDirectory = URL(fileURLWithPath: FileManager().currentDirectoryPath)
let destination = currentDirectory.appendingPathComponent("output-3.png")

let size = 500

if let image = Image(width: size, height: size) {
    for x in 0 ... size {
        for y in 0 ... size {
            image.set(pixel: Point(x: x, y: y), to: Color(red: Double(x) / Double(size), green: Double(y) / Double(size), blue: 0, alpha: 1))
        }
    }
        
    image.write(to: destination)
}

Images Loading & Reading

The free Swift library SwiftGD gives software apps the capability to load and read images inside their own Swift apps. You need to provide the location of the image on the disk for successful loading. The file extension is used by the library for loading the correct file format, so it's important you name your files with "jpg", "jpeg", or "png".

Reading Images via Swift API

 let location = URL(fileURLWithPath: "/path/to/image.png")
let image = Image(url: location)
 English