2

FacebookのグラフAPIからアルバム写真を取得しています。
このコードはうまくいきますが、130 以上の幅:130 x 高さ:130 の最小のサムネイルを選択しようとしています。

つまり、幅と高さの合計ではなく、幅または高さが 130 以上である必要があります。

注:配列リストは facebooks アルバムからの画像のバリエーションであるため、比例します。したがって、ポートレートまたはランドスケープの寸法である場合、それに応じて寸法が調整されます。

したがって、print_r以下から、最初の配列の項目 (2) がその説明に適合することがわかりますが、これは幅/高さが 130 を超える最小であるため、他の配列は数値 (2) と (1) になります。

$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

}
4

3 に答える 3

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 ピクセルを超える最小のサムネイルを $thumbnail 配列に格納する必要があります。

e: ところで、元の配列に保存されているサムネイルの 1 つを参照する配列があるということです。サムネイル全体を保存するわけではありませんが、「url」、「width」、および「height」インデックスを追加するだけで保存できると思います。

e2: また、これは総ピクセル サイズを考慮して最小をチェックします。マークがコメントで言ったように、合計ピクセルサイズを最小にするか、幅/高さにするかは不明です。

于 2013-07-29T21:44:03.253 に答える