Image Processing (IMG) is a massive, secure, cost-effective and highly reliable image processing service. After source images are uploaded to OSS, you can process images on any Internet device at any anytime, from anywhere through simple RESTful APIs.

For more information about IMG, see Image Processing.

Basic features

OSS offers the following IMG features:

Usage

IMG uses standard HTTP GET. You can configure IMG parameters in QueryString of a URL.

If the ACL of an image object is private read and write, only authorized users are allowed for access.

  • Anonymous access

    You can use the following format in level-3 domains to access a processed image:

    http://<yourBucketName>.<yourEndpoint>/<yourObjectName>?x-oss-process=image/<yourAction>,<yourParamValue>
    Parameter Description
    bucket Specifies the name of a bucket.
    endpoint Specifies endpoint used to access a region.
    object Specifies the name of an image object.
    image Specifies the reserved identifier for IMG.
    action Specifies the operations on an image, such as scaling, cropping, and rotating.
    param Specifies the parameter that indicates the operation on an image.
    • Basic operations

      For example, scale an image to a width of 100 px. Adjust the height based on the ratio.

      http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_100
    • Customized image styles

      You can use the following format in level-3 domains to access a processed image:

      http://<yourBucketName>.<yourEndpoint>/<yourObjectName>?x-oss-process=style/<yourStyleName>
      • style: specifies a reserved identifier of a customized image style.
      • yourStyleName: specifies the name of a custom image style. It is the name specified in the rule that is created on the OSS console.

      Example:

      http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=style/oss-pic-style-w-100
    • Cascade operations

      Cascade operations allow you to perform multiple operations on an image sequentially.

      http://<yourBucketName>.<yourEndpoint>/<yourObjectName>?x-oss-process=image/<yourAction1>,<yourParamValue1>/<yourAction2>,<yourParamValue2>/...

      Example:

      http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_100/rotate,90
    • HTTPS access

      IMG supports access through HTTPS. Example:

      https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_100
  • Authorized access

    Authorized access allows you to customize an image style, HTTPS access, and cascade operations.

    Use the following code to generate a signed URL for IMG:

    package main
    
    import (
        "fmt"
        "os"
        "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func HandleError(err error) {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    
    func main() {
        // Create an OSSClient instance.
        client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
        if err ! = nil {
    	HandleError(err)
        }
    	
        // Obtain the bucket.
        bucketName := "<yourBucketName>"
        bucket, err := client.Bucket(bucketName)
        if err ! = nil {
    	HandleError(err)
        }
    
        ossImageName := "<yourImageName>"
        signedURL, err := bucket.SignURL(ossImageName, oss.HTTPGet, 600, oss.Process("image/format,png"))
        if err ! = nil {
    	HandleError(err)
        } else {
    	fmt.Println(signedURL)
        }
    }
  • Access with SDK

    You can use SDK to access and process an image object.

    SDK allows you to customize image styles, HTTPS access, and cascade operations.

    • Basic operations

      Run the following code to perform basic operations on an image:

      package main
      
      import (
          "fmt"
          "os"
          "github.com/aliyun/aliyun-oss-go-sdk/oss"
      )
      
      func HandleError(err error) {
          fmt.Println("Error:", err)
          os.Exit(-1)
      }
      
      func main() {
          // Create an OSSClient instance.
          client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
          if err ! = nil {
      	HandleError(err)
          }
      	
          // Obtain the bucket.
          bucketName := "<yourBucketName>"
          bucket, err := client.Bucket(bucketName)
          if err ! = nil {
      	HandleError(err)
          }
      
          // Scale the image.
          sourceImageName := "example.png"
          targetImageName := "example-resize.png"
          style := "image/resize,m_fixed,w_100,h_100"
          err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(style))
          if err ! = nil {
      	HandleError(err)
          }
          
          // Crop the image.
          targetImageName = "example-crop.png"
          style = "image/crop,w_100,h_100,x_100,y_100,r_1"
          err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(style))
          if err ! = nil {
      	HandleError(err)
          }
          
          // Rotate the image.
          targetImageName = "example-rotate.png"
          style = "image/rotate,90"
          err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(style))
          if err ! = nil {
      	HandleError(err)
          }
          
          // Sharpen the image.
          targetImageName = "example-sharpen.png"
          style = "image/sharpen,100"
          err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(style))
          if err ! = nil {
      	HandleError(err)
          }
          
          // Add watermarks.
          targetImageName = "example-watermark.png"
          style = "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ"
          err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(style))
          if err ! = nil {
      	HandleError(err)
          }
          
          // Convert the image format.
          targetImageName = "example-formatconvert.jpg"
          style = "image/format,jpg"
          err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(style))
          if err ! = nil {
      	HandleError(err)
          }
      }
    • Customize an image style

      Run the following code to customize an image style:

      package main
      
      import (
          "fmt"
          "os"
          "github.com/aliyun/aliyun-oss-go-sdk/oss"
      )
      
      func HandleError(err error) {
          fmt.Println("Error:", err)
          os.Exit(-1)
      }
      
      func main() {
          // Create an OSSClient instance.
          client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
          if err ! = nil {
      	HandleError(err)
          }
      	
          // Obtain the bucket.
          bucketName := "<yourBucketName>"
          bucket, err := client.Bucket(bucketName)
          if err ! = nil {
      	 HandleError(err)
          }
      
          // Customize the image style.
          sourceImageName := "example.png"
          targetImageName := "example-custom.png"
          style := "style/<yourCustomStyleName>"
          err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(style))
          if err ! = nil {
      	HandleError(err)
          }
      }
    • Cascade operations

      Run the following code to perform cascade operations on an image:

      package main
      
      import (
          "fmt"
          "os"
          "github.com/aliyun/aliyun-oss-go-sdk/oss"
      )
      
      func HandleError(err error) {
          fmt.Println("Error:", err)
          os.Exit(-1)
      }
      
      func main() {
          // Create an OSSClient instance.
          client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
          if err ! = nil {
      	HandleError(err)
          }
      	
          // Obtain the bucket.
          bucketName := "<yourBucketName>"
          bucket, err := client.Bucket(bucketName)
          if err ! = nil {
      	HandleError(err)
           }
      
          // Customize an image style.
          sourceImageName := "example.png"
          targetImageName := "example-resize.png"
          style := "image/resize,m_fixed,w_100,h_100/rotate,90"
          err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(style))
          if err ! = nil {
      	 HandleError(err)
          }
      }

