2

I am retrieving album photos from facebook's graph api.
This code does the trick, however i am trying to select the smallest thumbnail that equal 130 and above width:130 x height:130

meaning the width OR height has to be 130 and above, NOT the sum of width and height.

note: the array list are image variation from facebooks album, so they would be in proportion. so if it is a portrait or landscape dimension it would scale in dimension accordingly.

So from the print_r below you can see the in the first array, item (2) fits that description, but the other arrays would be numbers (2) and (1) since this is the smallest above 130 width/height

$userURL2 = "https://graph.facebook.com/$albumID/photos?access_token=" . $fb_oAuth_token;
$ch2 = curl_init($userURL2);
curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
$data2 = curl_exec($ch2);
curl_close($ch2);
$pictures = json_decode($data2, true);

print_r($pictures);

output from print_r($pictures);

Array
(
        [0] => Array
                (
                        [height] => 288
                        [width] => 460
                        [source] => https://myurl.jpg
                )
        [1] => Array
                (
                        [height] => 200
                        [width] => 320
                        [source] => https://myurl.jpg
                )

        [2] => Array
                (
                        [height] => 130
                        [width] => 180
                        [source] => https://myurl.jpg
                )

        [3] => Array
                (
                        [height] => 81
                        [width] => 130
                        [source] => https://myurl.jpg
                )

)

Array
(
        [0] => Array
                (
                        [height] => 500
                        [width] => 500
                        [source] => https://myurl.jpg
                )

        [1] => Array
                (
                        [height] => 500
                        [width] => 500
                        [source] => https://myurl.jpg
                )

        [2] => Array
                (
                        [height] => 480
                        [width] => 480
                        [source] => https://myurl.jpg
                )


)
Array
(
        [0] => Array
                (
                        [height] => 335
                        [width] => 300
                        [source] => https://myurl.jpg
                )

        [1] => Array
                (
                        [height] => 335
                        [width] => 300
                        [source] => https://myurl.jpg
                )


)

question: How would i write this in php?

