Image API

This API is used to turn content like image galleries, Instagram posts, or image-rich articles into JSON.

For examples of data that might be returned, please see http://diffbot.com and run the Image API demo.

The Image API part of the Diffbot PHP client consists of two main classes: the API class, and the Image Entity class. We’ll describe them in order. Note that the API class extends Swader\Diffbot\Abstracts\Api, so be sure to read that first if you haven’t already.

Image API Class

class Swader\Diffbot\Api\Image

Basic Usage:

use Swader\Diffbot\Diffbot;

$url = 'http://some-article-to-process.com';

$diffbot = new Diffbot('my_token');
$api = $diffbot->createImageApi($url);

Swader\Diffbot\Api\Image::setMentions($bool)
Parametri:
  • $bool (bool) – Either true or false
Vraća:

$this

If set to true, the Image API will attempt to identify other locations online where the image was used - similar to Google Image reverse search.

Swader\Diffbot\Api\Image::setFaces($bool)
Parametri:
  • $bool (bool) – Either true or false
Vraća:

$this

Highly experimental. Finds the x, y, height and width of coordinates of human faces.

Swader\Diffbot\Api\Image::setOcr($bool)
Parametri:
  • $bool (bool) – Either true or false
Vraća:

$this

Highly experimental. If set, attempts to recognize and read text in the images.

Image Entity Class

When the Image API is done processing a URL, the result will be an instance of Swader\Diffbot\Entity\EntityIterator containing one Image Entity instance for every image found.

For an overview of the abstract class all Entities (including Image) build on, see Swader\Diffbot\Abstracts\Entity.

Note that the Image entities can also be returned by the Swader\Diffbot\Api\Analyze API in “image” mode, or in default mode when processing a URL that is essentially an image.

class Swader\Diffbot\Entity\Image

Swader\Diffbot\Entity\Image::getType()
Vraća:string

Will always return “image” for images:

// ... API setup ... //
$result = $api->call();

echo $result->getType(); // "image"

Swader\Diffbot\Entity\Image::getHeight()
Vraća:int

Height of image if resized by browser via CSS / JS. If not resized, serves as alias for Swader\Diffbot\Entity\Image::getNaturalHeight.

Swader\Diffbot\Entity\Image::getWidth()
Vraća:int

Width of image if resized by browser via CSS / JS. If not resized, serves as alias for Swader\Diffbot\Entity\Image::getNaturalWidth.

Swader\Diffbot\Entity\Image::getNaturalHeight()
Vraća:int

Raw image height, in pixels.

Swader\Diffbot\Entity\Image::getNaturalWidth()
Vraća:int

Raw image width, in pixels.

Swader\Diffbot\Entity\Image::getUrl()
Vraća:string

URL of the image

Swader\Diffbot\Entity\Image::getAnchorUrl()
Vraća:string | null

URL the image links to, if any. Null if image isn’t linked.

Swader\Diffbot\Entity\Image::getXPath()
Vraća:string

The XPath expression of the position of the image node in the DOM.

Swader\Diffbot\Entity\Image::getMentions()
Vraća:array

Returns an array of [title => “title”, link => “link”] arrays for all posts where this image, or a similar one, was found. If not found, returns empty array.

Swader\Diffbot\Entity\Image::getFaces()
Vraća:array | string

Finds the x, y, height and width of coordinates of human faces, returns array of arrays with those keys. In most cases, does not work at all and is in heavy alpha mode. Do not rely on this method for anything. Returns empty string if nothing found.

Swader\Diffbot\Entity\Image::getOcr()
Vraća:string

The text recognized in the picture. In most cases, does not work at all and is in heavy alpha mode. Do not rely on this method for anything. Returns empty string if nothing found.