Image processing persistence

Run the following code for image processing persistence:
package main

import (
    "fmt"
    "os"
    "encoding/base64"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func HandleError(err error) {
    fmt.Println("Error:", err)
    os.Exit(-1)
}

func main() {
    // Create an OSSClient instance.
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err ! = nil {
	HandleError(err)
    }    
    
    // Obtain the bucket.
    bucketName := "<yourBucketName>"
    bucket, err := client.Bucket(bucketName)
    if err ! = nil {
	 HandleError(err)
    }


    // Image processing persistence: Scale the image. The target image is stored in the same bucket by default.
    sourceImageName := "example.png"
    targetImageName := "example-resize.png"
    style := "image/resize,m_fixed,w_100,h_100"
    process := fmt.Sprintf("%s|sys/saveas,o_%v", style, base64. URLEncoding.EncodeToString([]byte(targetImageName)))
    result, err := bucket.ProcessObject(sourceImageName, process)
    if err ! = nil {
	HandleError(err)
    } else {
        fmt.Println(result)
    }
   
    // Image processing persistence: Scale the image. The target image is stored in the different bucket.
    targetBucketName := "<yourTargetBucketName>"
    targetImageName = "example-crop-different-bucket.png"
    style = "image/resize,m_fixed,w_100,h_100"
    process = fmt.Sprintf("%s|sys/saveas,o_%v,b_%v", style, base64. URLEncoding.EncodeToString([]byte(targetImageName)), base64. URLEncoding.EncodeToString([]byte(targetBucketName)))
    result, err = bucket.ProcessObject(sourceImageName, process)
    if err ! = nil {
	HandleError(err)
    } else {
        fmt.Println(result)
    }
    
    // Image processing persistence: Crop the image.
    targetImageName = "example-crop.png"
    style = "image/crop,w_100,h_100,x_100,y_100,r_1"
    process = fmt.Sprintf("%s|sys/saveas,o_%v", style, base64. URLEncoding.EncodeToString([]byte(targetImageName)))
    result, err = bucket.ProcessObject(sourceImageName, process)
    if err ! = nil {
	HandleError(err)
    } else {
        fmt.Println(result)
    }
    
    // Image processing persistence: Rotate the image.
    targetImageName = "example-rotate.png"
    style = "image/rotate,90"
    process = fmt.Sprintf("%s|sys/saveas,o_%v", style, base64. URLEncoding.EncodeToString([]byte(targetImageName)))
    result, err = bucket.ProcessObject(sourceImageName, process)
    if err ! = nil {
	HandleError(err)
    } else {
        fmt.Println(result)
    }
    
    // Image processing persistence: Sharpen the image.
    targetImageName = "example-sharpen.png"
    style = "image/sharpen,100"
    process = fmt.Sprintf("%s|sys/saveas,o_%v", style, base64. URLEncoding.EncodeToString([]byte(targetImageName)))
    result, err = bucket.ProcessObject(sourceImageName, process)
    if err ! = nil {
	HandleError(err)
    } else {
       fmt.Println(result)
    }
    
    // Image processing persistence: Add watermarks.
    targetImageName = "example-watermark.png"
    style = "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ"
    process = fmt.Sprintf("%s|sys/saveas,o_%v", style, base64. URLEncoding.EncodeToString([]byte(targetImageName)))
    result, err = bucket.ProcessObject(sourceImageName, process)
    if err ! = nil {
	HandleError(err)
    } else {
        fmt.Println(result)
    }
    
    // Image processing persistence: Covert the image format.
    targetImageName = "example-formatconvert.jpg"
    style = "image/format,jpg"
    process = fmt.Sprintf("%s|sys/saveas,o_%v", style, base64. URLEncoding.EncodeToString([]byte(targetImageName)))
    result, err = bucket.ProcessObject(sourceImageName, process)
    if err ! = nil {
	HandleError(err)
    } else {
        fmt.Println(result)
    }
}