Matthew Lawson
Matthew Lawson

Reputation: 33

Instagram API non-square images not being returned for $data->images->standard_resolution

API URL: api.instagram.com/v1/users/self/media/recent/?access_token=kablenghe

I'm trying to figure out a way to take the image URL for a standard_resolution image, but Instagram keeps serving up the cropped images.

Cropped Image URL:

scontent.cdninstagram.com/t51.2885-15/s480x480/e35/c236.0.607.607/14474173_314142642294695_7071485231332589568_n.jpg?ig_cache_key=blah

Non-cropped Image URL:

scontent.cdninstagram.com/t51.2885-15/s480x480/e35/14474173_314142642294695_7071485231332589568_n.jpg?ig_cache_key=splerf

So I need to removed the portion: "/c236.0.607.607" which is what Instagram uses to position and size the image crop. Several people are asking this question, but I'm not finding anyone who's found the answer to this irritating little Instagram API issue with cropped images and non-square media.

Upvotes: 1

Views: 678

Answers (3)

krisrak
krisrak

Reputation: 12952

Why dont you just check the box for non-square media in Client settings: enter image description here

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

You may use the following regexp:

'~/c\d{1,4}(?:\.\d{1,4}){3}/~'

See the regex demo

Details:

  • / - a slash
  • c - a literal c char
  • \d{1,4} - 1 to 4 digits
  • (?:\.\d{1,4}){3} - 3 sequences of
    • \. - a dot
    • \d{1,4} - 1 to 4 digits
  • / - a slash (if you remove it from the pattern, the regex may match a part of another URL subpart, but if you do, you will need to replace with an empty string)

PHP demo:

$re = '~/c\d{1,4}(?:\.\d{1,4}){3}/~';
$str = 'scontent.cdninstagram.com/t51.2885-15/s480x480/e35/c236.0.607.607/14474173_314142642294695_7071485231332589568_n.jpg?ig_cache_key=blah';
$result = preg_replace($re, '/', $str);
echo $result;

Upvotes: 0

Matthew Lawson
Matthew Lawson

Reputation: 33

Ok, so I think I may have actually figured it out. Here's what I have:

// The standard_resolution URL from Instagram API Response
$full_res_cropped = $item['images']['standard_resolution']['url'];

// Regex Pattern        
$pattern = '#\/c+(.\d[0-9]+\.\d.)+(\d)#';

// Replace with empty string
$replacement = '';

// preg_replace
$full_res = preg_replace($pattern, $replacement, $full_res_cropped);

So the results are:

// $full_res_cropped cropped image URL (original)
scontent.cdninstagram.com/t51.2885-15/s480x480/e35/c236.0.607.607/14474173_314142642294695_7071485231332589568_n.jpg?ig_cache_key=flarg

// $full_res resized, non-cropped URL (fixed)
scontent.cdninstagram.com/t51.2885-15/s480x480/e35/14474173_314142642294695_7071485231332589568_n.jpg?ig_cache_key=flarg

This will work until Instagram changes the way that they crop and position the images and change the URL structure, but until then. Cool, I guess.

Hope this helps someone else having a similar issue!

Upvotes: 1

Related Questions