foreach($pictures['data'] as $picture){

    $width = $picture['width'];
    $height = $picture['height'];

    // choose the smallest [source] above width:130 x height:130

}
18
  • Does this need to be based on total number of pixels? Or purely on one or other dimension being above 130? Which is the smaller? width = 140, height = 140; or width = 130; height = 200? Commented Jul 29, 2013 at 21:29
  • 1
    @vanamerongen - I'd guess from Facebook graph API Commented Jul 29, 2013 at 21:31
  • 1
    @tq - I asked, which would you'd want if you have 2 entries, one 130x200, the other 140x140 - it's not in your sample data, but you might get such dimensions from dynamic data in the future Commented Jul 29, 2013 at 21:48
  • 1
    @vanamerongen thanks for that! let me re do my question Commented Jul 29, 2013 at 22:00
  • 1
    @tq Heh, don't worry about it. Understand the confusion. If that's the case then you could use the example I gave in my answer, which checks for total size. But if the proportions stay the same, you may as well only check for only one of the values (width or height) and leave out the $size variable (still talking about after the check for > 130 width and height. Commented Jul 29, 2013 at 22:14

3 Answers 3

2

I believe this may work for you. It will return the smallest pic that satisfies the min width/height or false if none of the pictures satisfy.

$test = array(
    0 => array(
        'height' => 288,
        'width' => 460,
        'source' => 'https://myurl.jpg'
    ),
    1 => array(
        'height' => 81,
        'width' => 130,
        'source' => 'https://myurl.jpg'
    ),
    2 => array(
        'height' => 200,
        'width' => 320,
        'source' => 'https://myurl.jpg'
    ),
    3 => array(
        'height' => 130,
        'width' => 180,
        'source' => 'https://myurl.jpg'
    ),
    4 => array(
        'height' => 100,
        'width' => 120,
        'source' => 'https://myur5.jpg'
    )
);


$pic = getTheRightPic($test, 130, 130);

var_dump($pic);


function getTheRightPic($pictures, $min_width, $min_height)
{
    //get one to start with
    do
    {
        $win_pic = array_pop($pictures);
    }
    while ( ! checkXY($win_pic, $min_width, $min_height));

    //if none of the pic satisfies return false
    if ($win_pic === false)
    {
        return false;
    }

    foreach ($pictures as $pic)
    {
        $win_pic = comparePics($win_pic, $pic, $min_width, $min_height);
    }

    return $win_pic;
}

function comparePics($original, $compare, $min_width, $min_height)
{
    if ( ! checkXY($compare, $min_width, $min_height))
    {
        return $original;
    }

    //calculate sizes
    $original['size'] = $original['width'] * $original['height'];
    $compare['size']  = $compare['width'] * $compare['height'];

    //return the smaler pic
    if ($original['size'] > $compare['size'])
    {
        return $compare;
    }

    return $original;
}

function checkXY($pic, $min_width, $min_height)
{
    if ($pic['width'] < $min_width && $pic['height'] < $min_height)
    {
        return false;
    }

    return true;
}
Sign up to request clarification or add additional context in comments.

6 Comments

in your test, if you change the min_width to 131 you get a false $pic = getTheRightPic($test, 131, 130);
Nope, just got 130X180.
oh, its in the array, when i added another value ,4 => array( 'height' => 100, 'width' => 120, 'source' => 'https://myur5.jpg' ) it broke
@t q adding that array does break it, so I edited to correct.
In that case just change this line like ($pic['width'] < $min_width || $pic['height'] < $min_height) so both height and width must meet the requirement. Now condition is satisfied if at least one satisfies.
|
2

Here is some code for you, we are first setting up a picture array to compare against that has very large dimensions, we are then looping though your arrays arrays to find an image that has both the width and height that is greater then 130 but the area is less then the selected image.

$selectedPicture = array('width' => 10000, 'height'  => 10000, 'source' => '');

foreach($pictures as $album){
    foreach($album as $picture){
        if($picture['width'] > 130 && $picture['height'] > 130 && ($picture['width'] * $picture['height']) < ($selectedPicture['width'] * $selectedPicture['height'])){
            $selectedPicture = $picture;
        }
    }
}

1 Comment

@tq $selectedPicture['source']
1

Is this what you need? I think this works, but I haven't tested.

$thumbnail = array();                // array to store the smallest thumbnail over 130 x 130 in

foreach($pictures['data'] as $key => $picture){
    if($picture['width'] > 130 && $picture['height'] > 130){        // Check if thumbnail has height AND width over 130.
        $size = $picture['width'] * $picture['height'];     // Store total pixel size since the check for > 130 width and height was already done anyway.
        if(!isset($thumbnail['index'])){               // Check if there's a thumbnail already stored,
            $thumbnail['index'] = $key;                // and if not, store this one
            $thumbnail['size'] = $size;
        } elseif($thumbnail['size'] > $size) {         // Check if currently stored thumbnail is bigger,
            $thumbnail['index'] = $key;                // And if it is, store this one
            $thumbnail['size'] = $size;
        }
    }
}

And then you should have the smallest thumbnail over 130 x 130 px stored in the $thumbnail array if I'm not mistaken.

e: Oh, btw, I mean you have the array referring to one of the thumbnails stored in the original array. You wouldn't be storing the whole thumbnail, but I guess you could just do that by adding 'url', 'width', and 'height' indexes to it.

e2: Also, this takes into account the total pixel size to check for smallest. Like Mark said in his comment, it's unclear if you want the total pixel size to be the smallest one or the width/height.

3 Comments

thank you for all this, but it isnt choosing the correct one
Hm, well, all I can say is: it checks if it's over 130 height and width and it then checks for the smallest total size of the picture. Go through every line and read the comments for each thing it does, then see if that's what you actually want it to store.
thanks i got it to work, but on some of them the $picture['width'] > 130 && $picture['height'] > 130 && doesnt seem to be filtering properly. its allowing smaller height to get thru

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.