-1

How can i get an specific size of an image in the REST API?

I have created a view that displays as a REST export, the format of the view is:

  • Format: Serializer
  • Show: Entity

The image field is shown as:

field_image [
  {
    "target_id": "9",
    "alt": "Foo",
    "title": "",
    "width": "2304",
    "height": "3456",
    "target_type": "file",
    "target_uuid": "99d6732e-e9c3-4e05-be87-d2868e9f149d",
    "url": "http://mysite/sites/default/files/2016-12/foo-image-1313021.jpg"
  }
]

Where can I set the type of style(/admin/config/media/image-styles) that should return me in JSON format?

If I change the format by:

  • Format: Serializer
  • Show: Fields

It returns me the img tag.

  • isn't the "width" and "height" the size? Have you tried to REWRITE RESULTS of those fields? – No Sssweat Dec 24 '16 at 11:41
  • @NoSssweat In the "Image styles" /admin/config/media/image-styles there are 11 types of images sizes. How can I get a specific size and not the original image? – Lenin Meza Dec 26 '16 at 16:42
0

I ended up with the next configuration of my REST View. Not ideal but works...

FORMAT

  • Format: Serializer
  • Show: Fields

FIELDS

  • Content: Title
  • Content: Image
  • Content: Data

The settings of the image field are:

  • Formatter: Image
  • Image style: Max 650x650

The post is returned as:

{
    "title": "Foo",
    "field_image": "<img src=\"/sites/default/files/styles/max_650x650/public/2016-12/foo-1313021.jpg?itok=UiQ2F7lI\" width=\"433\" height=\"650\" alt=\"Bar\" typeof=\"foaf:Image\" class=\"img-responsive\" />",
    "field_event_date": "2016-12-02T13:04:03"
}

And i apply the next Javascript function in the frontend:

// Get image tag source attribute
var getImgSrc = function( tag ) {
    var rex = /<img[^>]+src="?([^"\s]+)"?[^>]*\/>/g;
    var m = rex.exec( tag );
    return m[1];
}
var src = getImgSrc( post_content.field_image );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.