2013-07-29 28 views
2

我從facebook的圖表api中檢索相冊照片。
此代碼的伎倆,但我試圖選擇等於130及以上寬度的最小縮略圖:130 x高:130
解析php數組,並根據條件選擇項目

意味着寬度或高度必須是130以上,而不是寬度和高度之和。

注意:數組列表是來自Facebook專輯的圖像變化,所以它們會成比例。所以如果它是縱向或橫向尺寸,則尺寸會相應地縮放。

因此,從下面的print_r可以看到第一個數組中的item(2)符合該描述,但其他數組將會是數字(2)和(1),因爲這是130寬度/高度上的最小值

$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); 

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 
       ) 


) 

問題輸出:我怎麼會寫這個在PHP?

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

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

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

} 
+0

這是否需要基於總像素數?或純粹在一個或其他維度高於130?哪個更小?寬度= 140,高度= 140;或寬度= 130;身高= 200? –

+0

你從哪裏得到'數據'索引​​? (無視最後編輯,沒有仔細閱讀) – vanamerongen

+1

@vanamerongen - 我想從Facebook圖表API –

回答

2

我相信這可能適合你。它將返回滿足最小寬度/高度的最小圖片,如果沒有圖片滿足,則返回false。

$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; 
} 
+0

在你的測試中,如果你改變了min_width到131你會得到一個錯誤的'$ pic = getTheRightPic($ test,131,130);' –

+0

不好,剛剛得到了130X180。 –

+1

哦,它在數組中,當我添加了另一個值',4 => array( 'height'=> 100, 'width'=> 120, 'source'=>'https://myur5.jpg ' )'它打破了 –

2

下面是一些對你的代碼中,我們首先建立一個圖像陣列來比較具有非常大的尺寸,我們再循環雖然你的陣列,陣列的發現具有寬度和高度的圖像即大於130,但該區域小於所選圖像。

$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; 
     } 
    } 
} 
+0

你怎麼從數組中獲得'source'? –

+0

@tq'$ selectedPicture ['source']' – cmorrissey

1

這是你所需要的?我認爲這有效,但我沒有測試。

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

然後,你應該對存儲在$縮略圖排列在130 X 130像素的最小縮略圖,如果我沒有記錯。 e:哦,順便說一句,我的意思是你有數組,存儲在原始數組中的縮略圖之一。你不會存儲整個縮略圖,但我想你可以通過給它添加'url','width'和'height'索引來做到這一點。

e2:此外,這考慮到總共像素大小以檢查最小。像Mark在他的評論中說的,目前還不清楚你想要的總像素大小是最小的還是寬度/高度。

+0

謝謝你這一切,但它不是選擇正確的一個 –

+0

嗯,好吧,我只能說:它檢查它是否超過130的高度和寬度,然後檢查圖片的最小總尺寸。仔細閱讀每一行,並閱讀它所做的每件事的評論,然後看看你是否真的希望它存儲。 – vanamerongen

+0

謝謝我讓它工作,但在其中一些''圖片['寬度']> 130 && $圖片['高度']> 130' &&似乎沒有正確過濾,其允許較小的高度得到通過 –