Extract Image Metadata
Use the image-info
endpoint to get metadata describing an image.
The image-info
endpoint is for obtaining metadata describing an image. In this tutorial we illustrate using this endpoint to obtain an image's metadata. We first call the image-info
REST endpoint directly using cURL. We then use the DynamicPDF Cloud API client libraries to programmatically call the endpoint.
#
Required ResourcesTo complete this tutorial, you must add the Get Image Information (image-info Endpoint) sample to your samples
folder in your cloud storage space using the the Resource Manager. After adding the sample resources, you should see a samples/get-image-info-image-info-endpoint
folder containing the resources for this tutorial.
Sample | Sample Folder | Resources |
---|---|---|
Get Image Information (image-info Endpoint) | samples/get-image-info-image-info-endpoint | dynamicpdfLogo.png |
- From the Resource Manager, download
dynamicpdfLogo.png
to your local system; here we assume/temp/dynamicpdf-api-samples/get-image-info
.
Resource | Cloud/Local |
---|---|
dynamicpdfLogo.png | local |
tip
See Sample Resources for instructions on adding sample resources.
#
Obtaining API KeyThis tutorial assumes a valid API key obtained from the DynamicPDF Cloud API's Environment Manager
. Refer to the following for instructions on getting an API key.
tip
If you are not familiar with the Resource Manager or Apps and API Keys, refer to the following tutorial and relevant Users Guide pages.
#
Make Request Using APIThe image-info
endpoint is a form post that takes the API key in the header and the image as binary data in the request's body. The following uses cURL to make an HTTP POST request, passing the image as binary data. Note that in the Header we pass the Content-Type
as image/png
.
curl -X POST "https://api.dynamicpdf.com/v1.0/image-info" -H "Authorization: Bearer DP.xxxx--api-key--xxxxx" -H "Content-Type: image/png" --data-binary "@c:/temp/dynamicpdf-api-samples/get-image-info/dynamicpdflogo.png"
#
Examine API ResponseThe REST call returns the following JSON describing the image.
[ { "pageNumber": 1, "width": 262, "height": 250, "horizondalDpi": 300, "verticalDpi": 300, "numberOfComponents": 3, "bitsPerComponent": 8, "colorSpace": "indexed" }]
info
The JSON response returns an array so that the endpoint can support multi-page tiffs. Refer to the Users Guide for an example using a multi-page tiff image.
#
Make Request Using Client LibraryNow let's use the a client library to call the REST endpoint. To simplify your development, you can use any of the DynamicPDF Cloud API client libraries to complete this tutorial section. Each client library tab contains tutorial steps particular to the selected language.
#
Complete SourceYou can access the complete source for this project at one of the following GitHub projects.
Language | File Name | Location (package/namespace/etc.) | GitHub Project |
---|---|---|---|
Java | GetImageInfo.java | com.dynamicpdf.api.examples | https://github.com/dynamicpdf-api/java-client-examples |
C# | Program.cs | GetImageInfo | https://github.com/dynamicpdf-api/dotnet-client-examples |
Nodejs | GetImageInfo.js | nodejs-client-examples | https://github.com/dynamicpdf-api/nodejs-client-examples |
PHP | GetImageInfo.php | php-client-examples | https://github.com/dynamicpdf-api/nodejs-client-examples |
tip
Click on the language tab of choice to view the tutorial steps for the particular language.
- C# (.NET)
- Java
- Node.js
- PHP
Available on NuGet:
Install-Package DynamicPDF.API
- Create a new Console App (.NET Core) project named
GetImageInfo
. - Add the DynamicPDF.API NuGet package.
- Create a new static method named
Run
. - Add a new
ImageResource
instance and pass the path to the image in the constructor. - Create a new
ImageInfo
instance and pass theImageResource
instance to the constructor. - Add a call to the
ImageInfo
instance'sProcess
method and return the response. - Check that the call was successful and print the image metadata as JSON to the console.
- Run the application and the JSON image metadata appears in the console.
using DynamicPDF.Api;using System;
namespace GetImageInfo{ class Program { static void Main(string[] args) { Run("DP.xxx--api-key--xxx", "c:/temp/dynamicpdf-api-samples/get-image-info/"); }
public static void Run(String apiKey, String basePath) { ImageResource imageResource = new ImageResource(basePath + "dynamicpdflogo.png"); ImageInfo imageInfo = new ImageInfo(imageResource); imageInfo.ApiKey = apiKey; ImageResponse response = imageInfo.Process();
if(response.IsSuccessful) { Console.WriteLine(response.JsonContent); } else { Console.WriteLine(response.ErrorJson); } } }}
Available on NPM:
npm i @dynamicpdf/api
- Use npm to install the DynamicPDF Cloud API module.
- Create a new class named
GetImageInfo
. - Create a static
Run
method. - Add a new
ImageResource
instance and pass the path to the image in the constructor. - Create a new
ImageInfo
instance and pass theImageResource
instance to the constructor. - Add a call to the
ImageInfo
instance'sprocess
method and return the response. - Check that the call was successful and print the image metadata as JSON to the console.
- Add a call to the
GetImageInfo.Run()
method.
import { ImageResource, ImageInfo, ImageResponse} from "@dynamicpdf/api"
export class GetImageInfo { static async Run() { var imageResource = new ImageResource("C:/temp/dynamicpdf-api-samples/get-image-info/dynamicpdfLogo.png"); var imageInfo = new ImageInfo(imageResource); imageInfo.apiKey = "DP.xxx--api-key--xxx";
var imageResponse = await imageInfo.process();
if (imageResponse.isSuccessful) { console.log(JSON.parse(imageResponse.content)); } else { console.log(imageResponse.errorJson); } }}await GetImageInfo.Run();
- Run the application
node ImageInfo.js
and the image metadata prints to the console.
Available on Maven:
https://search.maven.org/search?q=g:com.dynamicpdf.api
<dependency><groupId>com.dynamicpdf.api</groupId><artifactId>dynamicpdf-api</artifactId><version>1.0.0</version></dependency>
Create a new Maven project and add the DynamicPDF API as a dependency
Create a new class named
GetImageInfo
with amain
method.Create a new static method named
Run
.Add a new
ImageResource
instance and pass the path to the image in the constructor.Create a new
ImageInfo
instance and pass theImageResource
instance to the constructor.Add a call to the
ImageInfo
instance'sProcess
method and return the response.Check that the call was successful and print the image metadata as JSON to the console.
package com.dynamicpdf.api.examples;
import com.dynamicpdf.api.ImageInfo;import com.dynamicpdf.api.ImageResource;import com.dynamicpdf.api.ImageResponse;
public class GetImageInfo { public static void main(String[] args) { GetImageInfo.Run("DP.xxxx-api-key--xxxx", "C:/temp/dynamicpdf-api-samples/get-image-info/");
}
public static void Run(String key, String basePath) { ImageResource imageResource = null; imageResource = new ImageResource(basePath + "/dynamicpdflogo.png"); ImageInfo imageInfo = new ImageInfo(imageResource); imageInfo.setApiKey(key); ImageResponse response = imageInfo.process();
if(response.getIsSuccessful()) { System.out.println(response.getJsonContent()); } else { System.out.println(response.getErrorJson()); } } }
Available as a Composer package:
composer require dynamicpdf/api
- Use composer to ensure you have the required PHP libraries.
- Create a new class named
GetImageInfo
. - Add a
Run
method. - Add a new
ImageResource
instance and pass the path to the image in the constructor. - Create a new
ImageInfo
instance and pass theImageResource
instance to the constructor. - Add a call to the
ImageInfo
instance'sProcess
method and return the response. - Check that the call was successful and print the image metadata as JSON to the console.
<?php
require __DIR__ . '/vendor/autoload.php';
use DynamicPDF\Api\ImageResource;use DynamicPDF\Api\ImageInfo;
class GetImageInfo{ private static string $BasePath = "C:/temp/dynamicpdf-api-samples/get-image-info/";
public static function Run() { $imageResource = new ImageResource(GetImageInfo::$BasePath . "dynamicpdfLogo.png"); $imageInfo = new ImageInfo($imageResource); $imageInfo->ApiKey = "DP.xxxx--api-key--xxxx"; $response = $imageInfo->Process();
if($response->IsSuccessful) { echo ($response->JsonContent); } else { echo("Error: "); echo($response->StatusCode); echo($response->ErrorMessage); } }}GetImageInfo::Run();
- Run the application
php GetImageInfo.php
and the image JSON metadata prints to the console.
In all four languages, the steps were similar. First, we created a new ImageResource
instance by loading the path to the image via the constructor. Next, we created a new instance of the ImageInfo
class, which abstracts the image-info
endpoint. Then the ImageInfo
instance prints the extracted image information as JSON after processing. Finally, we called the Process
method and print the resultant JSON to the console.