S
S
Shlyahten2017-04-05 19:00:17
PHP
Shlyahten, 2017-04-05 19:00:17

What is the best way to extract a photo of the maximum size from the VK API?

Here is the result of the code var_dump($result -> response -> items[0] -> attachments[0] -> photo); :

object(stdClass)[5]
  public 'id' => int 456239017
  public 'album_id' => int -7
  public 'owner_id' => int -119862681
  public 'user_id' => int 100
  public 'photo_75' => string 'https://pp.userapi.com/c638522/v638522457/47abd/T0LAROT98EI.jpg' (length=63)
  public 'photo_130' => string 'https://pp.userapi.com/c638522/v638522457/47abe/GwUvwaKff_I.jpg' (length=63)
  public 'photo_604' => string 'https://pp.userapi.com/c638522/v638522457/47abf/jVm1hsXC7Nc.jpg' (length=63)
  public 'photo_807' => string 'https://pp.userapi.com/c638522/v638522457/47ac0/75K-RSy1kQc.jpg' (length=63)
  public 'width' => int 800
  public 'height' => int 800
  public 'text' => string 'Original: http://cs639119.userapi.com/v639119880/12bab/3czHoHZnZGs.jpg' (length=70)
  public 'date' => int 1491404363
  public 'post_id' => int 126
  public 'access_key' => string 'b7132ccfd6d6e03c98' (length=18)

How to choose a photo with the maximum resolution? Not exactly in this case, but to work with other sizes / photographs. Thank you)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
Pavel Gromadchuk, 2017-04-05
@Gromadchuk

My version of what's going on.

$photo = (object) array(
    'id' => 456239017,
    'album_id' => -7,
    'owner_id' => -119862681,
    'user_id' => 100,
    'photo_75' => 'https://pp.userapi.com/c638522/v638522457/47abd/T0LAROT98EI.jpg',
    'photo_130' => 'https://pp.userapi.com/c638522/v638522457/47abe/GwUvwaKff_I.jpg',
    'photo_604' => 'https://pp.userapi.com/c638522/v638522457/47abf/jVm1hsXC7Nc.jpg',
    'photo_807' => 'https://pp.userapi.com/c638522/v638522457/47ac0/75K-RSy1kQc.jpg',
    'width' => 800,
    'height' => 800,
    'text' => 'Original: http://cs639119.userapi.com/v639119880/12bab/3czHoHZnZGs.jpg',
    'date' => 1491404363,
    'post_id' => 126,
    'access_key' => 'b7132ccfd6d6e03c98'
);
         
$max_photo = array(
    'size' => 0,
    'url' => null
);

foreach($photo as $key => $value) {
    preg_match('/photo_([0-9]+)/', $key, $matches);
    
    if ($matches && $matches[1] > $max_photo['size']) {
        $max_photo = array(
            'size' => intval($matches[1]),
            'url' => $value
        );
    }
}

var_dump($max_photo);

S
Shlyahten, 2017-04-05
@Shlyahten

This is of course a crutch, but here's how I did it:

$array=json_decode(json_encode($array),true);

foreach ($array as $param => $value) {
    if (strstr($param, 'photo_')) {
    $photo = $value;
    }
}
echo $photo;

R
Roman Igoshin, 2017-04-06
@MasterRO

OMG why $array=json_decode(json_encode($array),true); ??
You already have a finished object.
$photo = $result -> response -> items[0] -> attachments[0] -> photo);
$url = $photo->photo_1024 ?? $photo->photo_807 ?? $photo->photo_604 ?? ...

U
UksusoFF, 2018-04-12
@UksusoFF

For those who came here from a search:
For photos.get, you can specify photo_sizes=1, then at least it will not be necessary to parse the name of the keys and sort nested objects by size.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question