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
}
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;
}
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;
}
}
}
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.